Integrating Angular4 application with VSTS and Azure App Service

This seed project has been created to assist fast tracking some of the issues that can arise when integrating an Angular 4 application with VSTS. This project aims to achieve the following goals:

This project was generated with Angular CLI version 1.3.2 and is based on the Angular Tour of Heroes tutorial

Please note, this guide is not intended to be an overview of how to create an Angular application. First point of call is to start here

Client Side Routing

In order to get client side routing to work for an application on Azure App Service, we’ll need to create a web.config file to issue a url redirect. Simply add the below snippet to a web.config file to the root of the project.

For why this is required or if you are hosting this application not on an IIS based web server, see here

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Setup PhantomJS Configuration

This section involves the setup and configuration of PhantomJS which will be used for headless website testing. Credit to Microsoft’s article on setting this up.

Polyfills

In order to get PhantomJS to work with the tests, some polyfills (or JavaScript fallbacks) are required to be enabled. Edit the polyfills.ts file in the src directory and uncomment the following polyfills.

 import 'core-js/es6/symbol';
 import 'core-js/es6/object';
 import 'core-js/es6/string';
 import 'core-js/es6/array';

At this juncture, you will have successfully setup your Angular project with support for running tests using PhantomJS. If you wish to see this in action, you can do so running the following command

npm test -- --watch=false --single-run=true --reporters=junit,progress --browsers=PhantomJS

The above command will execute your tests once using PhantomJS and output any results to the report files.


Create VSTS Build Definition

This section involves creating the Build definition in VSTS in order to build the application ready for deployment. Credit to Seth Reid’s article for pointing me to the majority of the grunt work.

Create VSTS Release Definition

The final section is to create a release definition that defines how we release this application to Azure. The assumption here is that you will have defined an Azure App Service instance already.