azure-pipelines-agent: Update environment variables using `task.setvariable` in a bash script does not work

I’m trying to update environment variables using a bash script. but the variables are not being udpated. Please see script below:

echo "version ~> $VERSION"
echo "build ~> $BUILD"
echo "##vso[task.setvariable variable=version;]2.0.0"
echo "##vso[task.setvariable variable=build;]20"
echo "Version ~> $VERSION"
echo "Build ~> $BUILD"
screen shot 2018-12-14 at 12 22 51 pm screen shot 2018-12-14 at 12 27 54 pm screen shot 2018-12-14 at 12 23 19 pm

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 31 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I have solved it in this way:

echo "##vso[task.setvariable variable=ACR_REGISTRY_LOGIN]$ACR_REG"

1.- With double quotes in echo command. 2.- Removing semicolon at the end of variable name.

Regards.

@TingluoHuang @bryanmacfarlane It would be nice to update the documentation on this matter, I have myself spent a few hours trying to figure it out.

Both are working for me:

export OLD_VARIABLE="Hello World"
echo '##vso[task.setvariable variable=new_variable;]'$OLD_VARIABLE

# and

export OLD_VARIABLE="Hello World"
echo "##vso[task.setvariable variable=new_variable;]$OLD_VARIABLE"

I cannot tell you how much this has saved my bacon 😃 amazing thank you!!

This does not work, I have also the same issue. This is happening in Bash@3 task in yaml pipeline.

@TingluoHuang @bryanmacfarlane It would be nice to update the documentation on this matter, I have myself spent a few hours trying to figure it out.

Both are working for me:

export OLD_VARIABLE="Hello World"
echo '##vso[task.setvariable variable=new_variable;]'$OLD_VARIABLE

# and

export OLD_VARIABLE="Hello World"
echo "##vso[task.setvariable variable=new_variable;]$OLD_VARIABLE"

@TingluoHuang I am a bit tired of try things… 😉 In this way it runs.

@carvalho-oak ##vso[task.setvariable] change the variables in the agent’s context, the change will show up in your following task. in your sample, if you add another bash script task and let it run printenv|sort, you will see your new $Build/$Version

@ferpega # is comment in bash, you need to escape it. ##vso[task.setvariable] is a instruction to let the agent update its variable collection, so following task can consume the variable.

the following should work. task1: echo ‘##vso[task.setvariable variable=ACR_REGISTRY_LOGIN;]$ACR_REG’ task2: echo $ACR_REGISTRY_LOGIN