vvaltchev
vvaltchev

Reputation: 628

How to get matrix's job's name in the steps part of the yaml file?

I have a pretty simple Azure Pipelines YAML file like:

pool:
  vmImage: 'ubuntu-18.04'

strategy:
  matrix:
    debian:
      img: 'debian:latest'
    fedora:
      img: 'fedora:latest'
    arch:
      img: 'archlinux:latest'
    opensuse:
      img: 'opensuse/leap:latest'


container: $[ variables['img'] ]

steps:
  - script: printenv
    displayName: Dump env
  - script: make
    displayName: Build the project

Which does nothing more than building the same project on each of the four containers enlisted above.

It works, but I'd like to have also a step that uses the name of the job (debian, fedora, etc.) for running a special script.

How can I get access to the job's name in steps? Is there something like $(job)?

Upvotes: 3

Views: 2995

Answers (2)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41765

You have the pre-defined variable $(Agent.JobName) that you can use. for example:

steps:
  - script: echo $(Agent.JobName)
    displayName: echo $(Agent.JobName)

Will give this output:

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 3

Victor
Victor

Reputation: 771

When I encountered a similar situation, I just created another matrix variable to solve the issue.

With your pipeline, it would look like

pool:
  vmImage: 'ubuntu-18.04'

strategy:
  matrix:
    debian:
      job: debian
      img: 'debian:latest'
    fedora:
      job: fedora
      img: 'fedora:latest'
    arch:
      job: arch
      img: 'archlinux:latest'
    opensuse:
      job: opensuse
      img: 'opensuse/leap:latest'


container: $(img)

steps:
  - script: printenv
    displayName: Dump env
  - script: make
    displayName: Build the project
  - script: echo $(job)
    displayName: echo $(job)

Upvotes: 0

Related Questions