Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set VSTS output variable to be result from bash command

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

like image 828
David Parsonson Avatar asked Jul 30 '18 07:07

David Parsonson


People also ask

How do I save the output of a command in Bash?

To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

How are output variables used in release pipeline?

Just specify the reference name in the blank of task, and then, you can call this output variable abc by using the format: <reference name>. <variable name> . For example, if you specify the reference name as mycustom , just call the output variable by using $(mycustom. abc) .

How do you set variables in Azure pipeline?

setvariable , the following tasks can use the variable using macro syntax $(myVar) . The variable will only be available to tasks in the same job by default. If you add the parameter isoutput , the syntax to call your variable changes. See Set an output variable for use in the same job.


2 Answers

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

like image 199
Andy Li-MSFT Avatar answered Oct 16 '22 15:10

Andy Li-MSFT


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.

like image 2
riezebosch Avatar answered Oct 16 '22 13:10

riezebosch