Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To set Azure pipeline variable from PowerShell

I am trying to set the Azure pipeline variable value in PowerShell. I have created one variable winversion in the Azure pipeline. Now, in a PowerShell task, I want to assign some values to the winversion variable. My simple question is how can I change the value of an Azure PipeLine variable at run time?

Write-Host "Main value is $winversion"
$env:WINVERSION="abhinav";
Write-Host "Modified value is $env:WINVERSION"
Write-Host "Main value is $(winversion)"

Firstline print: original value is 123
Thirdline Print: Modified value is abhinav
Fourth Line print: 123

I want when I change the value of winversion from "123" to "abhinav" so it actually changes the pipeline variable value to abhinav.

enter image description here

enter image description here

I want to update this variable through Powershell. I am using one PowerShell script calling the API and trying to update its variable but getting the page not found error:-

param(
[string]$winVersion
)
$body = "{ 'definition' : { 'id' :85}
}"
$valueName="Winver"
$definitionId=85
$User=""
$Password=""
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Password)))
$Uri = "https://Muac.visualstudio.com/OSGCXE/_apis/release/releases?api-version=2.0"
$urlDef = "https://Muac.visualstudio.com/OSGCXE/_apis/release/definitions/" + $definitionId + "?api-version=2.0"
$definition = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64authInfo)} -Method Get -Uri $urlDef 

#Write-Host $definition

$definition.variables.$valueName.Value = "$winVersion"
$definitionJson = $definition | ConvertTo-Json -Depth 50 -Compress

#Write-Host (ConvertTo-Json $definition -Depth 100)

$update=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64authInfo)} -Method Put -Uri $urlDef -Body $definitionJson -ContentType "application/json"

#Write-Host "$update"

#$buildresponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $body

#write-Host $buildresponse.status
like image 640
Abhinav Sharma Avatar asked Apr 02 '19 10:04

Abhinav Sharma


People also ask

How do you use pipeline variables in PowerShell?

Windows PowerShell has a unique pipeline variable $_ or $PSItem . Windows PowerShell's ability to pipe entire objects from one command to another is needed to represent that object traversing the pipeline. When one Windows PowerShell cmdlet pipes something to another command, that cmdlet can send an object.

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.

How do you set a variable in PowerShell?

To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it. The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable .


2 Answers

How To set azure pipeline variable from PowerShell

There is a bit of confusion here, you use the variable $winversion in the powershell scripts, but the variable is set testvar in the pipeline variable.

Anyway, no matter we overwrite the pipeline variable value directly like you, or use the script "##vso[task.setvariable variable=testvar;]testvalue" to overwrite it, the overwrite value only work for current build pipeline. When you use the $(winversion) to get the value, it will still pull the value from pipeline variable value. To get the current value, you need use $env:WINVERSION.

Besides, you said:

I want when I change the value of winversion from "123" to "abhinav" so it actually changes the pipeline variable value to abhinav.

If you mean you want change the pipeline variable value on the web portal, you need the REST API (Definitions - Update) to update the value of the build pipeline definition variable from a build task.

There is a very similar thread, you can check the answer for the details:

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

Note:Change the API to the build definitions:

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

Hope this helps.

like image 96
Leo Liu-MSFT Avatar answered Oct 12 '22 06:10

Leo Liu-MSFT


I found this link helpful: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell

This has the complete options of what you can do: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch

You can reuse set variable from task to task, and also job to job. I couldn't find anything on stage to stage.

In summary:

jobs:

# Set an output variable from job A
- job: A
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
    name: setvarStep
  - script: echo $(setvarStep.myOutputVar)
    name: echovar

# Map the variable into job B
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar
like image 34
Ryan Harlich Avatar answered Oct 12 '22 06:10

Ryan Harlich