Echo in the Azure Pipelines's YAML file prints different things

Viewed 9666

I have this code inside my YAML file for an Azure pipeline:

steps:
- bash: |
    echo $(date +%Y)
    echo "##vso[task.setvariable variable=buildName]$(date +%Y)_$(date +%m)_$(Build.BuildNumber)_v3.7"
    echo $BUILDNAME

The first echo correctly prints 2020, but the third print $(date +%Y)_$(date +%m)_4728573844_v3.7. As you can see, here the year and month are not translated into values. Why?

1 Answers

This is the expected action which caused by limitation of variable.

To set a variable from a script, you use the task.setvariable logging command. This doesn't update the environment variables, but it does make the new variable available to downstream steps within the same job.

The nutshell is when you use task.setvariable command to new one variable in Bash task, the variable you created will not available in current Bash task. It only available in next steps.

So, here, when you add another Bash task and echo this variable you created buildName. You will see it is created successfully:

steps:
- bash: |
    echo $(date +%Y)
    echo '##vso[task.setvariable variable=buildName]$(date +%Y)_$(date +%m)_$(Build.BuildNumber)_v3.7'

- bash: |
    echo $(buildName)

enter image description here

Related