Azure Pipeline Variables and Concatenation Best Practices

Author

Reads 861

Woman in focus working on software development remotely on laptop indoors.
Credit: pexels.com, Woman in focus working on software development remotely on laptop indoors.

Using variables in Azure Pipelines is a great way to make your builds and releases more dynamic and flexible. Variables can be defined at the pipeline level, stage level, or even job level.

One of the key benefits of variables is that they can be easily concatenated to create more complex values. For example, you can use the `${variable1} + ${variable2}` syntax to concatenate two variables.

To avoid common pitfalls when concatenating variables, it's essential to understand the difference between string and non-string variables. Non-string variables, such as numbers or booleans, cannot be concatenated using the `+` operator.

By following these best practices, you can ensure that your Azure Pipelines are efficient, scalable, and easy to maintain.

Azure Pipeline Variables

Azure Pipeline Variables are a powerful tool for automating your build and deployment processes. They allow you to reference predefined variables, such as the source branch, build reason, and artifact staging directory, which can be accessed using specific names like "Build.SourceBranch".

Credit: youtube.com, Pass variables and parameters between build and release pipelines on Azure DevOps

You can find a full list of predefined variables in the Azure DevOps documentation. Predefined variables include Source branch, Build reason, and Artifact staging directory.

To create or set a variable dynamically, you can use logging commands. This is useful for tasks like retrieving the connection string to a storage account or calculating a build number in a script.

A common use case for dynamic variables is to get the username of the current user for use in subsequent steps. You can create a variable called "currentUser" with the value using a logging command.

When writing bash or PowerShell commands, be careful not to confuse "$(var)" with "$var". "$(var)" is interpolated by Azure DevOps when the step is executed, while "$var" is a bash or PowerShell variable.

Here are some examples of creating dynamic variables using logging commands:

  • `echo "currentUser=$(whoami)"` (bash)
  • `Write-Host "currentUser=$(whoami)"` (PowerShell)

To make the variable a secret, simply add "issecret=true" into the logging command. This is useful for sensitive information like passwords or API keys.

Note that there are two flavors of PowerShell: "powershell" is for Windows and "pwsh" is for PowerShell Core, which is cross-platform.

Concatenating Variables

Credit: youtube.com, 27. append variable activity in azure data factory| Azure data factory

Concatenating variables can be a challenge, but Azure Pipelines provides a few ways to make it easier. You can use string literals with the "folded style" to concatenate long values, making them easier to read and manage.

This style uses the greater than (') character to combine multiple lines into a single string. For example, if you need to update Azure App Service settings, you can use this style to build a string value that can get quite long.

Sometimes, you need to concatenate values from an array, and that's where the "join" command comes in. It concatenates all elements in the array, separated by a string, and it's especially useful when you need to add a separator between each item.

Here are the details about the "join" command:

  • Concatenates all elements in the right parameter array, separated by the left parameter string.
  • Min parameters: 2. Max parameters: 2
  • Each element in the array is converted to a string. Complex objects are converted to empty string.
  • If the right parameter isn't an array, the result is the right parameter converted to a string.

This command is flexible and can handle different types of parameters, making it a valuable tool in your Azure Pipelines toolkit.

Variable Syntax and Usage

Credit: youtube.com, Azure DevOps Pipeline- Reference Variables and Syntax

Dynamic variables are created and/or calculated at run time, and can be used to inject values into a web.config, like retrieving the connection string to a storage account using the “az cli”.

You can create a variable called “currentUser” with the value using logging commands. The syntax is $(var), which is interpolated by Azure DevOps when the step is executed.

To avoid confusion, use env to create environment variables rather than dereferencing variables inline. For example, you can write env:CURRENT_USER=${USER}.

In PowerShell, you can use the same syntax, but there are two flavors: “powershell” for Windows and “pwsh” for PowerShell Core, which is cross-platform.

Inline Variables

Inline variables are variables that are hard coded into the pipeline YML file itself. They're perfect for specifying values that are not sensitive and unlikely to change. A good example is an image name, like in a pipeline that builds a Docker container and pushes it to a registry.

Credit: youtube.com, Getting Started - Referencing Variables

You can create a variable called "imageName" so that you only have to maintain the value once rather than in multiple places. This keeps to the DRY (Do not Repeat Yourself) principle.

You cannot create "secret" inline variables. If you need a variable to be secret, you'll have to use pipeline variables, variable groups, or dynamic variables.

Dynamic Variables and Logging

Dynamic variables are variables that are created and/or calculated at run time. You can use logging commands to create or set a variable dynamically.

To create a variable called “currentUser” with the value of the current user's username, you can use the following logging command: $(echo $USERNAME). Alternatively, you can use environment variables: env var=currentUser $USERNAME.

If you need to make the variable a secret, you can add “issecret=true” into the logging command.

In PowerShell, you can create a dynamic variable using the following command: echo $var > $(echo $var). Or, you can use environment variables: env var=$var.

One special case of a dynamic variable is a calculated build number. You can calculate the build number however you need to and then use the “build.updatebuildnumber” logging command.

Precedence and Expansion

Credit: youtube.com, Ansible Variable Precedence and Inclusion

Variables can be defined at various scopes in a pipeline, which can lead to conflicts if you define a variable with the same name at more than one scope.

You need to be aware of the precedence of variables, which is documented elsewhere.

Variables are expanded at the beginning of the run, as well as before each step, which means their values are applied at these points.

This can have significant effects on your pipeline's behavior, so it's essential to understand how it works.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.