如何在Azure Devops中将变量从一个管道传递到另一管道

时间:2020-10-29 14:59:05

标签: azure-devops

我配置了两个用于构建和发布的管道(管道A和管道B),管道A触发了管道B进行部署。

我在管道A上定义了变量,并在管道B上需要它们以用于部署。是否可以在两个管道之间传递它们?

非常感谢任何潜在客户。

3 个答案:

答案 0 :(得分:0)

您可以将其用于该变量组。 Here您有关于变量组的文档。

使用变量组来存储要控制的值,并使其在多个管道中可用。您还可以使用变量组来存储可能需要传递到YAML管道中的机密和其他值。在“库”页面的“管道”下定义和管理变量组。

您需要在两个管道中声明相同的变量:

variables:
- group: my-variable-group

例如,如果您想在一个管道中更新变量并在第二秒内获得此更新值,则可以使用Azure CLI

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

您应从Azure CLI task调用此命令

- task: AzureCLI@2
  displayName: Azure CLI
  inputs:
    azureSubscription: <Name of the Azure Resource Manager service connection>
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      az --version
      az pipelines variable-group --group-id value-of-group-id --name some-name --org your-org --project your-project --value some-value

如果您想Trigger one pipeline after another,则可以使用管道资源:

resources:
  pipelines:
  - pipeline: securitylib   # Name of the pipeline resource
    source: security-lib-ci # Name of the pipeline referenced by the pipeline resource
    trigger: 
      branches:
      - releases/*
      - master

答案 1 :(得分:0)

就像 Krzysztof Madej 指出的那样,变量组将有助于在构建和发布管道之间共享静态值

您需要的是一种将变量从一个管道传递到另一个管道的方法。恐怕要说目前还没有官方的方法。

作为解决方法,您可以更新变量组中变量的值。有多种处理方式,Rest API,powershell,3rd-party扩展。详细方法请参考此问题的答案:How to Increase/Update Variable Group value using Azure Devops Build Definition?

如果要在管道中获取变量的值。由于您使用了logging命令来更新该变量。

您只需要使用Rest API来获取特定的构建日志以获取相关信息。

答案 2 :(得分:0)

如何以天蓝色devops将变量从一个管道传递到另一管道

您可以在发布管道中设置一些默认变量,然后添加一个内联powershell任务来调用REST API(Definitions - Update),以使用来自构建的值更新构建管道中的发布定义变量的值。管道。

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=5.0

现在,我们可以在发布管道中使用多个管道中的值。

请检查my previous thread了解更多详细信息。

注意:不要忘记使用REST API将已更改的变量恢复为默认值,以便下次使用。