组名称变量在Azure管道中可以动态吗?

时间:2019-05-20 16:24:44

标签: azure-devops yaml pipeline devops azure-pipelines

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

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'

2 个答案:

答案 0 :(得分:2)

  

组名变量在天青管道中可以动态吗?

很抱歉给您带来不便。

目前恐怕不支持此操作。因此,我们必须在YAML管道中声明要使用的变量组。

其他一些社区先前提出了相同的要求,并且此要求已传递给产品团队,您可以从故障单中查看详细信息:

门票:Dynamic Variable Groups?

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

希望这会有所帮助。

答案 1 :(得分:1)

模板可以使用的一种方法或解决方法。 我确信最好的选择是动态使用“组变量”的可能性,而我相信这是不可能的。

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)'

相关问题