0
votes

I have a release pipeline with multiple jobs.

On the first agent job there is an Azure CLI task that retrieves the keys of a storage account on azure.

The command it executes is :

az storage account keys list --account-name "$(diagnosticsStorageAccountName)" --resource-group "$(resourceGroup)"

What I want to do is store the result of this command and utilize it in a task that is running under a deployment group job.

I've already looked into these resource :

Set Output Variable in Azure CLI task on VSTS

How to modify Azure DevOps release definition variable from a release task?

I've tried the first one but I didn't get it working. I didn't bothered with the second because it seems way to hacky.

Is there any way do achieve this that isn't hacky ?

1
Why the first one not work for you? Have you get any error? If yes, please share the error info here.Leo Liu-MSFT

1 Answers

0
votes

The output values didn't get stored properly.

The output of az storage account keys list --account-name "$(diagnosticsStorageAccountName)" --resource-group "$(resourceGroup)"is spread over multiple lines when you use the following syntax:

echo "##vso[task.setvariable variable=testvar;]%myvar%"

So one of the problems was that only the first line of the JSON array was being stored into the variable.

I solved this problem in the following way:

keys=`az storage account keys list --account-name "$(diagnosticsStorageAccountName)" --resource-group "$(resourceGroup)"`
taskvariable="##vso[task.setvariable variable=_[tempVariable];]"   
echo $taskvariable$keys

According to documentation echo "##vso[task.setvariable variable=testvar output=true;]%myvar% should make the variable available for the whole release. Unfortunately I had no luck with this.

I overcame this using a Powershell task under the same Agent Job (Ubuntu 16.0) :

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.1"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $(System.AccessToken)"
}

#Parsing JSON string from previous task
$keys = $(@"
$(_tempVariable)
"@ | ConvertFrom-Json)

# Assignment of variable value
$pipeline.variables.[variableName].value = $keys[0].value

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $(System.AccessToken)"}

write-host "==========================================================" 
Write-host "The value of Variable '[variableName]' is updated to" $updatedef.variables.[variableName].value
write-host "=========================================================="

Please note that in order to get this working there are a couple of things you need to do.

First you need to allow access to the OAuth token on the Agent Job.

On top of that you need to give the "Project Collection Build Service".

Click on security on the release and click on the "Project Collection Build Service" user.

Change the values for the "Edit release" and "Manage release" to allow and save the changes.