首页 > 解决方案 > 如何使用 REST API 更新 Azure DevOps Server 2019 中的当前阶段发布变量?

问题描述

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

请问我该怎么做?

##[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 StackOverflow 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"}

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

标签: azure-devopsazure-pipelinesazure-devops-rest-apiazure-devops-server-2019

解决方案


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

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

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

然后调用其余 API 来创建发布和更新运行时发布变量。

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

接口:

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

请求正文:

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

结果:

在此处输入图像描述

更新1

看来您想在代理作业之间传递变量,我们不能在经典模式下这样做,我们可以在 YAML 模式下使用输出变量,有关详细信息,请参阅此文档博客。

示例代码。

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

推荐阅读