bensgroi
bensgroi

Reputation: 371

Reference a pipeline job name in a YAML template

Is there a way to reference a job parameter's name in an Azure DevOps YAML template? I know that I could pass in the job name as its own string parameter, but I was hoping for something that's a little less clumsy.

template.yml

parameters:
- name: MyJob
  type: job

jobs:
- ${{ parameters.MyJob }}

- job: Job2
  dependsOn: # How to make this depend on MyJob?

azure-pipelines.yml

stages:
- stage: Stage1
  jobs:
  - template: template.yml
    parameters:
      MyJob:
        job: SomeJobName
        steps:
        - script: echo Hello

I tried accessing ${{ parameters.MyJob.name }} but it doesn't appear to exist.

Upvotes: 0

Views: 3899

Answers (2)

tomas
tomas

Reputation: 31

Can't comment, but as you see in previous answer, the property is not "name", but "job". You don't need the foreach then:

parameters:
- name: MyJob
  type: job

jobs:
- ${{ parameters.MyJob }}

- job: Job2
  dependsOn: ${{ parameters.MyJob.job }}
  steps:
  ...

Upvotes: 3

bensgroi
bensgroi

Reputation: 371

I've figured out, though it took a lot of trial and error to get the exact syntax and spacing right. The docs aren't super clear about spacing and expressions, and when to lead with a dash.

parameters:
- name: MyJob
  type: job

jobs:
- ${{ parameters.MyJob }}

- job: Job2
  ${{ each pair in parameters.MyJob }}:
    ${{ if eq(pair.key, 'job') }}:
      dependsOn: ${{ pair.value }}
  steps:
  ...

Upvotes: 2

Related Questions