azure-devops - 如何使用 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 步骤使用,因为我认为它需要是一个发布变量,而不仅仅是任务变量。
解决方案
根据错误消息和文档描述,您似乎正在使用此 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
推荐阅读
- laravel-5 - Laravel 长字符串加密但未解密
- spring - 为什么自动连线在我的示例中不起作用
- c - 为什么我们在 Linux 内核中有 ktime_get 的粗略变体?
- python - 在另一个中搜索 utf-8 字符串时出现问题
- go - Should I use struct tags when names of serialised fields are the same?
- node.js - Route.get() 需要一个回调函数,但得到一个 [object Undefined]
- r - 在一列值中的任意位置过滤多个字符串
- ruby-on-rails - 在不同的表上保存条目有时会失败
- spring-boot - 使用 ElasticSearch 启动 Spring Boot 应用程序时出错
- google-maps - GMapsFX 显示空白地图