Azure 在发布任务中使用变量组中的变量

Azure 在发布任务中使用变量组中的变量,azure,azure-devops,azure-pipelines,Azure,Azure Devops,Azure Pipelines,我已经创建了一个变量组,并在那里设置了一个变量CustomArtifactName,并将范围指定为Release 此变量的值在构建管道中设置,因此它为工件生成唯一的名称 Write Host“##vso[task.setvariable=CustomArtifactName]$(获取日期-格式yyyyMMddhhmmss)” 我可以在PublishPipelineArtifact@1像这样的任务 'drop-pom-'$(CustomArtifactName) 但是当我在maven POM文件字

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

Write Host“##vso[task.setvariable=CustomArtifactName]$(获取日期-格式yyyyMMddhhmmss)”

我可以在
PublishPipelineArtifact@1
像这样的任务

'drop-pom-'$(CustomArtifactName)

但是当我在
maven POM文件
字段中使用下面的发布管道
maven
任务时

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

我明白了

##[错误]未处理:未找到mavenPOMFile:

我还尝试在下载管道工件任务中使用这个变量,但得到了类似的结果


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

根据您的描述,在生成管道中引用变量组时,使用新值设置变量

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

要解决此问题,需要添加Powershell任务来运行Rest API()以更新变量组中的变量

下面是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脚本成功运行时,变量的值将传递给变量组中的变量。

能否共享yaml文件的重要部分?否则答案会很模糊。嗨@user1207289。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。我可以找到大多数字段,但我从哪里获得
{variablegroupid}
?从
az cli
获得的id为
1
。但是当我运行任务时,我在maven任务中得到了
2021-05-12T22:22:04.6137194Z##[错误]未处理:未找到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}
,但它和我以前的文章一样空白comment@user1207289根据您的描述,Powershell任务尚未更新变量组。您需要检查Powershell脚本中的字段。对于变量组id,您可以参考我的更新。脚本中的
$(CustomArtifactName)
似乎没有更新,如果我提供
“value”:“someValue”
,我将正确获得powershell输出。您对如何在powershell中使用
$(CustomArtifactName)
有何想法?