Showing posts with label Angular 2. Show all posts
Showing posts with label Angular 2. Show all posts

Friday, March 23, 2018

Continuous deployment and delivery of Angular App with VSTS & Azure

Background

This tutorial describes processes and best practices for an end-to-end continuous deployment and delivery of an Angular Application.

Assumptions:

  •       Node.js, npm and angular-cli are installed on dev computer
  •          Angular is installed on your computer: npm install -g @angular/cli
  •          GIT is installed on the dev computer
  •          Developer has access to an account on VSTS (Visual Studio Team Services) at http://www.visualstudio.com
  •          Developer has an account on Azure (http://portal.azure.com)

Creating a simple Angular CLI app on dev computer:

Create an angular app using angular-cli:
ng new angular-hello-world
cd  angular-hello-world
ng serve
Point your browser to http://localhost:4200 and you should see the following:

Web.config file

In order for Angular’s routing to work smoothly on Azure, it is necessary to have a web.config file in the root folder of the application. Therefore, create a web.config file in the src directory and add to it the following code:

<?xml version="1.0"?>
<configuration>
   <system.webServer>
      <rewrite>
         <rules>
         <rule name="Angular Routes" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
               <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
               <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
            </conditions>
            <action type="Rewrite" url="/" />
         </rule>
         </rules>
      </rewrite>
   </system.webServer>
</configuration>

To enable the web.config file to be packaged with production code, edit the angular-cli.json file and add ‘web.config’ to assets, as follows:
. . . . .
"assets": [
  "assets",
  "favicon.ico",
  "web.config"
],
. . . . .

Build app

Build the app using the following command:

ng build --prod

This produces the production version of the application into the dist folder. To test the trans-piled version of the app, point your browser to http://localhost:4200/dist. You should see the same app as before, this time it is being served from production code.


 VSTS

VSTS is Microsoft’s DevOps platform. Login into VSTS and create a new project. In my case I created a new project named angular-hello-world.



Once the project is created in VSTS, it is time for us to push our code to VSTS using GIT. Copy the address of the GIT repo so that we can use it to sync our code.


Back at your computer, run the following commands from your command prompt to push the code into your projects VSTS git repository:

git remote add origin {your-git-url-here}
git push -u origin master
In my case, this would be:

git remote add origin https://medhat.visualstudio.com/DefaultCollection/_git/angular-hello-world
git push -u origin master
Click on Code >> angular-hello-world in VSTS to verify that your code is indeed in VSTS:


The code will look like this:


 Building the code in VSTS

The same steps that we carried out to build our app on the development computer will be translated into tasks in VSTS. To build our app, choose: Build & Release >> Builds:


Click on the “+ New definition” button:



On the “Select a template” page, scroll to the bottom of the list and highlight item “Empty”, then click Apply:


Select “Hosted” in the “Default agent queue” drop-down-list:


Next, click on “+ Add Task” on the left side:


In the filter, enter npm. Highlight the npm task then click on Add:



This task will install all the npm dependencies:


The above task runs the command “npm install”. You do not need to make any changes to this task as it does exactly what we want it to do.

Add another npm task to run “npm run build” command, which is essentially a script in our package.json file:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
The second npm task should look like this:


Display name: build
Command: custom
Command and arguments: run build --prod


 Create release package

Click on the ‘Add Task’ button, enter “archive” in the search box, hover over the Archive Files task and click Add. Change the “Archive files” task as follows:


Display name: create release package
Root folder (or file) to archive: dist
Prefix root folder name to archive paths: UNCHECK


 Publish release package

Next, we publish the zip we created in the last step. Click on the Add Task button, enter “publish build” in the search box, hover over the Publish Build Artifacts task and click Add:




Display name: Publish release package
Path to Publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
Artifact Name: drop
Artifact Type: Server
That’s it for all our steps. Let’s run the build! Click on Save and Queue >> Save and Queue in the top right hand corner.


On the next dialog, click on “Queue”

Now the build will run for us. While that is happening let us turn on continuous integration so that when we make changes in our repository it will trigger a new build.

Click on the build number link in the in the top left-side corner. This will allow you to see progress of the build. When the build is completed without any errors, you will see a green “Build Succeeded” message as shown below:


Artifacts

You can see the package that was created by selecting the Artifacts tab:



Download the drop folder to see what was created inside that directory.

Continuous Integration

We will need to setup continuous integration so that whenever new code is committed then the build process is kicked off. Click on the build as shown below:



Click “Edit” on the top right-side:


On the next page, click on “Triggers”:



Enable continuous integration by enabling the switch:



Click: Save & Queue >> Save:



On the next dialog, add a comment then click Save:



Creating Release

Log into Azure and create a web app.



Create a release definition by clicking on Build & Release >> Releases:


Next, click on the blue “+ New definition” button. Under ‘Select a template select Azure App Service Deployment then click Next >:



On the next dialog, make sure that you have the right build name and enable “Continuous deployment”:



Click on “Create”:

Choose your Azure subscription then click on the blue Authorize button:


After you log into your Microsoft account, the border around the “Azure subscription” field will change from red gray. Choose the correct azure web app in the “App Service name” field. Your release task will look like this:



Click Save:



You can enter a comment in the following dialog then click on OK.



Click on Release >> Create Release:


This displays the following type of dialog:



Choose the correct build that will be used for the release. In the above example it is 40.

Click on Create.



Click on the link beside the definition file to see progress:


If all is well, the blue “IN PROGRESS” bar will change to a green “SUCCESS” bar.

Sunday, November 20, 2016

Continuous deployment of angular-cli Angular 2 app to Azure thru GitHub

In this post, I will show you how to deploy an Angular 2 application on GitHub to Azure. The application will first be pushed to GitHub then we will configure Azure to automatically sync changes from GitHub. This may not be the quickest way to do it, but it does work.

The following are prerequisites:
  • node.js is installed
  • angular-cli is installed
  • git is installed on your computer
  • you have a GitHub account
  • you have an Azure subscription

Creating a sample Angular 2 app

The first step is to create an application named ng2_to_azure. Type the following within a working directory in a terminal windows:

ng new ng2_to_azure

Once the above command completes, you will find the following directory and file structure has been created in a new directory named ng2_to_azure:
image

Change to the newly created directory with:

cd ng2_to_azure

Start the Angular 2 app with this command:

ng serve

If you point your browser to http://localhost:4200. You should see the following web page:

image

Creating production version of Angular 2 app

If you open the .gitignore text file in an editor, you will notice that the /dist directory is excluded. Comment out that line by adding a # so that it looks like this:

image

Save the .gitignore file and exit your editor. We will next produce the production version of our app. This is done by executing the following build command while in the root ng2_to_azure directory:

ng build --prod --aot

The above command transpiles all the TypeScript files JavaScript files and puts the results in the /dist folder. The aot switch builds using ahead-of-time compilation. Have a peek at the contents of the /dist folder:

image

Notice that the main page is index.html. Assuming that your web server is still running, point your browser to http://localhost:4200/dist. The production version of your application will display in the browser:

image

Adding GitHub source control to your application

Go to http://github.com and login into your account.

image

On the right-side, beside "Your repositories", click on the green "New Repository" button.

image

Enter ng2_to_azure for repository name, then click on the green "Create repository" button.

image

We will follow some of the instructions highlighted above. Go to a command-line in the ng2_to_azure directory on your computer. and type in the following commands:

git add .
git commit -m "first commit"
git remote add origin
https://github.com/{github user}/ng2_to_azure.git
git push -u origin master


Note: Make sure you enter your GitHub handle instead of {github user} in the URL above.
Once you have successfully completed the above, you can go back to GitHub in your browser and view all the files in your ng2_to_azure repository:

image

Note that the /dist folder is also contained in the repository.

Continuous deployment on azure

Go to http://portal.azure.com and login into you Azure account.

image

Click on the big + (New) on the top left-side.

image

Click Web App on the next blade.

image
  • Choose an "App name" that is sufficiently unique when combined with domain azurewebsites.net
  • Choose your subscription
  • For "Resource Group" either create a new or use an existing one
  • While configuring "App Service plan/Location", you get to choose which data center you want.
Finally, click on the blue Create button at the bottom of the blade. Once your web app is created, it will appear in the list of your "App Services".

image

Click on the web app you just created.

image

Enter "deploy" in the filter field at the top then select "Deployment Options".

image

On the next blade, click on "Choose Source".

image

Click on GitHub. If this is the first time you connect to GitHub from Azure, you will be asked for your credentials. Once your GitHub credentials are known to Azure then you will see a blade that looks like this:

image

Click on "Choose project" to select the appropriate GitHub repository that you wish to connect Azure with.

image

Click on the Angular 2 repository you previously setup on GitHub. Back in the "Deployment source" blade, click on the blue OK button at the bottom of the blade. If you click on "Deployment options" again, you will determine that Azure is building your app with the following message:

image

Finally, when the build process is complete, you should see the following:

image

Now let us point to our application in the browser. In my case, I tried to access http://ng2azure.azurewebsites.net and this is the response I got:

image

One can guess that the web server found no default page in the root of the web application. To prove my point, we can find out what files we have in the root of the web site. Go back to the Azure portal in your browser and enter the word “Tools” in your filter field:

image

Click on Console. This will take you into the Azure terminal window. Type "dir" to see what is in the root folder.

image

It is obvious that there is no welcome page in the root of the web application. What we need to do is educate Azure that the /dist directory is indeed the root of our web application.

Enter the word "application" into the filter field.

image

Click on Application settings. Under "App settings", add the following

key = Project
value = ./dist

image

Do not forget to click on Save at the top.

image

To get the new configuration to take effect, we must trigger a new deployment. Go back to "Deployment options" and click on Sync so a new deployment is initiated.

image

Once the new deployment is completed, click on your fully qualified web address on azure and you should see that your application is indeed working.

image

If you have a better way of deploying an Angular 2 application to Azure, I would appreciate your letting me know through a comment on this post.