Friday, March 17, 2017

Continuous Integration and Continuous Deployment of Apps

Continuous Integration and Continuous Deployment of Apps

Overview

Continuous Integration (CI) of apps, where every check-in initiates a build and test, was available in the Team Foundation Server (TFS) 2015 RTM release. Continuous Delivery (CD) of applications, to test and promote the applications through different stages, like Test, UAT, Staging, and Production, is available in the TFS 2015 Update 2 RC2 release. The CI/CD pipelines, enable the agile development methodology, and provides for a faster cadence of delivery of value to the end-customers. 

Objectives

·       Create a CI pipeline for building the app and run unit tests on it.
·       Create a CD pipeline for deploying the app to a stage.
·       Trigger the CI/CD pipeline by committing and syncing app changes to the TFS Git repo.
·       Confirm that the app and its database has been deployed properly, and view the test results in TFS.

Prerequisites

·       Windows Server 2012 R2.
·       Visual Studio 2015.
·       Team Foundation Server 2015 Update 2 Release Candidate 2.
·       Web Application with a Database project.

Intended Audience

This Quick Start Challenge is intended for developer’s familiar with C# and Visual Studio. It does not require any prior knowledge of TFS and CI/CD.

Step 1: Open TFS Web Portal


Open the TFS Web portal and navigate to the CI_CD_Lab team project, by entering the following URL in Internet Explorer.
TFS Web Portal URL: http://cicdlab:8080/tfs/DefaultCollection/CI_CD_Lab/

Step 2: Create a CI Pipeline

Click on the Build link at the top to open the Build hub.
In the Build Hub, click on the Create New Build Definition to create a CI pipeline.
 
 Select the Deployment template and then select Azure Website template, and click on the Next button.
In the Settings pane, keep the defaults for the Repository and the Default Branch as the app code is already checked in. Check the check-box titled Continuous integration (build whenever this branch is updated), and click on the button Create.
 
A new Build Definition will be created. Click on the Delete icon    next to the Azure Web App Deployment task to delete the task from the Build Definition.
Click on the Add Build Step link   , select Utility tasks in the Add Tasksdialog, and then click on  the Add button in front of the Copy and Publish Build Artifactstask. Close the Add Tasks dialog by clickon on the Close button. The Copy and Publish Build Artifacts task will appear in the End of the Build Definition.
Select the Copy and Publish Build Artifacts task   and enter the Parameter values as shown below. Leave others at their default values:
·       Copy Root: Enter $(build.sourcesDirectory).
·       Contents: Note that each of the values below are on a separate line. If they are not on separate lines the task will not work properly.
**\*.dacpac
**\HelloWorld\Deployment
·       Artifact Name: Enter HelloWorld.
·       Artifact Type: Select Server from the dropdown.
Click on the Save button   , and save the build definition as HelloWorld.CI.
The CI Pipeline is now ready. Queue a build by clicking on the  button. Keep all values to their defaults and click on the OK button. The build’s Console Outputwindow will show the progress of the build, and  if everything is OK, all tasks will be green.

Step 3: Create a Linked CD Pipeline

Click on the Release Hub to create a CD pipeline.
Click on the Create Release Definition icon on the top right to create a CD pipeline.
In the Deployment Templates pop-up dialog, select Empty and click on the OK button to create a new release definition.
Click on the Link to a Build Definition link to link the build artifacts to the release definition.
 
Retain all Defaults and click on the Link button to link the artifacts from the HelloWorld.CI build.
Click on the ellipses (...) next to the Default Environment to open the shortcut menu and select Configure Variables.
In the Configure – ‘Default Environment’ Environment dialog, enter the following values for the variables you will use in the release definition tasks:
  • DacPacLocation: The location of the DACPAC file on the remote machine to deploy to SQL Server. Enter - C:\HelloWorld\HelloWorld\HelloWorldDB\bin\Release\HelloWorldDb.dacpac.
  • DbName: Name of the SQL Server Database. Enter - HelloWorld.
  • DbServerName: The name of the SQL Server. Enter - .\SQLEXPRESS. Do not miss the “.\”.
  • MachineName: The name of the remote machine, where the app will be deployed. As the app will be deployed to the local machine itself, enter – localhost.
  • Password: The password of the remote machine. As this is local machine, enter its password – Password~1. Confirm that the password has been correctly entered and then click on the lock icon   to securely store the password.
  • Username: The user on whose behalf the machine will be accessed and the task will be run. Enter - cicdlab\Administrator.
  • WebDeployPackagePath: The path to the Web Deploy package for deploying the Web App. Enter - C:\HelloWorld\drop\HelloWorld.zip.
In the Configure – ‘Default Environment’ Environment, click on the Deployment Conditions. Select the radio button for the option After Release Creation, and click on the OK button to close the dialog.
In the release definition, click on the link Add tasks    to add tasks to the definition.
In the Add Tasks window, in the Deploy section, click on the Add button in front of the task Windows Machine File Copy. Then click the Add button in from of the task PowerShell on Target Machines twice. Two PowerShell on Target Machines tasks are required, one to deploy the SQL Database, and another to deploy the Web App.
Select the Windows Machine File Copy task and fill in the parameters as described below, and leave others at their default values:
  • Source: Enter $(System.DefaultWorkingDirectory)\HelloWorld.CI.
  • Machine: Enter the machine name - cicdlab.
  • AdminLogin: The administrator’s login. Enter the variable that was defined earlier - $(Username).
  • AdminPassword: The administrator’s password. Enter the variable that was defined earlier - $(Password).
  • Destination Folder: The location on the machine where the builds artifacts will be copied to. Enter - c:\HelloWorld.  
Select the PowerShell on Target Machines task that is just below the Windows Machine File Copy task, and fill in the parameters as described below, and leave others at their default values:
  • Machines: Enter the machine name - cicdlab.
  • AdminLogin: The administrator’s login. Enter the variable that was defined earlier - $(Username).
  • AdminPassword: The administrator’s password. Enter the variable that was defined earlier - $(Password).
  • Protocol: Select HTTPS.
Enter the following values in the deployment section of the task
  • PowerShell Script: Enter - C:\HelloWorld\HelloWorld\HelloWorld\Deployment\DeploySQLDB.ps1.
  • Script Arguments: Enter - $(DacPacLocation) $(DbServerName) $(DbName) 
Select the PowerShell on Target Machines task that is the last task in the definition, and fill in the parameters as described below, and leave others at their default values:
  • Machines: Enter the machine name - cicdlab.
  • AdminLogin: The administrator’s login. Enter the variable that was defined earlier - $(Username).
  • AdminPassword: The administrator’s password. Enter the variable that was defined earlier - $(Password).
  • Protocol: Select HTTPS.
Enter the following values in the deployment section of the task
  • PowerShell Script: Enter - C:\HelloWorld\HelloWorld\HelloWorld\Deployment\DeployWebApp.ps1.
  • Script Arguments: Enter - $(WebDeployPackagePath) $(DbServerName) $(DbName)
Click on the Triggers tab in the release definition, and in the Release Trigger section, select the Continuous Deployment option, and then select HelloWorld.CI in the dropdown for Set Trigger on Artifact Source.
Name the Release Definition as HelloWorld.CD and save it by clicking on the Save button.
Create a Release by clicking on the Create Release button.
In the Create New Release for HelloWorld dialog, select the HelloWorld.CI(Build) from the dropdown, and click on the Create button to create the release.
 
To navigate to the release, click on the link in the notification bar in the release definition.
In the HelloWorld.CD/Release window, click on logs to see the progress and status of the deployment.
Once the app has deployed successfully, navigate to the URL - http://cicdlab/HelloWorld_deploy/ - to confirm that the app is working. Register as a new user on the website to confirm that the database deployment is also working fine.

Step 4: Building and Deploying every Commit

Open Visual Studio, and click on the Team Explorer. Open the HelloWorld app by double-clicking on it.
 
Open Solution Explorer, and navigate to the _Layout.cshtml file, by expanding the HelloWorld project, and then the Views folder, and then the Shared folder. Double-click the _Layout.cshtml file to open it, and browse to the end of the file.
 
Replace the footer section with the snippet given below and press Ctrl+S to save the file:
        <footer>
            <p>© @DateTime.Now.Year - My ASP.NET Application Deployed by CI/CD</p>
        </footer>
Check-in the change to TFS. Click on the Team Explorer, and then Changes, enter a Commit Message, and then select Commit and Sync from the dropdown of Commit button.
After the change is synced a new build will be automatically started, and on its successful completion the Web app changes will be deploy. Confirm that the change appears on the website by navigating to http://cicdlab/HelloWorld_deploy/.

Summary

Congratulations on completing this Quick Start Challenge! In this lab, you’ve learned how you can setup a continuous integration and continuous deployment pipeline in TFS by using templates and by entering a few parameters for tasks.

Additional Resources

If you are interested in learning more about Ci/CD, then you can refer to the following resources:
Continuous Integration: https://msdn.microsoft.com/en-us/library/vs/alm/build/overview
Continuous Integration: https://msdn.microsoft.com/en-us/library/vs/alm/release/overview
Tasks on GitHub: https://github.com/Microsoft/vso-agent-tasks

1 comment:

Vcom Infotech Company said...

Very informative post sharing for handling the continuous deployment services. Thank you very much for the sharing. Best IT company in coimbatore | Best web site developer