Angular on Azure — Part I
Tags: azure, angular, devops, azurepipelines
Published: 2021-06-17 17:23:50

Pankaj Parker | ng-conf | May 2019

Using Azure Pipeline

Developers test software in a local environment using servers such as LAMP, WAMP, node, IIS, and Apache. Local deployment is good in terms of fast development and debugging, but we can’t ship our physical machines to the client in order to access to the application 😂. We have to deploy an application to a web server/cloud in order to make it accessible to the end user on their preferred platform (mobile, desktop, etc).

---

A variety of cloud providers exist in the current market, the most popular being [Microsoft Azure](https://azure.microsoft.com/en-us/), [Google Cloud Platform](https://cloud.google.com/), [AWS](https://aws.amazon.com/). These providers offer an unbeatable combination of ease, speed, and automation, so if you have not deployed using such a platform, then this is the article for you! This article focuses on deploying an Angular app to Azure.

### What is Deployment?

> ### The action of bringing resources into effective action

In web development, deployment is concerned with making our static/dynamic resources available over the wire so the end user can access them in a desired device, using a browser.

### What is hosting?

> ### Web hosting is a service that allows organizations and individuals to post a website or web page onto the Internet.

The deployment process is incomplete without hosting.

###Deployment Philosophy
Deployment exposes your web application using a philosophy that has been followed for year. The diagram below outlines typical deployment steps that could be applied to any type of software.

![Image for post](https://miro.medium.com/max/700/1*Mc5IA53FtAGiYqf9_IaIog.png)
###### Deployment Philosophy

---

### What is Azure?
Azure is a cloud platform service which provides cloud services, including those for compute, analytics, storage, serverless, AI and ML, IoT, containers, DevOps, mobile, and networking. It is widely considered as both a PaaS and IaaS product. This article covers the development, deployment, and DevOps aspects of the platform.

> _If you are new to Azure, a [free subscription](https://azure.microsoft.com/en-in/free/search/) is available for those wishing to try the platform without any commitment._

### Azure App Service
The Azure App Service is part of the PaaS section of the platform. It easily builds and deploys a highly available web app to the cloud. Multiple features are available right out of the box, as illustrated below.

![Image for post](https://miro.medium.com/max/1000/1*4RjvgjzIRDm6nsZEK9zbrw.png)
###### Azure app service

The important steps are numbered in the above screenshot. If a resource group is not created, then do so in step 3. Also, if you do not have a service plan, create one at this time. Make sure you select ‘ASP .NET 4.7’ in the ‘Runtime Stack’ option in step 5. For more information, follow the guide for the detailed creation of [Azure Service Plan](https://docs.microsoft.com/en-us/azure/app-service/app-service-plan-manage) and [Azure App Service](https://docs.microsoft.com/en-us/azure/app-service/environment/app-service-web-how-to-create-a-web-app-in-an-ase).

Once you’re done with the fill in details, click on “Review and create” button and then on the next screen press “Create” button. To see the newly created resource you can click on “All Resources” option in the sidebar.

![Image for post](https://miro.medium.com/max/1000/1*6k--VnP66c0sS43a2jJkxA.png)
###### Newly created resource group added

The following url can be loaded to check if the recently deployed application is available in the cloud, https://<app-name>.azurewebsites.net/

In my case I used app name as “angular-deployment” so URL would become https://angular-deployment.azurewebsites.net/

![Image for post](https://miro.medium.com/max/1000/1*24WH-7vUnIzc4Tyq_f4Exg.png)
###### The deployed application running in the cloud

But, before moving forward, we should minimize the final bundle size of the application. No worries; that process is discussed in a later section.

---

### Make angular application production ready
Angular CLI tooling is incredible; simply executing `ng serve` compiles angular code to Javascript and generates bundle files. For a simple hello-world app, however, the total file size is far short of desirable.

Angular currently offers two compilation modes

1. Just in Time (JIT) Mode
2. Ahead of Time (AOT) Mode

In short, JIT ships the Angular compiler over the wire and component templates are compiled inside the browser. AOT mode precompiles all templates and makes the resulting JS available for further optimization before shipping the bundled application over the wire. Smaller application sizes and quicker response makes for better UX!

For those new to the Angular CLI, AOT mode is enabled with the command

`ng build --prod`

This command compiles all templates, then applies tree-shaking, optimization, minification, and bundling to create a final, highly-optimized package. All distribution files are automatically placed in the `dist` folder of your project, which can be directly hosted to a cloud provider.

### Setup Deployment
In this article, [Azure DevOps](https://azure.microsoft.com/en-us/services/devops/?nav=min) (formerly known as VSTS) is used to deploy an application to the cloud.

![Image for post](https://miro.medium.com/max/1000/1*y1T09C1j2toCE5ip114lFQ.png)
###### DevOps create project

If you have not created an organization, then do so before clicking the ‘Create Project’ button, as shown in the above diagram. This displays the ‘Create New Project’ dialog.

![Image for post](https://miro.medium.com/max/638/1*7t2ioKH88sIk3QpMWhfM5Q.png)
###### Create a project inside the Organization

In “Create new project” screen, fill project name, description and select visibility (I selected private). Also, version control was set to ‘Git’, and ‘Work item process’ defaulted to ‘Agile.’ Then, click the ‘Create’ button.

The dashboard page is displayed after a project is created. Several actions may be performed from the dashboard sidebar.

---

### Azure DevOps Gist
1. Boards — To track down tasks and progress of a project
2. Repos — Code repositories involved in the development.
3. Pipelines — CI / CD pipeline
4. Test Plans — Provides all tools to successfully test your applications
5. Artifacts — Provides a secure, highly performant store and easy feed

The most important feature in the above list for purposes of this article is the Azure Pipelines setup.

### Create A Brand New Pipeline

![Image for post](https://miro.medium.com/max/1000/1*KId1tf-5Wy0iwSk0ilqU_w.png)
###### Pipeline page

Select the ‘Pipelines’ option from the left sidebar, which displays the ‘New Pipeline’ button in the middle of the screen. The following dialog is displayed after clicking the ‘New Pipeline’ button.

![Image for post](https://miro.medium.com/max/1000/1*hMNFxFASJKGKlUYlMFO-8w.png)
###### Pipeline creation page

Pipelines are created with `yaml` files. A new `yaml` file may be created with a visual tool or by using the ‘Use the classic editor’ link at the bottom of the dialog.

![Image for post](https://miro.medium.com/max/1000/1*-T2OdpYwJtzyqKb3zLePdw.png)
###### Repository selection

The next step is selecting a repository resource, which can be a new repository (above) or using an existing repo as shown below. I’m using my existing Github repo, so I selected ‘Github’ at this stage. To select a Github repo, click on the ‘…’ Button to browse repositories.

![Image for post](https://miro.medium.com/max/1000/1*V_fH_YHyaH5VoRF1o4qb2Q.png)
###### Select a repository

Select the desired repository for deployment. In this example, I selected the ‘weather-cast’ repo. Then, click the ‘Select’ button.

![Image for post](https://miro.medium.com/max/677/1*S_Z7wsW7cwIL3XtdeeXNEw.png)
###### Select a source

At this point, you are very close to creating a brand new pipeline! By default, the ‘master’ branch is selected. Click on the ‘Continue’ button.

![Image for post](https://miro.medium.com/max/1000/1*lC7Gi8Aerr5p1W0R_mZe6A.png)
###### Select job template page

Now, you’ve made it to the final page of pipeline creation! Next, we create a ‘Job’, or the steps involved in the actual deployment. For now, just select ‘Empty Job’ to create a blank Job with no content. Don’t worry, we will add steps for it in the next section.

![Image for post](https://miro.medium.com/max/1000/1*aAJBm0zeHJyVo5055Qn4Fg.png)
###### Empty Job page

After the pipeline is created, you will see a screen where an Agent pool is assigned to run a job when any tasks are to be deployed. For this tutorial, we are going to setup deployment tasks under the ‘Agent Job 1.’ Simply click on the ‘+’button in the dialog.

---

### Setting Up Pipeline Step
Cool! We’ve finally made it to the stage where we can add tasks for the deployment job! Refer to the following screen shot.

#### 1. Install node

![Image for post](https://miro.medium.com/max/1000/1*Gqt4vq4HVydEA5kAHVVBpg.png)
###### Install node on vm

After clicking the ‘+’ icon beside ‘Agent Job 1,’ you can search by ‘node’ in the list (item 1 in the screen shot) then select ‘Node Tool Installer.’ When that dialog displays (item 2), click the ‘Add’ button (item 3 in the above screenshot).

![Image for post](https://miro.medium.com/max/1000/1*qsKZB6m3HaLfn1oWd5dzCg.png)
###### Making node available on machine

This displays the first task in the ‘Agent job 1’ list. Next, fill in the details for this task. Enter display and version spec, as shown above. This configures NodeJS on our VM.

#### 2. Install Angular CLI

![Image for post](https://miro.medium.com/max/1000/1*-JFiJqXcNRsTTR-UtDh5Cg.png)
###### Install Angular CLI to machine

As before, search for ‘npm’ in the task list and then click the ‘Add’ button. Fill in the details as shown above to install the Angular CLI as the next step in the task list.

#### 3. npm install

![Image for post](https://miro.medium.com/max/1000/1*-5Zd-TNG559E72FDjdoH7g.png)
###### npm install on package folder

Continue the same process as above to create a task that installs all npm dependencies.

#### 4. Create Prod Build

![Image for post](https://miro.medium.com/max/1000/1*ah8jl4lECBwjeqSYoltK8Q.png)
###### Create prod build

Again add `npm` ask and fill in the details shown above. This time select command as in “custom”, and “command and arguments” would be `run build`. Basically, it calls `ng build --prod` command written as scripts in . This task helps to create a production ready angular package.

This is the task that creates the production-ready Angular package. Continue as before using the details shown in the above screenshot. ‘Command’ is ‘custom’ and the ‘Command and arguments’ input is ‘ng build — prod’. This causes the `ng build --prod` command to be written in the ‘scripts’ section of the `[package.json](https://github.com/pankajparkar/weather-cast/blob/master/package.json#L8)` file.

#### 5. Host dist folder
Next, search for ‘Azure App Service Deploy’ and add it to the task list. Fill in the details as shown below. This task hosts and deploys code to the server.

![Image for post](https://miro.medium.com/max/1000/1*so-VglqVEBzgwfQm80OigQ.png)
###### Azure app service deploy

After you have finished entering details, click on the ‘Save and queue’ button. This saves and subsequently runs the pipeline. You will see a message with a hyperlink containing a build number. Or, you may navigate to ‘Pipelines > Builds’ to see the following screen.

![Image for post](https://miro.medium.com/max/1000/1*VODLr5mpU-tlllwaLyxBzQ.png)
###### After the build is finished

After the job is finished, we can check it as shown below.

![Image for post](https://miro.medium.com/max/700/1*ACC1CI7PNdNJVVS4muxwyg.png)
###### Final Azure Pipeline

![Image for post](https://miro.medium.com/max/483/1*Kot5V0Amwt1C1mgmVYRTgg.png)
###### Final pipeline tasks

### Conclusion
This article outlined the steps to deploy an Angular application to Azure directly from Github or another repository. An Azure pipeline is a powerful feature to setup and visualize a deployment job with minimal effort.

---

If you like this article press 👏 clap button 50 times or as many times you want. Feel free to ask a question if you have any. Thanks a lot for reading!

---
## ng-conf: Join us for the Reliable Web Summit

Come learn from community members and leaders the best ways to build reliable web applications, write quality code, choose scalable architectures, and create effective automated tests. Powered by ng-conf, join us for the Reliable Web Summit this August 26th & 27th, 2021.
https://reliablewebsummit.com/


Soon, I will release part II of this article which covers CI and CD with Azure
Pipeline. Stay tuned!

For more Angular goodness, be sure to check out the latest episode of [The Angular Show podcast]