如何使用 REST API 更新 Azure DevOps Server 2019 中的当前阶段版本变量?

How to update current stage release variable in Azure DevOps Server 2019 using the REST API?

我想在阶段 运行 时更新版本(不是版本定义)范围变量的值,但每次我尝试时都会收到以下错误消息。

请问我该怎么做?

##[error]Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402898: Stage 'dev' cannot be modified as it is 
in-progress or completed. Changes were detected in the following properties: 
'Variables'","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Mic
rosoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}
At line:263 char:5

+     Invoke-RestMethod -Uri $url -Method Put -ContentType "application ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 

   eption

    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
2020-11-03T06:45:48.2096034Z ##[error]PowerShell exited with code '1'.

示例脚本:

$url = "{0}{1}/_apis/Release/releases/{2}?api-version=5.0" -f $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, $env:SYSTEM_TEAMPROJECTID, $env:RELEASE_RELEASEID

$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}

# Update an existing scoped variable named TestVar
# Just for Whosebug we assume that the currently running stage in Azure DevOps Server
# matches this environment[0] below
$pipeline.environments[0].variables.TestVar.value = "NewValue"

$body = $pipeline | ConvertTo-Json -Depth 99

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

以防万一还有其他解决方案...我想要实现的是稍后在单独的 'Substitute Files' JSON 中使用 PowerShell 脚本的变量输出将值填充到 Web 应用程序的 appsettings.json 文件中的转换步骤。问题是该变量是在一个作业中创建的,并且没有被替换文件步骤使用,因为我认为它需要是一个发布变量,而不仅仅是任务变量。

根据错误信息VS402898: Stage 'dev' cannot be modified as it is in-progress or completed和文档描述,您似乎正在使用this REST API。我们只能用它来更新完整版本

作为解决方法,我们可以在创建发布时更改变量值。

我的测试步骤:创建一个发布变量->将其命名为test并将值设置为55->启用选项发布时可设置->添加任务power shell 打印变量 test

然后调用其余部分 API 创建发布并更新运行时发布变量。

注意:不会改变发布定义变量。

API:

POST https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0

请求正文:

{
  "definitionId": {Release definition ID},
  "artifacts": [
  ],
  "variables": {
    "test": {
      "value": "99"
    }
  }
}

结果:

更新1

看来你想在代理作业之间传递变量,我们在经典模式下不能这样做,我们可以在 YAML 模式下使用输出变量,请参阅doc and blog了解更多详情。

示例代码。

jobs:
- job: A
  steps:
  - task: MyTask@1  # this step generates the output variable
    name: ProduceVar  # because we're going to depend on it, we need to name the step
- job: B
  dependsOn: A
  variables:
    # map the output variable from A into this job
    varFromA: $[ dependencies.A.outputs['ProduceVar.MyVar'] ]
  steps:
  - script: echo $(varFromA) # this step uses the mapped-in variable