0

On an Angular 7 application I have the following environment.prod.ts file:

export const environment = {
  production: true,
  apiBaseUri: 'https://api.xyz.com'
};

I need to publish the application in 2 different machines.

And I need, for each one, two configurations: Production and Staging.

So I need to have something like the following environment files:

environment.prod.machine-1.ts
environment.prod.machine-2.ts

environment.staging.machine-1.ts
environment.staging.machine-2.ts

How should I defined these environment files?

And how to build the application?

1

1 Answer 1

2

In angular.json, under "projects.<your project name>.architect.build.configurations" you can define each type of configuration per machine. Under each configuration define fileReplacements, like so:

"projects": {
  "<your project name>": {
    "architect": {
      "build": {
        "configurations": {
          "machine-1.production": {
            "fileReplacements": [
              {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.prod.machine-1.ts"
              }
            ]
          },
          "machine-1.staging": {
            "fileReplacements": [
              {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.staging.machine-1.ts"
              }
            ]
          },
          // same for machine 2, etc...
        }
      }
    }
  }
}

Then, to build your project using that environment, run

ng build --configuration=machine-2.staging

(or whichever other environment you want).

Sign up to request clarification or add additional context in comments.

2 Comments

I see. One thing that confuses me is the use of "production: true / false" on the environment files. What does it do?
@MiguelMoura There's no such "production" property defined under "configurations.<your configuration>" according to the angular.json schema. What do you mean exactly? Are you referencing the --prod argument for ng build? If so, it's just a shortcut of --configuration=production, so if you have no configuration named "production" under "projects.<your project name>.architect.build.configurations", running ng build --prod won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.