Set VSTS output variable to be result from bash command
N

4

11

I'm running a task in VSTS which performs some operations on a variable from a previous step and I then need to output the result to be used in future tasks. I have the following in a command line task running on a linux build host but am having no luck when trying to use the result later with $(podName3).

COMMAND="$(echo '$(pods)' | grep -oh -P '[^ ]*' | grep schema)"
##vso[task.setvariable variable=podName3]"$COMMAND"

I have tried several variations on this to no avail and need some direction as this has stumped me for too long now

Nephew answered 30/7, 2018 at 7:6 Comment(0)
B
18

Seems the syntax is incorrect.

Just try to below format:

COMMAND="$(echo '$pods' | grep -oh -P '[^ ]*' | grep schema)"
echo "##vso[task.setvariable variable=podName3]$COMMAND"

Or add a powershell task and run below commands to set the variable:

$COMMAND="$(echo '$env:pods' | grep -oh -P '[^ ]*' | grep schema)"

Write-Host "##vso[task.setvariable variable=podName3]$COMMAND"

More information please see Define and modify your variables in a script

Barreto answered 30/7, 2018 at 9:43 Comment(3)
The first code snippet you posted simply assigns $COMMAND to the variable and does not evaluate the command block beforehand. Unfortunately as its a linux build host I'm not able to run a powershell script on it. If this isn't the right way to go about evaluating the command block I've assigned to the variable and then assign the output to an output variable please let me know of a better way about this problemNephew
You set me on the right path with this thanks i think it was mostly a case of looking at the one thing for too long. The solution was essentially just correctly formatting the syntax to the following which evaluated the variable and then assigned it to the output variable. COMMAND=$(echo '$(pods)' | grep -oh -P '[^ ]*' | grep schema) echo "##vso[task.setvariable variable=podName3]${COMMAND}"Nephew
^ ${COMMAND} method worked for me too, thanks @David Parsonson. Though, for any one else struggling with this, this will still evaluate as ${COMMAND} if your echo "##vso[...]" command uses single quotes (i.e. echo '##vso[...]') - so make sure you are using double-quotes!Beefeater
Y
2

I created a command line tool & an Azure DevOps task for this: https://marketplace.visualstudio.com/items?itemName=riezebosch.setvar

It just lets you pipe the output of a command into the tool and output it as the magic variable string. It's written in Go and cross compiled so works on all major platforms and all different shells.

Your example:

echo '$pods' | grep -oh -P '[^ ]*' | grep schema | setvar -name podName3

You only need to include the setvar task prior to this script task in order to get the tool on the agent.

Yeryerevan answered 14/11, 2018 at 19:47 Comment(0)
N
1

Adding a PSA to this post, looks like they changed the way the variables are accessed now - you have to access variables like this: $(variable)

COMMAND=$(echo '$pods' | grep -oh -P '[^ ]*' | grep schema)
echo "##vso[task.setvariable variable=podName3]$(COMMAND)"
Navigable answered 22/3, 2021 at 20:29 Comment(0)
G
0

Here's what worked in 2023, similar but (critically) not identical to the other answers:

  - script: |
      version=$(npm pkg get version)
      echo "##vso[task.setvariable variable=VERSION]${version}"
  - task: Docker@2
    inputs:
      command: build
      arguments: --build-arg VERSION=$(VERSION)
      ...
Guthrie answered 2/11, 2023 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.