Creating Build and Release Pipeline in Azure DevOps

Ali Karaca
inventiv
Published in
7 min readOct 13, 2021

Introduction

Azure Pipelines automatically builds and tests code projects to make them available to others. Azure Pipelines combine continuous integration (CI) and continuous delivery (CD) to test and build your code and ship it to any target. You can accomplish this by defining a pipeline. You build pipelines by using a GUI editor in Classic Editor or you build pipelines as code with YAML. You define a build pipeline to build and test your code, and then to publish artifacts. You also define a release to consume and deploy those artifacts to deployment targets.

With the help of this guide, one will be able to set up build and release pipelines with Classic Editor on Azure DevOps. The image below shows the overall workflow.

A self-hosted windows agent is used in this article. Agent and Deployment servers are different machines. These machines are on the same network and the agent can control the server remotely. If self-hosted windows agent will be used, Azure Pipelines Agent windows service user credentials must be provided.

Steps of Creating Build Pipeline

Several version control systems can be used such as Git or Subversion. GitHub is used in this project.

Classic editor is preferred to build the pipeline. Build can also be configured with YAML.

Repository and branch are selected.

Build template is selected according to the development stack.

To build code or deploy software using Azure Pipelines at least one agent is needed. In this article self-hosted agent is used, however other agents can be picked from the agent pool.

The build process is split into tasks regarding the build steps. In this example, tasks are used for restoring NuGet packages, building solution, assembly tests and publishing the artifact. Other task templates can be found on Azure Marketplace.

Click Save & Run to run the pipeline and build the project

This page is used to view pipelines and output details of previous runs.

Steps of Creating Release

Release Pipeline is employed for automating the deployment. In this project, we start by downloading the artifact file generated by the build pipeline. Then we divided the pipeline into two stages, which are Backup and Deployment.

The backup stage consists of 2 tasks, archiving the source and creating a transient backup of the last version. These tasks are explained below.

The deployment stage consists of 2 jobs:

Deployment job:

  • File transform for replacing config values from Azure Library,
  • IIS utilities to stop site,
  • IIS utilities to stop pool,
  • Delete files for clear application root for artifact files,
  • Extract files for extracting artifact file,
  • IIS utilities to start pool,
  • IIS utilities to start site,
  • Rest call to check if site is alive

Rollback job:

  • IIS utilities to stop site,
  • IIS utilities to stop pool,
  • Delete files task to clear application root for backup files,
  • IIS utilities to start pool,
  • IIS utilities to start site,
  • Rest call to check if the site is alive,
  • First new release pipeline is created.

Creating Release Pipeline

Navigate to “Releases > New > New release pipeline”

In order to create a custom template, click Empty Job.

An artifact is selected for deployment.

In this project build is selected as source type. Different version control systems can be used if the need arises. Source alias is renamed to be used in other tasks.

In this step stages are customized and populated with tasks.

Backup Stage and Tasks

In this project first stage is renamed Backup Stage. Inside Agent job, artifact and pool settings are setup.

Since this agent won’t use any artifact, artifact download option can be disabled via unselecting artifact

  • Archive Files Task:

Click add a task to agent job, then search “archive” and select Archive Files task from Microsoft

Change values according to screenshot.

Define appRemoteSourcePath and appRemoteSourceBackupFileName deployment release variables. For example:

appRemoteSourcePath : \\192.168.33.334\\c$\inetpub\wwwroot\HelloAzurePipelines

appRemoteSourceBackupFileName: \\192.168.33.334\\c$\Backup\HelloAzurePipelines\$[format(‘{0:yyyyMMddHHmm}’, pipeline.startTime)].zip

  • Copy Files Task:

Click add a task to agent job, then search “copy files” and select Copy Files task from Microsoft

This task is conducted so that rollbacks can be done in case of failure. The latest stable source code is backed up in Latest folder.

Source Folder: $(appRemoteSourcePath)

Target Folder: $(appRemoteSourceBackupLatest)

These radio buttons are checked.

Define “appRemoteSourceBackupLatest” as release variable.

Click Pipeline in menu to return to the pipeline overview. Now we will add deployment stage.

Deployment Stage and Tasks

Add a new stage with empty job. Rename it to “Deployment Stage”. This time artifact download will be needed, make sure all artifacts are selected.

  • Replace Variables from Library Task:

Add file transform task rename it to Replace Variables from Library Task:

This task is performing variable substitution. It can also perform xml transformation. Xml transformation is not used in this project. This task can also be utilized by carrying the transform settings in *.csproj to here. Variables in *.config files are replaced by the variables in library with matching keys/names.

Pipeline variables are specific to that pipeline. Variable groups are created in the library and used by linking variable groups to pipelines.

When adding new variable to variable groups, stage can be specified or release can be picked for general pipeline usage.

In this project configuration variables are moved to “Pipeline -> Library -> Variable group”

  • IIS Website Stop Task:

Add IIS Utilities task

From pipeline variables define:

“serverComputerName” with Deploy machine computer name as value. Also “iisWebsiteName” and “iisAppPoolName” should be declared.

Select scopes as deployment stage

  • IIS Application Pool Stop Task:

Same steps, only change task name and Action

  • Delete Everything Except Content Task:

For keeping the Content folder “**” and “!Content/**” are added to Contents field.

  • Extract Artifact to App Path:

Destination folder: $(appRemoteSourcePath)

  • IIS Application Pool Start and IIS Website Start:

Clone IIS Website Stop and IIS App pool Stop and replace actions to start.

  • Is App Alive Test:

Add a Rest Call task and add a webservice endpoint which should be pinged for liveness test.

Rollback Jobs:

IIS Website Stop, IIS Application Pool Stop, Delete everything except Content, IIS Website Start, Is App Alive Test are all same tasks. The only different task is: “Copy From Latest Backup”

Conclusion

To sum up, Azure Pipelines offer rich features to design and automate integration and deployment processes. We have given end to end examples for you to utilize Azure pipelines in your projects. You can use this article while building your CI/CD pipelines on Azure as a guide.

Have automated days…

Special thanks to İncilay Dikbıyık, Evren Pehlivan and Fatih Akgöz for their contributions in the preparation of this article.

--

--