Azure Pipeline Variables: What You Need to Know
In Azure Pipelines, programmers use variables to conveniently manage configuration values within builds and releases.
Pipeline Variables
In mathematics, a variable is a value that can be any number. In Azure DevOps, a variable has a similar function: holding the place for a value that can change from one run to the next. Azure DevOps pipeline variables can be either user-defined or system-defined:
- User-defined variables - Manually set by the user. These variables are usually defined before each run.
- Predefined variables - Set by the system and can’t be changed by the user. These system-defined variables are read-only and set prior to each run.
Once set, a variable lets you change a pipeline’s workflow by passing bits of data into various parts of the pipeline. Additional variables include:
- Output variables - Generated during one task in the pipeline and used downstream. Allows data to be passed through different points in the pipeline without additional user input.
- Secret variables - Defined by either the user or the system. Encrypted to protect sensitive information (e.g., API key, password, etc).
Variable Environments
An Azure DevOps pipeline includes variable environments - locations where you can interact directly with the aforementioned variables. In a YAML (Yet Another Markup Language) build definition, this space is called the pipeline environment. Within a script, the space to interact with variables is called a script environment.
Variable Syntax
Azure Pipeline variables are referenced as three syntax methods: macro, template expression, and runtime expression. Each syntax has a different function and limitations to consider when selecting one to use.
- Macro syntax is written as $(var) where var is the variable value. It is processed during runtime, but before a task runs.
- Template expression syntax is written as ${{variable.var}} and is processed at compile time (before runtime starts).
- Runtime expression syntax, written as $[variable.var], is similar to macro syntax. It is processed during runtime, but utilized with conditions and expressions
Within macro and runtime expressions, if variables are not replaced, they return as an empty string. In a script environment, a variable is made available by creating an environmental variable. The environmental variable is accessed and set through standard methods depending on its coding language. This variable is always upper-case in the code and dots will be replaced with underscores.
Variable Execution
A pipeline runs in three stages: queue, runtime, and compile.
- The Queue stage occurs before the pipeline begins its run, but it’s queued up and ready to go once the agent is available. During this stage, variables are defined by the user or system.
- Following the Queue stage, the pipeline begins the Runtime stage during which the YAML file is processed, but scripts are not running yet.
- Once the Runtime stage is complete, the pipeline enters the Compile stage. During this stage, the code is executed via the variables defined during the Queue stage.
By knowing what variables are and how they are used in Azure Pipelines, you can incorporate them into your own Azure DevOps pipelines to automate a wide range of tasks. Enjoy improved accuracy and productivity by knowing how and when to incorporate variables into your builds.
Source
Trusted Tech Team is an accredited Microsoft CSP Direct Bill Partner, carrying multiple Solutions Partner designations and the now-legacy Microsoft Gold Partner competency. Based in Irvine, California, we report trends affecting IT pros everywhere.
If your organization uses Microsoft 365 or Azure, you may be eligible to receive a complimentary savings report from a Trusted Tech Team Licensing Engineer. Click here to schedule a consultation with our team now to learn how much you can save today.
Subscribe to the Trusted Tech Team Blog
Get the latest posts delivered right to your inbox