Unable to print the variable right after set it in Azure devops pipeline

733 Views Asked by At

Please see the code below. It is unable to print the variable right after set.variable task! I had feelings and tried as well that NOTHING below the set.variable task will be executed at all. This is so annoying and How can I solve it?

  - bash: |
      echo "##vso[task.setvariable variable=BUILD_NUMBER]$(Build.BuildId)"
      echo $(BUILD_NUMBER) // BUILD_NUMBER: command not found
    displayName: Set 

  - bash: |
      echo "you shall saw build id below"
      echo $(BUILD_NUMBER) // Same line and this is OK!
    displayName: Print 
1

There are 1 best solutions below

0
On BEST ANSWER

That makes sense to me, honestly. You run the ##vso[task.setvariable] command, and it makes the variable available to subsequent tasks once the current task is finished running. It doesn't alter the environment of the current task. Since you clearly already have the data available in the current task, it shouldn't matter.

You don't even need to do what you're doing: You can reference variables as environment variables by replacing periods with underscores. I.E. $BUILD_BUILDID. Refer to the documentation.