Azure Env Variables are a crucial part of managing your environment settings in Azure. They allow you to store sensitive information, such as API keys and connection strings, securely and make them available to your applications.
In Azure, you can create and manage environment variables through the Azure portal, Azure CLI, or Azure PowerShell. This flexibility allows you to choose the method that best suits your needs.
Azure Environment Variables can be used in various Azure services, including Azure Functions, Azure App Service, and Azure Kubernetes Service.
Naming and Syntax
Azure environment variables have specific syntax and naming conventions. You can use macro, template expression, or runtime expression syntax to reference variables in a pipeline.
There are three ways to reference variables in Azure Pipelines: macro, template expression, and runtime expression. Macro syntax variables $(var) get processed during runtime before a task runs.
Runtime expression syntax is used for variables that are expanded at runtime ($[variables.var]). Runtime expression variables silently coalesce to empty strings when a replacement value isn't found.
Template expression variables ${variables.var} get processed at compile time, before runtime starts. Macro syntax variables $(var) get processed during runtime before a task runs.
Here's a summary of the syntax:
Environment variables are specific to the operating system you're using. They're injected into a pipeline in platform-specific ways.
Working with Env Variables
You can delete variables in your pipeline with the az pipelines variable delete command. This command requires several parameters, including the name of the variable you want to delete.
To delete a variable, you'll need to specify its name, as well as the Azure DevOps organization URL. You can configure the default organization using az devops configure -d organization=ORG_URL. Alternatively, you can use the --org parameter to specify the organization URL directly in the command.
The following parameters are required to delete a variable: name, org, pipeline-id, and project. The pipeline-id and pipeline-name parameters are mutually exclusive, and you can't use both at the same time.
Environment Provided
Environment variables provided by Azure DevOps (azd) can be a huge help when working with pipelines. These variables are automatically set and can be used in your pipeline scripts.
Some examples of environment variables provided by azd include AZURE_ENV_NAME, which is the name of the environment in-use, and AZURE_LOCATION, which is the location of the environment in-use. These variables are available when an environment is created.
Here are some environment variables provided by azd, along with a brief description of each:
These environment variables can be used in your pipeline scripts to automate tasks and make your pipelines more efficient.
System
System variables are automatically set by the system and their values cannot be changed by you, the end-user. System variables are also referred to as "predefined variables" and are defined by Azure DevOps.
You can find a comprehensive list of system variables for builds here and for releases here. System variables get set with their current value when you run the pipeline.
Some variables are set automatically, like Build.BuildId which gives the ID of each build and can be used to identify different pipeline runs.
Delete
Deleting a variable is a straightforward process. You can use the az pipelines variable delete command to get started.
You'll need to specify the name of the variable you want to delete, which is a required parameter. You can also use the org parameter to specify the Azure DevOps organization URL.
If you're using the pipeline-id parameter, you'll also need to specify the ID of the pipeline. Alternatively, you can use the pipeline-name parameter if the pipeline-id isn't supplied.
To avoid being prompted for confirmation, you can use the yes parameter. Here are the required parameters:
You can use the az devops configure command to configure the default organization and project. For example, you can set the default organization using az devops configure -d organization=ORG_URL.
List
Listing all the variables in your pipeline is a great way to get a snapshot of what's available. You can do this using the `az pipelines variable list` command.
The `az pipelines variable list` command requires a few parameters to work properly. The `org` parameter is the Azure DevOps organization URL, which can be configured as a default using `az devops configure -d organization=ORG_URL`.
You can also use the `pipeline-id` or `pipeline-name` parameter to specify the pipeline you want to list variables for. If you use `pipeline-id`, you must provide it. If you use `pipeline-name`, it's required unless you also provide `pipeline-id`.
The `project` parameter is also required, either by name or ID. This can be configured as a default using `az devops configure -d project=NAME_OR_ID`.
Here are the parameters you can use with the `az pipelines variable list` command:
- org: Azure DevOps organization URL
- pipeline-id: ID of the pipeline
- pipeline-name: Name of the pipeline (ignored if pipeline-id is supplied)
- project: Name or ID of the project
You can use these parameters to list all the variables in a pipeline with a specific ID, like this: `az pipelines variable list --org https://dev.azure.com/MyOrganizationName/ --pipeline-id 12`.
Security and Access
Azure environment variables are stored securely in Azure Key Vault, a cloud-hosted secret store that provides a single source of truth for sensitive information.
To access environment variables in Azure, you can use the Azure CLI or Azure PowerShell, both of which allow you to retrieve and use variables in your scripts and applications.
In Azure DevOps, environment variables can be accessed through the UI, allowing you to easily manage and update variable values without having to write code.
Scopes
Scopes are a crucial aspect of security and access in a pipeline. You can define variables at different scopes to control their availability.
Variables defined at the root level are global and available to all jobs and stages in the pipeline. This means they're not visible in the pipeline settings UI.
You can also define variables at the stage level to make them available only to a specific stage. Variables at this level override those at the root level.
Variables defined at the job level override those at the root and stage levels. This is useful for making variables available only to a specific job.
Here's a quick rundown of the variable scope hierarchy:
- Job level: overrides root and stage levels
- Stage level: overrides root level
- Root level: global, available to all jobs and stages
Access Through the
Access through the environment is a secure way to make variables available to scripts. You can use environment variables to access variables in your pipeline.
To use environment variables, the syntax depends on the scripting language: %VARIABLE_NAME% for batch scripts, $env:VARIABLE_NAME for PowerShell scripts, and $VARIABLE_NAME for Bash scripts.
If you're running Bash script tasks on Windows, use the environment variable method to ensure you have the correct file path styling. Predefined variables that contain file paths are automatically translated based on agent host type and shell type.
You can set a variable for a build pipeline by following these steps:
- Go to the Pipelines page, select the pipeline, and select Edit.
- Locate the Variables for this pipeline.
- Add or update the variable.
- To mark the variable as secret, select Keep this value secret.
- Save the pipeline.
After setting the variable, you can use it as an input to a task or within the scripts in your pipeline. To use a variable as an input to a task, wrap it in $().
Secret
Secrets are a crucial aspect of security in Azure DevOps, and it's essential to handle them properly.
You can store secrets in variable groups, which are groups of variables that can be shared across multiple pipelines.
Variable groups are designed to store sensitive information, such as connection strings, passwords, and API keys, and can be linked to Azure Key Vault for added security.
To reference a secret variable in a YAML file, you need to map it to an environment variable, as shown in Example 1.
The token variable in Example 1 is an example of a secret variable, which is mapped to the environment variable $env:MY_MAPPED_TOKEN.
You can also mark a value in a variable group as secret by clicking the padlock icon next to the value, as mentioned in Example 3.
Variable groups can be cloned to create new environments, and it's essential to keep the variable names the same across environments to ensure consistency.
Configuring and Using
You can set variables by using expressions, such as $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 100)], which increments a counter by 1 for every run and resets it to 100 every day.
To configure settable variables for steps, you can define them within a step or specify that no variables can be set. For example, you can allow the variable sauce but not the variable secretSauce.
To set variables in a pipeline, you can define them in the YAML file and use them within the file. You can also define variables in the pipeline settings UI and reference them in your YAML. For example, you can set two variables, configuration and platform, and use them later in steps.
You can update variables in your pipeline using the az pipelines variable update command. To get started, see Get started with Azure DevOps CLI.
Here's a summary of the parameters for updating a variable:
Predefined
Predefined variables are a convenient way to access system information in your pipeline. You can reference them using their unique names, such as "Build.SourceBranch" or "Build.Reason".
These variables are automatically set by the system and cannot be changed by you, the end-user. You can find a full list of predefined variables in the Azure DevOps documentation.
Some examples of predefined variables include the source branch, build reason, and artifact staging directory. You can use these variables in your pipeline to make it more dynamic and flexible.
Here are some examples of predefined variables:
- Source branch: “Build.SourceBranch”
- Build reason: “Build.Reason”
- Artifact staging directory: “Build.ArtifactStagingDirectory”
These variables are available as “Environment Variables” in the context of build or release runtime. They can be used to customize your pipeline and make it more efficient.
How to
To create a variable in your pipeline, you can use the az pipelines variable create command. This allows you to define variables that are specific to the pipeline and can be used in tasks or scripts.
You can also create variables in the pipeline settings UI, on the variables tab of the build or release definitions. This is useful if you want to set variables that are specific to the pipeline and can be used in tasks or scripts.
To reference these variables in tasks, wrap them in $(), such as $(exampleVariableName). This syntax is used to access the value of a variable.
There are different types of variables you can create in Azure DevOps pipelines, including inline variables, pipeline variables, and variable groups. Inline variables are variables that are hard-coded into the pipeline YAML file itself, while pipeline variables are defined in the pipeline settings UI. Variable groups are a set of variables that can be used across multiple pipelines.
To use a variable as a task input, you must make the variable an output variable, and give the producing task a reference name. This allows you to pass variables from one job to another in a build pipeline, using YAML.
Here are some key points to keep in mind when working with variables in Azure DevOps pipelines:
- Use the az pipelines variable create command to create variables in your pipeline.
- Use the variables tab in the pipeline settings UI to create variables that are specific to the pipeline.
- Reference variables in tasks using the syntax $().
- Use inline variables, pipeline variables, and variable groups to manage variables in your pipeline.
- Use YAML to pass variables from one job to another in a build pipeline.
- Make variables output variables and give the producing task a reference name to use them as task inputs.
By following these best practices, you can effectively manage variables in your Azure DevOps pipelines and make the most of this powerful feature.
Enable Demo Mode
To enable demo mode in azd, set the AZD_DEMO_MODE environment variable. This feature is useful for scenarios where you want to demo or present azd commands in a public setting.
To enable demo mode, run azd with the --demo-mode flag. This will hide your subscription ID in the console output.
If you want demo mode to persist across reboots, you can also run azd with the --set-env flag. This will set the AZD_DEMO_MODE environment variable permanently.
In PowerShell, you can enable demo mode by running azd with the --demo-mode flag, followed by the --set-env flag. This will achieve the same result as running the command in Command Prompt.
Frequently Asked Questions
How do I get environment variables in Azure function?
To access environment variables in Azure function, sign in to the Azure portal, navigate to your function app, and click on Environment variables under Settings. From there, you can view and manage your app's environment variables.
Sources
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables
- https://colinsalmcorner.com/azure-pipeline-variables/
- https://marcusfelling.com/blog/2019/azure-devops-pipeline-variables/
- https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/manage-environment-variables
- https://gregorsuttie.com/2021/08/05/how-to-set-environment-variables-for-use-with-an-azure-function/
Featured Images: pexels.com