'How to get GitHub Actions matrix job name within job?
I'm trying to upload logs separately for each failing job in a matrix. Each set of logs needs to have a name based on the job matrix, otherwise they clobber each other and I can't tell which job the resulting artifact came from.
I tried printing all the contexts:
name: My workflow
on:
pull_request:
types: [opened, synchronize]
jobs:
my-job:
name: OS ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
steps:
- run: echo "${{ toJSON(github) }}"
- run: echo "${{ toJSON(env) }}"
- run: echo "${{ toJSON(job) }}"
- run: echo "${{ toJSON(steps) }}"
- run: echo "${{ toJSON(runner) }}"
- run: echo "${{ toJSON(secrets) }}"
- run: echo "${{ toJSON(strategy) }}"
- run: echo "${{ toJSON(matrix) }}"
- run: echo "${{ toJSON(needs) }}"
- run: echo "${{ toJSON(inputs) }}"
None of these contain the job name as displayed when running the workflow.
Solution 1:[1]
One possible workaround I discovered recently is to use an expression for the matrix, rather than naming each component separately:
jobs:
test:
name: Run ${{ join(matrix.*, ' - ') }} - ${{ github.event_name }}
Solution 2:[2]
It's currently not possible to extract the jobs.<job_id>.name
directly from the Github Context.
There is no native way (yet?) to extract the job_name
from the runner context, only the job_id
(using ${{ github.job }}
).
Therefore, the answer to your (title) question is that it's not possible (yet), using or not a matrix, without a workaround.
Note: You can find some other threads about the same issue here and here
In your case, as you use the matrix
to define the job name, it is actually possible to set the job name the same way in the workflow as env variable (if you wish), doing this workaround:
jobs:
my-job:
name: OS ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
steps:
- run: echo ${{ github.job }} # Will only return the job_id: "my-job"
- run: |
JOB_NAME="OS ${{ matrix.os }}"
echo $JOB_NAME
echo "JOB_NAME=$JOB_NAME" >> $GITHUB_ENV
- name: Print JOB_NAME
run: |
echo "JOB NAME:" ${{ env.JOB_NAME }}
I tested it here if you want to have a look.
That way, the JOB_NAME
env variable can be used in any step (if you want to use it as a log file name), or even be saved as an output for use in sequencial jobs.
When creating your log files, you could add a timestamp
or the workflow run_id
(${{ github.run_id }}
) to the JOB_NAME
as log file name, to identify which workflow produced it, when uploading it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user1712368 |
Solution 2 |