Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can group name variable be dynamic in azure pipelines?

I have two environments on azure. One difference between them is only environment variables that came from variable groups. Is it possible to set up group name dynamically for one pipeline instead of set up two pipelines that can map their own group variables? It is an example of my build pipeline

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'
like image 789
alexche8 Avatar asked Jan 01 '23 23:01

alexche8


2 Answers

One approach or workaround that you can use is with templates. I'm sure that the best option is the possibility to use "group variable" dynamically, while it isn't possible I believe that this is one option.

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

Inside of template:

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

like image 159
enamba Avatar answered Jan 13 '23 11:01

enamba


Can group name variable be dynamic in azure pipelines?

Sorry for any inconvenience.

I am afraid this is not supported at this moment. So we have to declare the variable group you want to consume in a YAML pipeline.

Some other communities raised the same requirement earlier, and this requirement has been passed to product team, you can check the details from the ticket:

Ticket: Dynamic Variable Groups?

Note: You can vote and add your comments for this feedback. When there are enough communities vote and add comments for this feedback, the product team member will take this feedback seriously.

Hope this helps.

like image 35
Leo Liu-MSFT Avatar answered Jan 13 '23 11:01

Leo Liu-MSFT