天蓝色管道中的组名变量可以是动态的吗?

Can group name variable be dynamic in azure pipelines?

我在 Azure 上有两个环境。它们之间的一个区别只是来自变量组的环境变量。是否可以为一个管道动态设置组名,而不是设置两个可以映射自己的组变量的管道? 这是我的构建管道的一个例子

trigger:
  - master
  - develop


jobs:

- job: DefineVariableGroups
   steps:
    - script: |
      if [ $(Build.SourceBranch) = 'refs/heads/master' ]; then
        echo "##vso[task.setvariable variable=group_name_variable;isOutput=true]beta_group"
      elif [ $(Build.SourceBranch) = 'refs/heads/develop' ]; then
        echo "##vso[task.setvariable variable=group_name_variable;isOutput=true]alpha_group"
      fi
    name: 'DefineVariableGroupsTask'
  - script: echo $(DefineVariableGroupsTask.group_name_variable)
    name: echovar # that works.

- job: Test
  dependsOn: DefineVariableGroups
  pool:
    vmImage: 'Ubuntu-16.04'
  variables:
    - group: $[ dependencies.DefineVariableGroups.outputs['DefineVariableGroupsTask.group_name_variable'] ]
    # that doesn't work. Error here
steps:
  - script: echo $(mode)
    displayName: 'test'

Can group name variable be dynamic in azure pipelines?

对于给您带来的不便,我们深表歉意。

恐怕目前不支持。所以我们必须在 YAML 管道中声明您要使用的变量组。

早前有其他社区提出了同样的需求,这个需求已经传递给产品组,具体可以查看工单:

门票:Dynamic Variable Groups?

注意:您可以对此反馈进行投票和发表评论。当有足够的社区对此反馈进行投票和评论时,产品团队成员将认真对待此反馈。

希望对您有所帮助。

您可以使用的一种方法或解决方法是使用模板。 我确信最好的选择是动态使用 "group variable" 的可能性,虽然我认为这是不可能的,但这是一种选择。

trigger:
  tags:
    include:
    - develop
    - qa
    - production

variables:
- name: defaultVar
  value:  'value define yml on project repo'
- group: variables-example-outside


resources:
  repositories:
    - repository: yml_reference
      type: github
      ref: refs/heads/yml_reference
      name: enamba/azure_devops
      endpoint: enamba

jobs:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/develop') }}: 
  - template: deploy.example.yml@yml_reference
    parameters:
      GroupVariablesName: variables-example-developer

- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/qa') }}: 
  - template: deploy.example.yml@yml_reference
    parameters:
      GroupVariablesName: variables-example-qa

- ${{ if eq(variables['Build.SourceBranch'], 'refs/tags/production') }}: 
  - template: deploy.example.yml@yml_reference
    parameters:
      GroupVariablesName: variables-example-production

模板内部:

parameters:
  GroupVariablesName: ''

jobs:
- job: deploy_app
  displayName: 'Deploy application'
  variables:
  - group: ${{ parameters.GroupVariablesName }}
  - group: variables-example-inside

  steps:
    - script: |
        echo outsidevar '$(outsidevar)'
        echo defaultVar '$(defaultVar)'
        echo var by tag '$(variable)'

我遇到了类似的情况,想分享一下我的结果。

虽然理论上您可以在 Pipeline 中使用组变量(假设权限设置正确),但我发现 它不是 预期的Azure DevOps 的工作流在 Pipeline.

中使用某种条件将一组加载到另一组上

所以我所做的是:

  1. 对于管道,集中所有任务以生成您将在发布 中使用的工件。请记住,触发器中包含的每个分支都会 运行。
  2. 然后在 Release 中,选择工件并为每个环境创建不同的阶段,并将这些环境链接到适当的变量组。

这样,流水线 可以由提交触发,运行 用于每个单独的分支,然后执行需要 [=21] 中的组变量的任务=]Release 创建工件时触发的部分。因此,总而言之,我通过 Release 中的不同阶段过滤了我的条件,并应用了工作流中那个点所需的任何任务。

我希望这可以帮助人们理解 pipelinerelease 之间的结构和区别,并节省构建 DevOps 的时间 Build/Release 第一次正确的工作流程 :)

我 运行 遇到了类似的问题,这对我有用:

例如,您正在尝试检索名为 dev-01

的组变量
    - name: currentStack
      value: ${{ format('$({0}-{1})', variables['env'], variables['location']) }}