user3067684
user3067684

Reputation: 1258

Azure pipeline dynamic creation of variables based on parameters

I've have added a azure-pipeline.yml file that calls on templates for auditing, testing, building and deployment.

What I need to work on is piping values/secrets from Azure DevOps Library. The catch is each search is calling strings/secrets different things.

E.g. The node.js services have this service A has a cache connection string as process.env.CACHE_CONNECT service B has a cache connection string as process.env.CACHE_CONNECTION_STRING service C has a cache connection string as process.env.CONNECT_TO_CACHE

What I am planning on doing is passing in a parameter list and then map over this and create variables that can be used in multiple places and also passed into the docker build/deploy steps.

The problem is dynamically creating variables key/value pairs.

Is this possible? Is there a better way to go about this?

name: CD

parameters:
- name: environment
  type: object
  default:
  - foo
  - bar
- name: dotEnvPairs
  type: object
  default:
  - envKey: 'NODE_ENV'
    libraryKey: Production
  - envKey: 'CACHE_STRING'
    libraryKey: 'testMe'

variables:
- group: WebDevelopment   

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:

- ${{ each value in parameters.dotEnvPairs }}:

# The goal is to do something like this:
# <value.envKey> = <variableGroup<value.libraryKey>
  - script: |
      echo Env name is ${{ value.envKey }}
      echo Env value is ${{ value.libraryKey }}
      echo Static key $(testMe)
      echo Dynamic A key %$(value.libraryKey)%
      echo Dynamic B key %${{value.libraryKey}}%
      echo Dynamic C key %$(variables[value.libraryKey]: value)%

I'm automating deployment of services, but I cannot at this stage touch any of the base code.

Upvotes: 0

Views: 1720

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35524

I am afraid that there is no built-in feature can directly meet your requirements.

To create variable based on the parameters value and search the value in variable group, you can use the Variable Set task from extension: Variable Toolbox.

Here is an example:

Variable Group:

enter image description here

Pipeline YAML:

parameters:
- name: environment
  type: object
  default:
  - foo
  - bar
- name: dotEnvPairs
  type: object
  default:
  - envKey: 'NODE_ENV'
    libraryKey: Production
  - envKey: 'CACHE_STRING'
    libraryKey: 'testMe'

variables:
- group: WebDevelopment   


steps:

- ${{ each value in parameters.dotEnvPairs }}:
  - script: |
      echo Env name is ${{ value.envKey }}
      echo Env value is ${{ value.libraryKey }}
      echo Static key $(testMe)
      echo Dynamic A key %$(value.libraryKey)%
      echo Dynamic B key %${{value.libraryKey}}%
      echo Dynamic C key %$(variables[value.libraryKey]: value)%
  - task: VariableSetTask@3
    inputs:
      variableName: '${{ value.envKey }}'
      value: '$(${{ value.libraryKey }})'

- script: | 
    echo $(NODE_ENV)
    echo $(CACHE_STRING)

Result:

enter image description here

Upvotes: 1

Related Questions