avatarKelli Mohr

Summary

The web content provides a comprehensive guide on using Variable Groups in Azure DevOps Pipelines to manage multiple environments without duplicating code, demonstrating how to create, link, and utilize Variable Groups in both Build and Release Pipelines.

Abstract

The article discusses the challenge of managing multiple AWS regions and accounts without duplicating code, which can lead to maintenance issues and poor code quality. The author introduces Azure DevOps Variable Groups as a solution to apply different sets of variables to a single codebase. The guide explains how to create Variable Groups in Azure DevOps, link them to Build and Release Pipelines, and use them to streamline the CI/CD process for multi-region deployments. It includes step-by-step instructions for setting up a repository, defining Variable Groups, and integrating them into both YAML-based Build Pipelines and GUI-based Release Pipelines. The article also illustrates how to echo variable values to validate the linking process and how to create separate jobs and stages for different environments, such as AWS us-east-1 and us-west-2.

Opinions

  • The author expresses that duplicating code for multiple regions and environments would result in a "nightmare to maintain" and have a "horrible code smell."
  • Upon discovering Variable Groups, the author found them to be a "promising" feature for managing variables across pipelines.
  • The use of Variable Groups is seen as an effective way to reduce repeated code and apply a single codebase to various environments, which is considered an opportunity to improve the CI/CD process in Azure DevOps.
  • The author's tone suggests that mastering Variable Groups can significantly enhance the efficiency and maintainability of deployment pipelines in Azure DevOps.
Photo by Bruce Warrington on Unsplash

Learn to Use Variable Groups in Azure DevOps Pipelines

A few months ago, I set up an AWS development environment in one region with Terraform and Chef. I realized I needed to refactor my code to handle multiple regions aka multi-region and AWS accounts. I didn’t want to duplicate code for the various regions and environments. The solution would become a nightmare to maintain and have a horrible code smell. Ew.

I was new to Azure DevOps for CI/CD pipelines and I wanted to find a way to apply different sets of variables to the same code base, so I poked around and found Variable Groups. ‘This sounds promising!’ I thought and started digging through Microsoft docs to learn more. So here’s the scoop on Variable Groups.

  • You can share variable groups between pipelines.
  • You can “link’“ variable groups to specific stages in a pipeline, build, or release.

Variable Groups

To create a Variable Group, follow the steps below:

  1. In the left panel, click and expand Pipelines.
  2. Under Pipelines, click Library.
  3. Click the Variable group button.
  4. Enter a name for the variable group in the Variable Group Name field.
  5. Use the Description field to enter information about the variable group.
  6. Click the Add button to create a new variable for the group.
  7. Fill in the variable Name and Value.

An example variable group for AWS us-east-1.

For this tutorial, we’ll link our Variable Groups to an Azure DevOps Build and Release Pipeline. I’ll show two methods:

  1. As code with azure-pipeline.yml file in the Build Pipeline.
  2. With the Azure DevOps GUI in the Release Pipeline.

Build Pipeline

Before we can link the variable group to our build we need to set up a repository. The repository can be set up in Azure DevOps or GitHub. For this tutorial, I used Azure DevOps. I’ve also posted the code on GitHub but it’s pretty basic output to demonstrate how the Variable Groups function.

Lets set up the build with the steps below:

  1. In the left panel, expand Pipelines.
  2. Click on the New pipeline button.
  3. Associate the pipeline to a repo in GitHub or Azure DevOps.
  4. Click Run.

As we can see, our first build succeeds as expected, but none of the variables were echoed during the build.

Next, lets associate the variable groups the build. Since the build uses the azure-pipelines.yml file , we’ll update the code to use the variable groups. For more information on Azure Pipelines, check out Microsoft’s YAML schema reference.

In your editor, open up azure-pipelines.yml and add the code snippet below to the beginning of the file.

variables:

- group: dev_us-west-2

Save and commit the file. The build should execute and output the variables stored in the variable group dev_us-west-2 (see below). Now try it with dev_us-east-1.

Let’s take our variable groups a step further and create two jobs: “Build us-east-1” and “Build us-west-2”

For this exercise, we’ll match the appropriate variable group to the it’s job. Open up the azure-pipelines.yml file in your favorite editor and replace the text provided in this GitHub branch. Or use the code below.

After you’ve saved your changes, commit and push the code to run the build. Now you see two separate jobs, one for “build_Virgina” and another for “build_Oregon” each of our regions.

Open each job and click on the “Show Variable Values” step. The values echoed for each Variable Group correspond to the assigned job in the code block above. See the images below.

Now that we learned how to associate Variable Groups to build pipelines through code, let’s move on to Releases!

Release Pipeline

For this section of the tutorial, we will create a Release Pipeline for both of our regions, us-east-1 and us-west-2. Use the steps below to create a new Release in Azure DevOps.

  1. In the left hand panel, click Pipelines.
  2. Click Releases.
  3. Click the New pipeline button.
  4. A new pane will open for Stage 1, prompting you to Select a template Or start with an Empty job.
  5. Click the Empty job link.
  6. A new pane opens for the stage. Change the Stage name to Release us-east-1.
  7. Use the x in the upper right corner to close the pane.

Next, we will link the Variable Group dev_us-east-1 to the Release us-east-1 stage. To do this, follow the steps below:

  1. Click the Variables menu.
  2. Select Variable groups in the middle panel.
  3. Click the Link variable group button.
  4. Select Dev_us-east-1.
  5. Under the Variable group scope, select the Stages radio button.
  6. In the drop-down box, select Release us-east-1.
  7. Click the Link button.
  8. Click the Pipeline to return to the Stages.

Now we need to create a new task for our release. To keep it simple for this tutorial, we’ll write a script to echo the values of our pipeline variables to validate our Variable Group has been linked.

  1. Click the 1 job, 0 task link under the Release us-east-1 stage.
  2. Click the blue + on the Agent job section.
  3. A new pane opens, search for or select Command Line.
  4. Under the Agent job section, Click Command Line Script.
  5. Update the Display name to Echo Variable Group Values.
  6. Add the snippet below to the Script text box.

echo %REGION%

echo %STATE_BUCKET%

echo %INSTANCE_SIZE%

  1. Click Save.
  2. Click Release.
  3. Select Create a Release.
  4. A new pane opens, click the Release button.

After the release begins, a banner displays on screen. Click the Release-1 link provided to open the details and see the output. Use the steps below.

  1. After the release completes, click the Release us-east-1 stage box.
  2. Click the Echo Variable Group Values in the Agent job section.

The variable values for dev_us-east-1 have been exported successfully for this stage of the release.

Finally, we will create a new stage for Release us-west-2 and associate this stage with the dev_us-west-2 Variable Group. Go back to the Pipelines and Releases sections to edit the existing release.

  1. Hover over the Release us-east-1 box, click the Clone button (looks like two papers stacked).
  2. A copy of the release appears, click the box to edit.
  3. Update the Stage name to Release us-west-2.
  4. Click the Lightening Bolt icon.
  5. Change the Triggers to After Release.
  6. Click the Variables menu.
  7. Click the Variable groups section.
  8. Click the Link variable group button.
  9. Select dev_us-west-2.
  10. Under the Variable group scope, click the Stages radio button.
  11. This time select Release us-west-2.
  12. Click the Link button.
  13. Click the Save button in the upper right.

Now we have separate stages to release to our two AWS regions in us-east-1 and us-west-2. As you can see, Azure DevOps Variable Groups gives us the opportunity to apply one code base to many different environments and the ability to reduce repeated code.

DevOps
AWS
Cloud Computing
Azure
Women In Tech
Recommended from ReadMedium