Create a Single Build/Release Pipeline for Multiple Branches

In this article we will look at how to create a single Azure DevOps Build Pipeline that triggers on multiple branches.

I am using the repository at https://dev.azure.com/maruma/SampleApp as the working example. If you would like to follow along you will want to clone this repository into your own Azure DevOps subscription.

This example depends on two branches, a develop and a master branch, and the work flow is as follows:

Whenever we commit to thedevelop branch, we want Azure DevOps to kick off a build and release to the Azure development environment.

If things look good in the development environment, we will create a pull request for the master branch, which in turn will kick off another build and release to the Azure staging environment.

If things look good in the production staging environment, we will then promote what is currently in the Azure staging environment to the Azure production environment.

Follow the steps below to create the Build Pipeline.

  1. In Azure DevOps, navigate to the project and then navigate to Builds.
  2. Click New and then New build pipeline.
  3. Click Use the classic editor, if you have YAML preview turned on, otherwise, skip this step.
  4. Select Azure Repos Git.
  5. If it is not already selected, select master branch, and click Continue.
  6. Select ASP.NET Core from the available build templates.
  7. Click Apply.

This repository contains multiple projects but we only want to build and release the SampleWebApp.

  1. Click Tasks.
  2. Click Pipeline.
  3. Add SampleWebApp/*.csproj for Project(s) to restore and build.
  4. Add SampleWebAppTests/*.csproj for Project(s) to test.

We now want to enable continuous integration to auto start our build on a commit.

  1. Click Triggers.
  2. Click enabled continuous integration.
  3. Add Branch filters for both the develop and master branch.
  4. Click Save & queue, and then click it again, select the develop branch and click Save & queue again.

This will create an initial build for our develop branch.

Now lets create our Release Pipeline based on the desired workflow.

  1. In your Azure DevOps project, navigate to Releases.
  2. Click New and then New release pipeline.
  3. Click Empty job, we will add the tasks later.
  4. Name your first stage to Development.
  5. Add two more stages, one for Staging and one for Production, again select Empty job and rename.
  6. Next to Artifacts, click Add, then select the build pipeline.

Now the fun begins.

We want to modify the Pre-deployment conditions for the Staging stage.

  1. Click on the lightning bolt icon on the Staging stage – should say Pre-deployment conditions if you hover over it.
  2. Change the Select Trigger to After Release.
  3. Enable Artifact filters.
  4. Click Add to create a filter for the master branch. This part of the release pipeline will only run for builds of the master branch.
  5. Repeat for the Development stage, except create a filter for the develop branch.

We now want to add an approval gate to the Production stage.

  1. Click Pre-deployment conditions, the lightning bolt icon next to the stage.
  2. Enable Pre-deployment approvals.
  3. Add yourself to the list of Approvers.

We are now going to enable continuous deployment, this will trigger the Release pipeline after a successful execution of the Build pipeline.

  1. In Artifacts, click the lightning bolt icon next to the build artifact.
  2. Enable continuous deployment.
  3. Add Build branch triggers for both the develop and master branches.

Last step is deploying to Azure.

This example assumes you have a resource group for each environment and a single web app in each resource group.

  1. Click the 1 job, 0 task link on the Development stage.
  2. Click Agent Job and click Add, the plus sign icon.
  3. Select Azure App Service Deploy.
  4. Select your Subscription.
  5. Select your App Service Name.
  6. Click Pipeline.
  7. Repeat for the Staging and Production stages, pointing to the correct Azure resources.
  8. Click Save.

Queue up a new Build for the develop branch.

When the build completes it should kick-off the Release.

To test the Staging to Production workflow queue up a new Build for the master branch.

Taking a closer look at the release triggered by the master branch build we can see the Artifact condition was not met for the Development stage, so it was bypassed.

You can also see that our Production stage is now pending Approval. Were you to click Approve it would deploy the Staging build to Production.

Hopefully this helped you and if not or if there was something I could have added/removed to make it more beneficial, please let me know.

God bless and keep coding!

3 Replies to “Create a Single Build/Release Pipeline for Multiple Branches”

  1. “If things look good in the production environment, we will then promote what is currently in the Azure staging environment to the Azure production environment.”

    I think that it should be: “If things look good in the staging environment”

Leave a Reply

Your email address will not be published.