0

I am setting up a new pipeline which i want to have dynamic as possible. First thing which i will need to solve is find mechanism which will allow me to have dynamic variables. I thought about few ways how to do it or how to manage it.

question:

I want to ask if there is any option how to create new pipeline variable during release ? I know that there is possibility to update already created pipeline variables but i am curious if is it possible to create new variable ?

Richard
  • 15
  • 2
  • 9
  • According to the documentation you can define variables as well as set values if this is what you are after? i.e. `Write-Host "##vso[task.setvariable variable=newvariable]newvalue"` https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=powershell – MikeS Jul 19 '19 at 08:38
  • Hi Thank you. I meant it more globally like set variables for whole Release on start of release according to environment without any manual steps. This should work i hope but i need to run same script for example 25times for each stage . – Richard Jul 19 '19 at 08:55
  • The other option would be to use the API and update the release, there is a section for `variables` so I think you could call the API to add a new variable. Not any examples though in the documentation: https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/update%20release%20resource?view=azure-devops-server-rest-5.0#release – MikeS Jul 19 '19 at 09:12

2 Answers2

1

It is not supported to add new variable when start release.

The workaround is that, you can define the dynamic variables in a variable and add a PowerShell task (before all tasks) to create variables dynamic. After that, you can update this variable's value when start release.

For example:

enter image description here

enter image description here Script:

Write-host "$(dyntest)"
$varObject = ConvertFrom-Json –InputObject "$(dyntest)"
$varObject.PSObject.Properties | foreach-object { $vn=$_.name;$vv=$_.value; Write-Host "##vso[task.setvariable variable=$vn]$vv"}

Simple way to create release and update variable through REST API:

enter image description here

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
0

Here is the answer. This api call can create new pipeline variables on already started release.

$urlget = "https://vsrm.dev.azure.com/{collection}/{project}/_apis/release/releases/$($env:RELEASE_RELEASEID)?api-version=5.0"

$getdef = Invoke-RestMethod -Method GET -UseDefaultCredentials -ContentType application/json -Uri $urlget -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}

$blockcvalue =@"
    {
      "value": "NEWVARIABLEVALUETEST"
    }
"@

$getdef.variables | add-member -Name "NEWVARIABLENAMETEST" -value (Convertfrom-Json $blockcvalue) -MemberType NoteProperty -Force -PassThru

$getdef = $getdef | ConvertTo-Json -Depth 100
$getdef | clip

$urlput = "https://vsrm.dev.azure.com/{collection}/{project}/_apis/release/releases/$($env:RELEASE_RELEASEID)?api-version=5.0"
$putdef = Invoke-RestMethod -Uri $urlput -Method PUT -Body $getdef -ContentType "application/json" -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Richard
  • 15
  • 2
  • 9