Efficient Angular Environment Management with Azure Pipelines

Managing different environment configurations for a modern Angular web application can be daunting. However, with the help of Azure DevOps Release Pipelines, you can streamline this process to ensure efficient and hassle-free environment management.

This blog post will explore how to request an Angular environment as a JSON file and keep the configuration in Azure variables to set it at the Release Pipeline stage. With this approach, we can easily manage multiple environments and ensure our application is deployed with the correct configurations for each environment.

Let’s dive in!

Configure Angular Application with a Base Environment

Before we configure an Angular app, let’s first create a new one. We will call it “env-management”.

ng new env-management

Next, we should add an environment.json file, which the pipeline task will transform during release. We can put this file in the “assets/config” folder or any other folder that works better for you. The contents of this file should include any configuration you want to vary by environment, such as the API’s base URL, which we will set by the Azure Release Pipeline.

{
  "apiBaseUrl": "$apiBaseUrl",
}

In the next step, we should create an APP_CONFIG Injection Token to use in our app. This token will allow us to inject the configuration into our application.

export interface IAppConfig {
  apiBaseUrl: string;
}

export const APP_CONFIG: InjectionToken<IAppConfig> = 
    new InjectionToken<IAppConfig>('APP_CONFIG');

Fetch Environment

In our application’s main.ts file, we need to wrap the application bootstrap with a fetch request to retrieve the environment configuration file. The fetch request uses the environment name and build number to determine which configuration file to retrieve. Once the fetch request is complete, we can bootstrap our application with the retrieved configuration.

fetch(`/assets/config/${isDevMode() ? `environment.${buildVariables.environmentName}` : 'environment'}.json?v=${buildVariables.buildNumber.replace(/\D/g, '')}`)
  .then((response) => response.json())
  .then((config: IAppConfig) => {
    bootstrapApplication(AppComponent, {
      // App configuration
      providers: [
        // App providers here
        {
          provide: APP_CONFIG,
          useValue: {
            ...config,
            portalType,
          },
        },
      ],
  })
});

Disable Cache for the Main File

In our setup, we’re using Azure Static Web Apps, which will cache heavily by default, so we should turn off caching for our environment file. We can do this by creating a staticwebapp.config.json file (if you don’t have one already) and adding the following code to “routes”:

{
  "route": "/assets/config/environment.json",
  "headers": {
    "Cache-Control": "no-cache" // or "no-store"
  }
},

If you host your Angular application using some other technology, you can use another approach that works for your hosting setup. For example, you can add a timestamp to the environment file URL to ensure it’s always fresh.

fetch(`/assets/config/${isDevMode() ? `environment.${buildVariables.environmentName}` : 'environment'}.json?v=${new Date().toISOString()}`)

Add Environment Variables in Azure

To create variables for different environments, we need to go to Azure DevOps -> Pipelines -> Library and create a set of variables. Here, we can specify the API base URL for our environment.

Create Azure Release Pipeline

For the final step, we need to set up our release pipeline. We can do this by going to Releases and clicking “+ New” -> “New release pipeline” (or edit the existing one).

NOTE: You can use Azure CLI for this purpose if you prefer using terminal instead of UI.

Then we add the Files Transform task, which will transform our environment configuration file during release. We set the env file folder path in the “Package or folder” field and select JSON in “File format” — set environment.json as “Target Files”.

NOTE: Remember to link development environment variables. Go to the “Variables” -> Variable groups -> Link variable group and select the required variable groups.

Add other required tasks to deploy your application and hit the “Save” button. That’s it. Now, each time you run the release pipeline, it will replace values in the environment.json file with ones you added as Variables of the pipeline.

Conclusion

Managing different environment configurations for an Angular application can be complex and tedious, especially when you must keep environment configuration in TypeScript files, which requires the application to rebuild each time you change it. Instead, we can use Azure DevOps release pipelines to easily manage multiple environments and make changes to their configuration without the need to rebuild the application by requesting an Angular environment as a JSON file and saving the configuration in Azure variables.

Picture of Maks Nechesonov

Maks Nechesonov

Maks is a highly experienced front-end developer with over nine years of experience in the industry. He has a strong background in creating responsive designs and web app development. He is passionate about delivering high-quality code, improving development processes, and always seeking ways to improve his skills. When not occupied with coding, Maks enjoys playing futsal, video games and board games, and spending time with his family.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Sentiment Analysis in C#: Azure AI Language or LLMs

Explore how C# developers can leverage both Azure AI Language and Azure OpenAI for sentiment analysis. This post compares traditional NLP services with LLM-based approaches using real-world code from an exit interview system, helping you choose the right tool for your specific use case.

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.