Set Azure devops Release pipeline variable using REST API

微笑、不失礼 提交于 2020-06-23 11:08:23

问题


I am able to update the variable in the build pipeline using the below json body

        $body = '
{ 
    "definition": {
        "id": 25
    },
    "parameters": "{\"var\":\"value\"}"

}
'

The same json is not working with Release pipeline . Is there any way to pass the variable through same way through release pipeline


回答1:


Set Azure devops Release pipeline variable using REST API

We could use the REST API Definitions - Get to get all the info about this definition in the body, then we could update the body and use the (Definitions - Update) to update the value of the release definition variable from a release pipeline:

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

Following is my test inline powershell scripts:

$url = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.1"

Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named TestVar to its new value 2
$pipeline.variables.TestVar.value = "789"

####****************** 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 $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'TestVar' is updated to" $updatedef.variables.TestVar.value

As test result, the variable TestVar updated to 789:

Update:

But I want to achieve it without updating\changing the definition

The answer is yes. You could use the Releases - Create with request body:

{
  "definitionId": Id,
  "environments": [
    {
      "variables": {
        "TestVar": {
          "value": "xxxx"
        },
        "TestVar2": {
          "value": "xxxx"
        }
      },

    }
   ],
}

For more information refer the post here.

Hope this helps.



来源:https://stackoverflow.com/questions/59249440/set-azure-devops-release-pipeline-variable-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!