Search Sreeni's Insights

Filter by content type
cloud September 13, 2016 7 min read

How to automate releases with VSTS Release Management?

VSTS Release Management explained through release definitions, environments, approvals, variables, deployment slots, smoke tests and automated ASP.NET deployments for practical guidance.

Share:

How to automate releases with VSTS Release Management?

Most of the teams have automated the build by now. Every check-in triggers a build, runs the unit tests and produces a package. But the journey of that package to production is still manual in many places. Someone copies the files to the QA server, someone else updates the config, an email goes to the manager for approval and finally somebody deploys to production on Friday evening with a checklist in a Word document. Every manual step is a chance for a mistake, and when the release fails, nobody knows exactly what was deployed and by whom.

Last year, we discussed how to set up continuous integration with Visual Studio Online (now called as Visual Studio Team Services, VSTS). In the previous article, we discussed how to create your first Azure Function. In this article, we are going to see what is VSTS Release Management, the key concepts and the step by step instructions to automate the release of an ASP.NET application to Azure App Service through Dev, QA and Production environments with approvals.

TL;DR Summary

  • Release Management takes the artifact produced by the build and deploys it through a chain of environments.
  • Build once, deploy many times, the same artifact goes to all environments.
  • A release definition has artifacts, environments, tasks, variables, approvals and triggers.
  • Environment specific values (connection strings, URLs) are variables, not separate builds.
  • Use pre-deployment approvals for QA and Production, and automatic deployment for Dev.
  • Every release is recorded, what was deployed, where, when and who approved it.

Challenges

Manual and inconsistent deployments

When each environment is deployed by a different person with slightly different steps, the environments drift apart. A release which worked in QA fails in production because a config value was missed.

Rebuilding for each environment

Some teams create a separate build for each environment with different config transforms. It means the code tested in QA is not exactly the code deployed to production.

No traceability

When a production issue happens, the first question is “what changed?”. Without a release history, we search emails and chat messages to find out which version is running.

What is Release Management?

VSTS Release Management is a service in Visual Studio Team Services which automates the deployment of build artifacts through a series of environments, with configurable tasks, approvals and variables, and keeps a full audit history of every release.

In simple terms, if the build is the factory which produces the product, Release Management is the delivery truck which takes the same product to every shop, and gets a signature at each stop.

The key concepts are,

  • Release definition - the template for releases, it defines the artifacts, environments and the steps.
  • Artifact - the output of a build (ex: the web deploy package of our application).
  • Environment - a logical deployment target, Dev, QA, Production. Each environment has its own tasks, variables and approvals.
  • Task - a step in the deployment, ex: “Azure App Service Deploy”, “Run PowerShell”, “Visual Studio Test”.
  • Approval - a person or group who must approve before (pre-deployment) or after (post-deployment) an environment is deployed.
  • Release - an instance of the release definition for a specific artifact version.

The following diagram depicts the release pipeline we are going to create.

VSTS release pipeline
VSTS release pipeline

The flow highlighted in purple is the path of one artifact from the build to production. The same artifact is deployed automatically to Dev, and with approvals to QA and Production.

Why Release Management is important?

In order to deploy the same artifact consistently to all environments, to remove the manual steps which cause failed releases, and to know exactly what is running in each environment, release automation is mandatory. It also makes small and frequent releases possible, because the cost of a release is a click and an approval, not an evening of manual work.

Steps to Implement:

Follow the following instructions to create a release definition for an ASP.NET application hosted in Azure App Service.

Prepare the build

  • Make sure the build definition produces a web deploy package and publishes it as an artifact. In the “Visual Studio Build” task, add the MSBuild arguments.

    /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"
  • Add a “Publish Build Artifacts” task with the path $(build.artifactstagingdirectory) and artifact name drop.

  • Queue the build and check the drop artifact contains the .zip package.

Connect VSTS to Azure

  • In the team project, open Settings → Services → “New Service Endpoint” → “Azure Resource Manager”.

  • Select the subscription and give the connection a name. VSTS creates a service principal with access to the subscription.

    Note: For production subscriptions, it is highly recommended to create the service principal manually and give it access only to the resource groups it needs to deploy, following the least privilege principle.

Create the release definition

  • Open the “Release” hub, click ”+” → “Create release definition” and select the “Azure App Service Deployment” template.

  • Link the build definition as the artifact source.

  • Rename the first environment to “Dev”.

  • In the “Azure App Service Deploy” task, select the Azure service endpoint, the App Service name and the package path.

    $(System.DefaultWorkingDirectory)/<build definition name>/drop/*.zip
  • Add environment variables for the values which change per environment. For example, ConnectionStrings.FundDb and ReportingApiUrl. Mark the passwords as secret (the lock icon), they are encrypted and hidden in the logs.

  • Use these variables in the task through the “File transforms & variable substitution” option, or set them as App Service application settings with an “Azure PowerShell” task. In our case, we keep the secrets in Azure Key Vault and only the vault URL in the variables.

Add QA and Production

  • Click “Add environment” → “Clone environment” for QA and then for Production, and change the App Service name and variables.
  • For QA, set “Pre-deployment approval” to the QA lead group.
  • For Production, set “Pre-deployment approval” to the release manager and the product owner.
  • In the environment “Deployment conditions”, set QA to deploy automatically after a successful deployment to Dev, and Production after a successful deployment to QA.

Set the triggers

  • In the “Triggers” tab, enable “Continuous Deployment” and select the build artifact, so every successful build of master creates a new release.
  • Save the release definition.

Pulse check

  • Just to pulse check, queue a new build. A release should be created automatically and deployed to Dev.
  • Open the release, check the logs of each task and the summary, it shows the work items and commits included in this release.
  • Approve the QA deployment from the release page or the email notification, and verify the application in QA.

Deployment slots for zero downtime

For production, we deploy to a “staging” deployment slot first and swap it with production after a smoke test. The “Azure App Service Deploy” task has a “Deploy to slot” option, and the “Azure App Service Manage” task can do the swap. We discussed deployment slots in detail last year, refer the How to configure deployment slots in Azure App Service? article.

Must Do and Don’ts

  • Must Do: Build once and promote the same artifact through all environments.
  • Must Do: Keep the environment specific values in variables, mark secrets as secret.
  • Must Do: Use approvals for QA and Production.
  • Should: Run automated smoke tests after each deployment, a “Visual Studio Test” or PowerShell task which calls the health endpoint.
  • Should: Keep the release definition the same for all environments, only variables should differ.
  • Don’t: deploy to production from a developer machine, even “just this once”.
  • Don’t: use one service principal with owner access to all subscriptions.

Wrap up

In this article, we have looked at the challenges of manual deployments, what is VSTS Release Management, the key concepts and the step by step instructions to create a release pipeline from Dev to Production with approvals. I really appreciate that you took the time to read this article. In the next article, I will walk you through how to set up Azure Application Insights for an ASP.NET application.

Related Posts