How to get the check_run_id of GitHub Actions?

I have run my build.cake file tasks in PowerShell script on the windows machine. Do I need to get the check_run_id of GitHub Actions in the workflows .yml file?

i.e. https://github.com/Siddharth/my-project/pull/15/checks?check_run_id=2508655272

I need this id(2508655272) value like this in the workflows .yml file. I have attached my workflow file.

Is there any way to get check_run_id?
Could you suggest a solution for how to achieve this in my workflow .yml file?

1 Like

The check_run_id is not available in any of the contexts, but it’s possible to retrieve them for all jobs of a workflow run via the REST API. However, this may not be available while the job is running. It should be possible to query for it with a dependent job, however.

jobs:
  build:
    ...
  post-build:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v4
        id: query-jobs
        with:
          script: |
            const jobs = await github.rest.actions.listJobsForWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: ${{ github.run_id }},
            });
            console.log(jobs[jobs.length-1].id); // not fail-safe
          result-encoding: string
      - run: |
          echo "${{ format('{0}/checks?check_run_id={1}', github.event.pull_request._links.html.href, steps.query-jobs.outputs.result) }}"
1 Like

Hi @Simran-B ,

Thanks for the update. I have faced the below issue while trying to run the shared code. Could you suggest the solution, how to resolve this issue?

@ElangoRajendran Can you change the following line

const jobs = await github.rest.actions.listJobsForWorkflowRun({

to

const jobs = await github.actions.listJobsForWorkflowRun({

and see if that works? I believe github in github-script is actually the equivalent of octokit.rest as opposed to just octokit.

Hi @Simran-B ,

Thanks for the update. Reported changes have done but this time I have faced another issue as below mentioned.
Could you suggest the solution, how to resolve this issue?

My code:

- uses: actions/github-script@v4
        id: query-jobs
        with:
          script: |
            const jobs = await github.actions.listJobsForWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: ${{ github.run_id }},
            });
            console.log(jobs[jobs.length-1].id); // not fail-safe
          result-encoding: string
      - run: |
          echo "${{ format('{0}/checks?check_run_id={1}', github.event.pull_request._links.html.href, steps.query-jobs.outputs.result) }}"

Error snap:

It took me a while to identify the problem: the result needs to be returned in the script, not console.logged and the relevant payload is at result.data.jobs, not result.jobs.

      - uses: actions/github-script@v4
        id: query-jobs
        with:
          script: |
            const result = await github.actions.listJobsForWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: ${{ github.run_id }},
            });
            return result.data.jobs[result.data.jobs.length-1].id;
      - name: Print check run link
        run: |
          echo "${{ format('{0}/checks?check_run_id={1}', github.event.pull_request._links.html.href, steps.query-jobs.outputs.result) }}"

@ElangoRajendran take a look at Workflow RUN/JOB and Check SUITE/RUN messy relation