Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to parametrize/dynamically set variable group names in Azure DevOps Pipelines YAML?

I have a nested Azure DevOps YAML pipeline:

---
name: Some Release Pipeline

trigger: none

variables:
  - group: "DEV VARIABLE GROUP" # This is the environment variable library

stages:
  - stage: Stage1
    displayName: "Stage 1"
    dependsOn: []
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml

What I would like to do is reuse this release pipeline for any environment. Ideally I would set a pipeline variable "group-name" and then assign it to the group. Something like this:

---
name: Some Release Pipeline

trigger: none

variables:
 - group: "$(group-name)" # This is the environment variable library

stages:
 - stage: Stage1
    displayName: "Stage 1"
    dependsOn: []
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml

However, this doesn't seem to work. Desperately, I've tried a variety of approaches:

  • Using ${{ group-name }}
  • I've tried to pass the group-name as a parameter using:
    jobs:
      - template: /pipelines/pipeline_templates/sometemplate.yml
        parameters:
          variablegroup: $(group-name)

and then setting it in the sometemplate.yml in the job. eg:

jobs:
  - job: Job1
    variables:
      - group: ${{ parameters.variablegroup }}

However, this didn't work neither.

  • I've tried using insertion ({{ insert }}) as suggested here. However, either I don't know how to use insertion properly, or that doesn't work either as I was always encountering some form of validation errors.

According to this, and this, and this, and this it doesn't seem possible.

I am wondering if anyone has found a solution to this yet (other then doing a really messy workaround of calling the DevOps REST API)?

like image 834
sunspots Avatar asked Feb 10 '20 22:02

sunspots


2 Answers

Your intuition of passing the group name as a parameter to the template was correct. I was able to get this working:

Template file passing-variable-groups.yml

parameters:
- name: deploymentVariableLibraries
  type: object
  default:
    dev: ''
    qa: ''
    prod: ''

jobs:
- job: TestDev
  variables:
    - group: ${{parameters.deploymentVariableLibraries.dev}}
  steps:
    - script: echo "$(whichEnvironment)"
- job: TestQa
  variables:
    - group: ${{parameters.deploymentVariableLibraries.qa}}
  steps:
    - script: echo "$(whichEnvironment)"
- job: TestProd
  variables:
    - group: ${{parameters.deploymentVariableLibraries.prod}}
  steps:
    - script: echo "$(whichEnvironment)"

Pipeline

trigger: none

resources:
  repositories:
    - repository: templates
      type: git
      name: c4ePipelineExamples-Templates

jobs:
- template: passing-variable-groups.yml@templates
  parameters:
    deploymentVariableLibraries:
      dev: 'test-dev'
      qa: 'test-qa'
      prod: 'test-prod'

My output from each job is DEV, QA, PROD respectively (the value of whichEnvironment in each test variable library).

like image 183
donlinmi Avatar answered Oct 16 '22 17:10

donlinmi


Following worked for me without any templates

variables:
- group: variables-${{variables['Build.SourceBranchName']}}
like image 43
antonpinchuk Avatar answered Oct 16 '22 15:10

antonpinchuk