skip to Main Content

I have a situation where I have a variable group that will hold the credential (username/pwd) which will be used by my pipeline task.

I want to use one variable group for holding credentials for all environment and I want to add environment names(dev, test. etc) to all the variables so I can use it as a parameter to help the task distinguish which one to use. I do not want to create multiple variable groups for each environment.

My variable group name: CredentialsVariableGroup
Variables:
usernamedev
pwddev
usernametest
pwdtest
usernameuat
pwduat
usernameprod
pwdprod

Now in the yaml, I will pass environment name while calling this. Potential values for environment name – dev, test, prod:

parameters:
- name: environmentName
  type: string

// some other parameters, stages, variable groups, jobs

- task: Sometask
  displayName: Sometaskdisplayname
  inputs:
    username: '$(usernamedev)' 
    password: '$(pwddev)'

I want a way to make the environment name part to be used from parameter so this becomes dynamic and refers the right variable when deploying in the respective environment.

Edit:

Snap of my variable in Library (Pipeline)
enter image description here

My Yaml Pipeline

parameters:
- name: environmentName
  type: string

stages:
- stage: 
  dependsOn:  
  condition: 
  displayName: 
  variables:
  - group: Group1
  - group: Group2
  - group: 'MyVariableGroup'
  - template: vars.yaml
  - ${{ if ne(parameters[''], 'PROD') }}:
    - group: NonProdGroup

  - ${{ if eq(parameters[''], 'PROD') }}:
    - group: ProdGroup

  - group: Group3
  - group: Group4
 
  pool:
    name: Default
  jobs:
  - deployment: 
    displayName: 
    pool:
      name: Default
    environment: 
      name: ${{ parameters.paramname }}
    strategy:
      runOnce:
        deploy:
          steps:
            - download: current
              artifact: drop

            - task: AzureResourceManagerTemplateDeployment@3
              displayName: 'Deploy arm template'
              inputs:
                azureResourceManagerConnection: 
                subscriptionId: ''
                deploymentName: $()
                location: ''
                resourceGroupName: 
                csmFile: ''
                csmParametersFile: ''
                deploymentMode: 'Incremental'

  - job: DeployApiApp
    dependsOn: 
    displayName: 
    steps:
      - task: DownloadBuildArtifacts@0
        displayName: 
        inputs:
          buildType: 'current'
          downloadType: 'single'
          artifactName: 'drop'
          downloadPath: ''

      - task: AzureWebApp@1
        displayName: 
        inputs:
          appType: webApp
          azureSubscription: ''
          appName: ''
          package: ''

  - job: DeployWebApp
    dependsOn: 
    displayName: 
    steps:
      - task: DownloadBuildArtifacts@0
        displayName: 
        inputs:
          buildType: 'current'
          downloadType: 'single'
          artifactName: 'drop'
          downloadPath: ''    

      - task: AzureWebApp@1
        displayName: 
        inputs:
          appType: webApp
          azureSubscription: ''
          appName: ''
          package: ''

  - job: DeployDB
    dependsOn: 
    displayName: 
    steps:
      - download: current
        artifact: 

      - task: SqlAzureDacpacDeployment@1
        displayName: 
        inputs:
          azureSubscription: ''
          ServerName: ''
          DatabaseName: ''
          SqlUserName: ''
          SqlPassword: $[variables['adminpwd${{ upper(parameters.environmentName) }}']] 
          deployType: 'DacpacTask'
          DeploymentAction: 'Publish'
          DacpacFile: ''
          AdditionalArguments: ''
          IpDetectionMethod: ''
          StartIpAddress: ''
          EndIpAddress: ''
          DeleteFirewallRule: 

2

Answers


  1. So first you will want to load the variable group into the pipeline like. I think the best way to achieve this will also be to use a variable template file to store the base names you are looking for.

    variables:
    - group: variableGroupName
    - template: variablesNames.yml
    

    Then for the individual ones you’d reference the parameters like:

    - task: Sometask
      displayName: Sometaskdisplayname
      inputs:
        username: ${{variables.username}}${{parameters.environmentName) 
        password: ${{variables.username}}${{parameters.environmentName) 
    

    You can confirm how this works by downloading the azure-pipeline-expanded.yml file after the run to see the templates fully expand. I would still recommend loading the variable group with the environment name. i.e. separate variable group for each environment. This is due to variable group security as well as pipeline variable scoping.

    The separate variable template file is not needed; however, I have found it helps when scaling.

    Login or Signup to reply.
  2. As commented by @daniel-mann, you probably don’t want to mix variables from different environments in the same variable group.

    If you need to build your variable name dynamically, this syntax should work:

    parameters:
    - name: environmentName
      type: string
    
    steps:
    - pwsh: |
        Write-Host "$(username${{parameters.environmentName}})"
    

    Depending on how you organize your yaml files, runtime variable should work as well:

    parameters:
    - name: environmentName
      type: string
    
    jobs:
    - job: A
      variables:
      - group: my-variable-group-name
      - name: username
        value: $[variables['username${{parameters.environmentName}}']]
      steps:
      - pwsh: |
          Write-Host "$(username)"
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search