Skip to content

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

  1. ##vso[task.setvariable]:
  2. 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.

  3. variable=release:

  4. This specifies the name of the variable you want to set or update. In this case, the variable name is release.

  5. isReadOnly=true:

  6. 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.

  7. $release:

  8. This represents the value to assign to the release variable. The $release notation implies that the value is taken from a previously defined variable or expression.

Example Scenario

  • Purpose: You want to set a pipeline variable named release to 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 release variable is derived from another variable or is dynamically generated during the pipeline execution.

How It Works

  1. Execution: During a pipeline run, the command echo "##vso[task.setvariable variable=release;isReadOnly=true]$release" is executed by the Azure Pipelines agent.
  2. Setting Variable: The task.setvariable command sets the value of the release variable to the value specified by $release.
  3. Read-Only: By specifying isReadOnly=true, the release variable 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 release will be available to subsequent steps in the pipeline, but it cannot be modified after it's set as read-only.

  • Scope: The variable release will 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.