Notification texts go here Contact Us Buy Now!

Obtain job id from a workflow run using contexts

Obtain job id from a workflow run using contexts 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
- name: Get Current Job Log URL uses: Tiryoh/gha-jobid-action@v0 id: jobs with: github_token: ${{ secrets.GITHUB_TOKEN }} job_name: ${{ github.job }} - name: Output Current Job Log URL run: echo ${{ steps.jobs.outputs.html_url }}
Job ID may be obtained with `curl` command (piped to `jq`):
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}/jobs | jq .jobs[].id
where all ENVs `GITHUB_*` should be accessible inside workflow by default PS `jobs` value is a list so multiple IDs may be returned for a matrix strategy, although not pretty this will get the html url to the proper matrix job (in this example, the "name" of each individual matrix job is `matrix.region`):
gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | map(select(.name | contains("${{ matrix.region }}"))) | .[0].html_url'
example:
example-job: name: ${{ matrix.region }} runs-on: ubuntu-latest strategy: matrix: region: ["us-east-1", "us-west-2"] - name: example-matrix-step env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | job_id=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | .[0].id') matrix_job_html_url=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | map(select(.name | contains("${{ matrix.region }}"))) | .[0].html_url') matrix_id=$( sed 's/.*\jobs\///' <<<$matrix_job_html_url) echo "matrix job html url: $matrix_job_html_url" echo "matrix id: $matrix_id" # i doubt this is of any use. in the github response this id is only present as part of the html_url echo "job id: $job_id"
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 }}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.