Pierre
Pierre

Reputation: 13

In a job template, how can I change the job name depending on the template parameters?

I have a job template:

parameters:
- name: is_release_version
  type: boolean
  default: false

jobs:
- job: 'Build'
  displayName: 'display name'

But I would like the job name to be "Build_Test" or "Build_Release" depending on my parameter.

I tried:

parameters:
- name: is_release_version
  type: boolean
  default: false

variables:
- ${{ if eq(parameters.is_release_version, false) }}:
  - name: name_ext
    value: Test
- ${{ if eq(parameters.is_release_version, true) }}:
  - name: name_ext
    value: Release

jobs:
- job: 'Build_${name_ext}'
  displayName: 'display name'

But "variables" is not expected at this level. I tried passing the name directly as parameter and that works, but I would rather fix the possibilities in the template.

I also tried:

parameters:
- name: is_release_version
  type: boolean
  default: false

jobs:
- ${{ if eq(parameters.is_release_version, false) }}:
  - job: 'Build_Test'
- ${{ if eq(parameters.is_release_version, true) }}:
  - job: 'Build_Prod'
  displayName: 'display name'

But it does not work either. I guess the problem is the indentation of "displayName", and that would mean I would have to duplicate the whole job for the two if.

For now I will pass the name, but I am interested in any method to change the job name in a template.

Upvotes: 1

Views: 2556

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18978

The variable called format you used in job name is not correct. You can try to use Build_${{variables.name_ext}}:

jobs:
- job: 'Build_${{variables.name_ext}}'
  displayName: 'Build_${{variables.name_ext}}'

See the execution result while I define is_release_version value as true:

enter image description here

You can see that it can pass name_ext value into job name.

Upvotes: 3

Related Questions