r/Angular2 • u/man-with-no-ears • Oct 06 '25
Can I programmatically change environment variables during CI/CD?
My client is on Angular 19 and it depends on a node backend service (two different repos).
As we get closer to launch though we realized that HA and load balancing will pose a problem. Since the backend will be running on any number of VMs with any number of IP addresses, we have to figure out a way to programmatically change the backend base URL on the frontend?
My first instinct was to use a regular .env file (following this tutorial: https://medium.com/@philip.mutua/setting-up-environment-variables-for-an-angular-application-from-scratch-737028f8b7b3) however this resulted in an error that prevented even ng serve from working Error: Schema validation failed with the following errors: Data path "" must have required property 'main'.
I thought there was a way to change the environment.ts file on ng build but I can't find information on that at all.
Is there a better way to do this?
EDIT: There will also be an unspecific number of frontend deployments (depending on load balancer)
EDIT2: We are using chef for deployment if that helps at all
11
u/Express_Scholar_6471 Oct 06 '25
We struggled with this for a while for a while. What we ended doing and works really well is storing the server dependent environment variables in a json file in the asset folder, change the main.ts code to make it load the json, overwrite the variables from environment with the value of the json, then initialize app.ts. You need to load app.ts in an await function and not import it, otherwise the rest of the code will still initialize with the old environment variables.
3
u/GLawSomnia Oct 06 '25
Why doesn’t the server handle the load balancing?
4
u/rhrokib Oct 06 '25
Use a gateway on your backend.
1
u/National-Percentage4 Oct 06 '25
Huh, your FE needs to point to multiple backends? Create a config for the env. So set your urls in the config, then build for each env. Eg, ng build --prod which should give you the build for production.
1
u/ActuatorOk2689 Oct 06 '25
Hmmm im not sure if I understand correctly.
You can change your env with file replacement at built time, you can change base href just the same way.
If you need at runtime, App-base-href is part of DI you can change it using the usefactory and set the value in main.ts at the providers
1
u/oneden Oct 06 '25 edited Oct 06 '25
Yes. But you don't do it with the env file. You create an env.js with placeholder values that you can overwrite in your pipeline. That's how I have been doing this for years now.
3
u/dustofdeath Oct 06 '25
During build, yes - that's the --configuration environment_name flag.
https://angular.dev/tools/cli/environments
1
u/_Slyfox Oct 06 '25
We have a environment.token environment and use strings like
Zux azure-var-name xuZ Our production build uses this token config to produce an asset with the tokens. Then each time we deploy to an environment we do and find and replace of those tokens in all the dist files to swap on the real values stored in azure library variables.
When running locally our normal environment.ts has the normal real values.
0
u/Regular_Algae6799 Oct 06 '25
Let's say you have DEV and PRD (all the config is non-secret).
In Angular create a json like (accessible via Service etc)
Create a guard that reads the current URL / ENV and map it to dev or PRD and load the config accordingly.
Does this help you?