【问题标题】:using variables from variable group in release tasks在发布任务中使用变量组中的变量
【发布时间】:2021-05-10 20:37:05
【问题描述】:

我创建了一个变量组并在那里设置了一个变量 CustomArtifactName 并将范围指定为 Release 这个变量的值是在构建管道中设置的,因此它会为工件生成一个唯一的名称

Write-Host "##vso[task.setvariable variable=CustomArtifactName]$(Get-Date -Format yyyyMMddhhmmss)"

我可以在 PublishPipelineArtifact@1 这样的任务中使用 yaml 构建管道

'drop-pom-''$(CustomArtifactName)'

但是当我在Maven POM File 字段中的发布管道maven 任务中使用以下内容时

$(System.DefaultWorkingDirectory)/cucumber-java-junit5-webapp/drop-pom-$(CustomArtifactName)/s/target/pom.xml

我明白了

##[error]Unhandled: Not found mavenPOMFile:

我也尝试在Download Pipeline Artifact 任务中使用这个变量,但得到了类似的结果

如何在发布任务字段/属性中使用变量组中的变量?根据this msdoc,我们只需要使用$(variableName),但这不起作用。

【问题讨论】:

  • 你能分享你的yaml文件的重要部分吗?否则答案会很模糊。
  • 嗨@user1207289。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。

标签: azure azure-devops azure-pipelines


【解决方案1】:

根据您的描述,当您在构建管道中引用变量组时,您将变量设置为新值。

新值只适用于构建管道,它不会更新变量组中变量的值,所以它仍然会使用发布管道中的旧变量。这可能是此问题的根本原因。

为了解决这个问题,你需要添加一个Powershell任务来运行Rest API(Variablegroups - Update)来更新变量组中的变量。

这是 Powershell 脚本示例:

$token = "PAT"

$url="https://dev.azure.com/{organizationname}/{projectname}/_apis/distributedtask/variablegroups/{variablegroupid}?api-version=5.0-preview.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  
  "variables": {
    "CustomArtifactName": {
      "value": "$(CustomArtifactName)"
    }

  },
  "type": "Vsts",
  "name": "{variablegroupname}",
  "description": "Updated variable group"

}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json

结果:变量组中的变量会根据每次构建更新。

更新:

变量组ID:

您可以在网站中打开变量组并检查 URL。

当Powershell脚本运行成功后,变量的值会传递给变量组中的变量。

【讨论】:

  • 我可以找到大多数字段,但我从哪里获得{variablegroupid}
  • 得到了 id,来自 az cli1。但是当我运行任务时,我在 maven task 中得到了2021-05-12T22:22:04.6137194Z ##[error]Unhandled: Not found mavenPOMFile: D:\a\r1\a\cucumber-java-junit5-webapp\drop-pom-\s\target\pom.xml。 Powershell 脚本运行良好,带有绿色检查。我只更改了脚本 $url="https://dev.azure.com/myOrg/myProject/_apis/distributedtask/variablegroups/1?api-version=5.0-preview.1" 和 ` "name": "var-group"` 中的这一部分,这是我的实际组名
  • 我期待变量在这里被替换 drop-pom-{CustomArtifactName} ,但它和我之前的评论一样空白
  • @user1207289 根据您的描述,Powershell 任务尚未更新变量组。您需要检查 Powershell 脚本中的字段。变量组id可以参考我的更新。
  • 脚本中的$(CustomArtifactName) 似乎没有更新,如果我提供"value": "someValue",我会正确获得powershell 输出。你知道如何在 powershell 中使用$(CustomArtifactName) 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多