Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build once and deployed multiple environments using angular-cli 4?

We planned to deploy angular into amazon S3 static website, currently we are doing separate build for each environments(development/production). Is any solution to build once and deployed to all environment.

Thanks in advance.

like image 886
Saravanan Krishnan Avatar asked Sep 27 '17 06:09

Saravanan Krishnan


People also ask

How do you create an angular app once and deploy it to multiple environments?

All we have to do is: Add a JSON configuration file in the src folder. Update our angular/webpack configuration to include the file in our dist folder. Add a simple configuration service with a call to get our config data from our config file.

How angular manage multiple environments CLI?

Adding multiple environments:Open your angular project in a preferred IDE. Navigate to src>environments, there you can see two files environment. ts and environment. prod.

How is angular application deployed in production?

When you add a package with deployment capability, it'll automatically update your workspace configuration ( angular.json file) with a deploy section for the selected project. You can then use the ng deploy command to deploy that project. For example, the following command automatically deploys a project to Firebase.


1 Answers

As I understand you right you want to have one build (e.g. a continuous integration build) for several stacks (develop, test, production and so on). If you have different settings in your environment file for each stack you will have different builds. You have only one option to achieve that with only one build.

In your develop environment file keep your developing settings, which you need for developing locally. In your production environment file use Placeholders for the settings, which are different to your developing settings.

Then you can always build your application for production and you will have your application with the Placeholders compiled inside the main.*.bundle.js.

For Deployment you have to manipulate the main.*.bundle.js file (there are the Placeholders):

  1. Pre-Deploy: run a script which manipulates the main.*.bundle.js to set the e.g. production settings.
  2. Deploy: Deploy the application to your App Service
  3. Post-Deploy: run a script which manipulates the main.*.bundle.js to set the placeholders again

Yes that isn't a nice solution, but after 3 days of researching this is the best option. You can then deploy your application for several stacks with running the scripts.

I did this with powershell scripts on Visuals Studio Team Services to use the release functionality there to deploy the application to Microsoft Azure.

like image 190
alfredson Avatar answered Sep 19 '22 20:09

alfredson