0

I have Release Variable $(ecomm) = Yes, in Azure Release Pipeline

enter image description here

Through Powershell, i want to update the value of $(ecomm) = No

Write-Host "Before update: "$(ecomm)
Write-Host "##vso[task.setvariable variable=ecomm;]No"
Write-Host "After update: "$(ecomm)

But the value is not updating. Can you please help me on this. Thanks in advance.

sanjai
  • 128
  • 1
  • 1
  • 13
  • Hi @sanjai. Glad to know that you have successfully changed the variable. If the answers could give you some help, you could consider [accepting one as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). this can be beneficial to other community members reading this thread. – Kevin Lu-MSFT Nov 26 '20 at 06:30

2 Answers2

1

Repcak is correct.

When you use the logging command to set variables in Powershell, you can only change the value of the variable in Pipeline Run instead of the Release Definition.

To Update the Release definition in Powershell Task, you could try the following Pipeline Settings:

Add two Powershell tasks.

1.The first PowerShell task run the following script:

Write-Host "##vso[task.setvariable variable=ecomm;]No"

This script is used to update the variable values in pipeline run.

2.The second Powershell task run the following script:

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"


$pipeline.variables.ecomm.value = "$(ecomm)"


$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

Note: You also need to set some release options:

  1. Select the option: Allow scripts to access the OAuth token in Releases -> Agent Job

  2. Grant the Edit release pipeline permission to the Role :Project Collection Build Service (OrgName)

Result:

enter image description here

For more detailed information, you could refer to this ticket.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
0

The way you do it, it updates only the variable for the given pipeline run. Azure Pipelines import those variables from the interface you show and then you can override them (the instance of the imported variable). To change the variable in the classic interface you would have to make an API call to AzureDevops.

Repcak
  • 907
  • 1
  • 7
  • 18