Zeppyk
Zeppyk

Reputation: 228

Azure Yaml Pipelines - Dynamic object parameter to template

I would like to trigger a job template with an object as parameter. Unfortunately, even based on the examples I couldn't find a way to do that. I would appreciate if someone could guide me how to achieve this.

What I want to achieve, is to replace the ["DEPLOY", "CONFIG"] part with a dynamic variable:

  - template: job-template.yaml
    parameters:
      jobs: ["DEPLOY", "CONFIG"]

Upvotes: 0

Views: 17862

Answers (2)

USAMA SHAHID
USAMA SHAHID

Reputation: 11

It's possible with some logic. see below

- template: job-template.yaml
  parameters:
    param: ["DEPLOY", "CONFIG"]

and in job-template.yaml file you can define. So every job name will be different

parameters:
 param: []

 jobs:
 - ${{each jobName in parameters.param}}:
   - job: ${{jobName}}
     steps:
       - task: Downl......

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40939

This is not possible. YAML is very limited here and you may read more about this here

Yaml variables have always been string: string mappings.

So for instance you can define paramaters as complex type

Template file

parameters:
- name: 'instances'
  type: object
  default: {}
- name: 'server'
  type: string
  default: ''

steps:
- ${{ each instance in parameters.instances }}:
  - script: echo ${{ parameters.server }}:${{ instance }}

Main file

steps:
- template: template.yaml
  parameters:
    instances: 
    - test1
    - test2
    server: someServer

But you are not able to do it dynamically/programmatically as every output you will create will end up as simple string.

What you can do is to pass as string and then using powershell split that string. But it all depends what you want to run further because you won't be able to simply iterate over yaml structure in that way. All what you can do is to run in in powershell loop and do something, but it can be not enough for you.

Upvotes: 4

Related Questions