I have a situation where I’m trying to build my angular application with production config and deploy to multiple environments, say, ng build --configuration=production
The work flow here is when I build using the above command (ng build --configuration=production), the environment.ts file gets replaced with environment.prod.ts
The configurations I have in environment.prod.ts is as follows,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
The configurations I have in environmrnt.test.ts is as follows,
export const environment = {
production: true,
environment: 'Test',
_webApiHost: 'testsomename.company.com/api/',
};
The setting I have on angular.json file is as follows,
"configurations": {
"production": {
"fileReplacements": [ {
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
},
"test": {
"fileReplacements": [ {
"replace": "src/assets/configs/environment.ts",
"with": "src/assets/configs/environment.test.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
}
}
If I build the solution for every environment separately and deploy to appropriate environment as below figure,
it works like charm, which mean the,
testApp communicates to _webApiHost: testsomename.company.com/api/ and prodApp communicates to _webApiHost: prodsomename.company.com/api/
In the above case the artifact which is tested by QA is different from the artifact which is deployed to production, which is not the ideal way of pushing the code to production.
But my concern is I want to build the app only once and deploy it to multiple environments, where each environment will communicate to appropriate api, like below figure,
When I build it using the command ng build --configuration=production, the environment.ts file will have production configurations,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
So if that artifact is deployed to test environment, the testApp is trying to communicate with _webApiHost: 'prodsomename.company.com/api/, which is not right.
Here is the Azure DevOps build pipeline powershell script I use to build the solution.
Set-Location "$(Build.Repository.LocalPath)\Buffini.Web.UI\Angular"
Write-Host 'Angular Install Starting'
npm install -g @angular/[email protected] -Verbose
Write-Host 'Angular Install Finished'
Write-Host 'NPM Install Starting'
npm install -Verbose
Write-Host 'NPM Install Finished'
Write-Host 'NPM Update Starting'
npm update -Verbose
Write-Host 'NPM Update Finished'
Write-Host 'NPM Audit Starting'
npm audit fix -Verbose
Write-Host 'NPM Audit Finished'
Write-Host 'Angular Build Starting'
ng build --configuration=production --deleteOutputPath=true
Write-Host 'Angular Build Finished'
I have tried searching for a solution online but I couldn’t find any. Please help me in resolving the issue. I’ll highly appreciate your time and help on this. Thanks in advance.