bashazureazure-pipelinescicd

Reference a variable azure pipeline not working


azure-pipelines.yml

variables:
  version : "1"

  - script: | 
      echo "version is: ${{variables.version}}"  // version is 1
      set /a newVersion = $(version) // not working returns empty
      newVersion = $(( ${{version}} )) // not working returns empty

The bash script not working, i want to assign version in a variable newVersion and increment the number. Any help


Solution

  • You can pass the value of pipeline variable to the Bash variable like as below.

    See below examples as references:

    variables:
      version: 1
    
    steps:
    - bash: |
        echo "version is: $(version)"
        newVersion=$(version).2.3
        echo "newVersion is: $newVersion"
      displayName: 'Set variable'
    

    enter image description here

    NOTE:

    Above Bash variable newVersion can only available for the current script step (Set variable) and not for the subsequent steps.

    If you want to use the value of newVersion in the subsequent steps, you can use the logging command "SetVariable" to set a pipeline variable with the its value.

    echo "##vso[task.setvariable variable=New_Version;]$newVersion"
    

    This command also will automatically map a environment variable (NEW_VERSION) for the pipeline variable (New_Version).

    Then in the subsequent steps within the same job, you can use the expression "$(New_Version)" to directly reference the pipeline variable, or the expression $NEW_VERSION to reference its environment variable.

    variables:
      version: 1
    
    steps:
    - bash: |
        echo "version is: $(version)"
        newVersion=$(version).2.3
        echo "newVersion is: $newVersion"
        echo "##vso[task.setvariable variable=New_Version;]$newVersion"
      displayName: 'Set variable'
    
    - bash: |
        echo "version is: $(version)"
        echo "newVersion is: $(New_Version)"
        echo "newVersion is: $NEW_VERSION"
      displayName: 'Print variable'
    

    enter image description here