Azure devops 如何使用RESTAPI更新Azure DevOps服务器2019中的当前阶段版本变量?

Azure devops 如何使用RESTAPI更新Azure DevOps服务器2019中的当前阶段版本变量?,azure-devops,azure-pipelines,azure-devops-rest-api,azure-devops-server-2019,Azure Devops,Azure Pipelines,Azure Devops Rest Api,Azure Devops Server 2019,我想在stage运行时更新一个release(而不是release定义)作用域变量的值,但每次尝试时都会收到以下错误消息 请问我怎么做 ##[error]Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402898: Stage 'dev' cannot be modified as it is in-progress o

我想在stage运行时更新一个release(而不是release定义)作用域变量的值,但每次尝试时都会收到以下错误消息

请问我怎么做

##[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脚本的变量输出,以填充web应用程序的appsettings.JSON文件中的值。问题在于,变量是在一个作业中创建的,并且没有被替换文件步骤使用,因为我假设它需要是一个发布变量,而不仅仅是任务变量。

您似乎正在使用,根据错误消息VS402898:Stage“dev”无法修改,因为它正在进行或已完成和文档说明。我们只能使用它来更新完整版本

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

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

然后调用RESTAPI来创建发布和更新运行时发布变量

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

API:

请求机构:

{
  "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

不幸的是,设置变量的值并通过RESTAPI触发发布在这里不是一个选项。该值作为发布步骤的一部分生成,然后必须在以后的作业中使用。通过额外的测试,我发现如果我添加或更新一个版本范围的变量,而不是一个范围的变量,它实际上允许我更新版本。我想知道为什么你不能修改当前运行的阶段变量。。。一个可能的解决方法是只将变量添加为发布变量,并使用它……嗨@Benjamin,你的意思是将发布变量的作用域更改为Release而不是stage,然后可以在发布运行时更新发布变量,并在这个发布中使用它吗?你改变剧本了吗?我试过了,但没有得到和你一样的结果。如果我在版本中添加了一个新的变量,它允许我添加它。现在的问题是,尽管我可以在版本中看到新变量,但作业中的后续步骤并没有使用它。我假设这是因为当发布阶段启动并使用任何存在的值时,该阶段会获取发布阶段状态的快照。如果您以任何方式对其进行修改,则在下一阶段/版本之前,它不会使用这些更改。这是我的用例的一个问题。也许我需要问一个不同的问题,以不同的方式解决潜在的问题。。。我真正的问题是替换变量步骤没有使用以前作业中动态创建的变量更新我的appsettings.json文件中的值。Hi@Benjamin,是的,我们可以使用YAML模式创建发布定义,只需将阶段定义为发布,然后在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