Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain job id from a workflow run using contexts

I have a problem, when I want to get the job id using the GitHub context in yaml file, it responds with a String:

  - name: Test
    run: |
      sendEmail ${{github.job]}

I obtain this response:

sendEmail Job_Test

In the GitHub documentation for the API, it says the following, which is an Integer field: enter image description here

But, in the documentation of the contexts it says that it is String: enter image description here

My question is, what is it or how could I obtain the context to obtain the job id, the integer value?

like image 307
Oscar P Avatar asked Sep 10 '25 18:09

Oscar P


2 Answers

There is no straight way to do that - those are not the same values.

The only solution I know is to:

  1. read github.job getting the key
  2. get list of jobs for a workflow using API: /repos/{owner}/{repo}/actions/runs/{run_id}/jobs
  3. find the job by the name and get id as int value
like image 134
Grzegorz Krukowski Avatar answered Sep 12 '25 14:09

Grzegorz Krukowski


This information is not available in the github context level, however you can just filter out it, using a unique job information, like the runner.name. In this way you can get job id for any case, including matrix.

  - name: Get Job ID from GH API
    id: get-job-id
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    run: |
      jobs=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs)
      job_id=$(echo $jobs | jq -r '.jobs[] | select(.runner_name=="${{ runner.name }}") | .id')
      echo "job_id=$job_id" >> $GITHUB_OUTPUT

  - name: Display Job ID
    run: |
      echo Job ID: ${{ steps.get-job-id.outputs.job_id }}
      echo My full job URL is ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.get-job-id.outputs.job_id }}
like image 31
Lucio Veloso Guimarães Avatar answered Sep 12 '25 15:09

Lucio Veloso Guimarães