Set Variable¶
##vso[task.setvariable variable=release;isReadOnly=true]$release¶
The Azure Pipelines command echo "##vso[task.setvariable variable=release;isReadOnly=true]$release" is used to set a pipeline variable in a specific way.
Syntax and Components¶
##vso[task.setvariable]:This is an Azure Pipelines logging command used to set or update variables during a pipeline run. It communicates directly with the Azure DevOps agent to change variable values.
variable=release:This specifies the name of the variable you want to set or update. In this case, the variable name is
release.isReadOnly=true:This option marks the variable as read-only. Once a variable is set with
isReadOnly=true, it cannot be changed by subsequent steps in the pipeline. This is useful to ensure that critical variables remain constant once they've been set.$release:- This represents the value to assign to the
releasevariable. The$releasenotation implies that the value is taken from a previously defined variable or expression.
Example Scenario¶
Purpose: You want to set a pipeline variable named
releaseto a certain value and ensure that this variable is immutable (read-only) for the remainder of the pipeline execution.Value: The value assigned to the
releasevariable is derived from another variable or is dynamically generated during the pipeline execution.
How It Works¶
- Execution: During a pipeline run, the command
echo "##vso[task.setvariable variable=release;isReadOnly=true]$release"is executed by the Azure Pipelines agent. - Setting Variable: The
task.setvariablecommand sets the value of thereleasevariable to the value specified by$release. - Read-Only: By specifying
isReadOnly=true, thereleasevariable is locked, preventing any further changes to it.
Example Usage in a Pipeline¶
jobs:
- job: SetVariable
steps:
- script: |
# Define a value for the variable
release_value="v1.2.3"
# Set the variable 'release' and make it read-only
echo "##vso[task.setvariable variable=release;isReadOnly=true]$release_value"
displayName: 'Set release variable'
- script: |
# Use the read-only variable
echo "The release version is $(release)"
displayName: 'Display release variable'
Key Points¶
Persistence: The variable
releasewill be available to subsequent steps in the pipeline, but it cannot be modified after it's set as read-only.Scope: The variable
releasewill only be available in the pipeline run in which it was set and won't persist across different pipeline runs.
This command is a powerful way to manage and secure variable values within Azure Pipelines, ensuring that critical data remains unchanged throughout the pipeline's execution.