无法从变量组(库)更新变量

时间:2020-05-14 14:21:14

标签: azure azure-devops azure-pipelines

我正在尝试在Azure Devops中创建一个生成管道,该更新管道将更新变量组中的变量。

  • 所以我有我的变量组:
    enter image description here

  • 然后,在我的构建管道中,链接变量组:
    enter image description here

  • 在我的yaml文件中,声明变量:

variables:
- group: Validation
- name: BetaVersionClient
  value: '0.0.0.0'
- name: BetaVersionServer
  value: '0.0.0.0'
  • 稍后在同一文件中设置变量:
- task: VariableSetTask@1
  displayName: 'Set variable'
  inputs:
    VariableName: BetaVersionClient
    Value: '$(AssemblyInfo.AssemblyVersion)'

在工作期间更新变量,但不在包含变量组的库中更新。

我在这里想念什么?如何从yaml文件更新库变量?

2 个答案:

答案 0 :(得分:3)

第一部分将作为假设,但看起来Azure DevOps会为构建执行拍摄快照。这就是为什么您在特定运行中看到此结果,但总体上却看不到的原因。这可能是通过这种方式完成的,因为可能会根据构建定义共享组,因此一个管道可能会更改另一个管道。

如果这是您要执行的操作,则可以使用REST API或CLI。

对于CLI,您有这个command

az pipelines variable-group variable update --group-id
                                            --name
                                            [--detect {false, true}]
                                            [--new-name]
                                            [--org]
                                            [--project]
                                            [--prompt-value {false, true}]
                                            [--secret {false, true}]
                                            [--value]

我不记得代理上是否已经安装了devops扩展,因此您可能对installing it也感兴趣。

另一种选择是使用REST API和此端点:

PUT https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=5.1-preview.1

如何从管道中调用REST API的示例,您可以在这里找到:How to send post build message in azure DevOps YAML pipeline?

编辑

这是在变量组中更改变量的方法:


variables:
  orgName: 'thecodemanual'
  variableGroupId: 3

steps: 
- pwsh: |

    $url = "https://dev.azure.com/$(orgName)/$(System.TeamProject)/_apis/distributedtask/variablegroups/$(variableGroupId)?api-version=5.1-preview.1"
    $variableGroup = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
    Write-Host "Pipeline = $($variableGroup | ConvertTo-Json -Depth 100)"

    $variableGroup.variables.name.value = "fromPipeline"

    $json = $variableGroup | ConvertTo-Json -Depth 100

    Invoke-RestMethod -Method PUT -Uri $url -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Body $json
  name: initial
  env:
    SYSTEM_ACCESSTOKEN: $(system.accesstoken)

如果出现此错误:

{“ $ id”:“ 1”,“ customProperties”:{“描述符”:“ Microsoft.TeamFoundation.ServiceIdentity; a960b165-7bec-46ad-9fa3-a4a754989d4e:Build:4fa6b279-3db9-4cb0-aab8-e06c2ad550b2 “,” IdentityDisplayName“:” DevOps手动构建服务(代码手册)“,”令牌“:” 3“,” RequestedPermissions“:2,” NamespaceId“:” b7e84409-6553-448a-bbb2-af228e07cbeb“},” innerException“ :null,“消息”:“您没有权限对变量组执行此操作。变量组Administrator应该将您添加到Administrator角色。”,“ typeName”:“ Microsoft.VisualStudio.Services.Security.AccessCheckException ,Microsoft.VisualStudio.Services.WebApi“,” typeKey“:” AccessCheckException“,” errorCode“:0,” eventId“:3000}

请检查此Having no permission for updating Variable Group via Azure DevOps REST API from running Pipeline

答案 1 :(得分:0)

该代码无法正常工作,如果您是Invoke-RestMethod的新手,它可能会导致您陷入陷阱。但是,这种方法是正确的。

相关问题