Search code examples
angularazure-web-app-serviceazure-deployment-slots

Angular 2 configuration issues with Azure Slot deployments


I'm having a problem with Angular environment variables and Azure Slots

We want to use an app service providing static Angular files and we also want to use Azure slots to make our deployments safer.

The reasons we want to use slots are:

  1. Ensure that code works in the production environment before making it fully available to everyone
  2. Reduce downtime to almost nil as there is no build being deployed during the "go-live" phase

Our Angular site only serving up static files means that slot deployments require a different build to populate the env.json environment settings differently for each slot.

The solution we're thinking of taking is to create an endpoint in the same Angular website and make a call from the Angular site back to its origin to get its configuration. This way configuration can be set differently in the staging and production slots in Azure and only one Angular build would be required.

We need some server side code to pick up these Azure Application settings and provide them back in an endpoint on the site. We are yet to make a decision about the technology we use to create that endpoint - we are thinking either .Net Core or NodeJs at the moment because they seem to fit well with the Angular product and the development team.

Does anyone have any experience of plugging in a server side component to provide configuration for a previously static Angular website?


Solution

  • To achieve your requirement, you can just put the following PHP file to your site root folder. Then send a GET request to endpoint http://<websitename>.azurewebsites.net/appSettings.php via your Angular app. This will give you a JSON Object that contains all App settings.

    appSettings.php

    <?php
    
    $appSettings = [];
    
    foreach ($_SERVER as $key => $value) {
    
        if(preg_match('/^APPSETTING_/', $key)) {
            $appSettings[str_replace('APPSETTING_', '', $key)] = $value;
        }
    }
    
    header('Content-Type: application/json');
    echo json_encode($appSettings);