toolkit icon indicating copy to clipboard operation
toolkit copied to clipboard

Getting the job_id with toolkit

Open 8398a7 opened this issue 5 years ago • 2 comments

Describe the enhancement https://docs.github.com/en/rest/reference/actions#get-a-job-for-a-workflow-run I want to use the above API from toolkit. toolkit is currently unable to retrieve the information corresponding to the job_id.

Code Snippet

Now we need to use the list API to identify the job_id in the executed job and combine it with name(context.job, GITHUB_JOB_NAME) to identify the job.

    const resp = await github.actions.listJobsForWorkflowRun({
      owner: context.repo.owner,
      repo: context.repo.repo,
      run_id: context.runId,
    });
    const currentJob = resp?.data.jobs.find(job => job.name === context.job);

This works for simple jobs, but is more complicated when matrix is used. The reason for this is that the name will be different from the context.job.

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-18.04, ubuntu-20.04]
        flavor: [dev, prod]

In the above yaml, the job name is build(ubuntu-18.04, dev), build(ubuntu-18.04, prod), build(ubuntu-20.04, dev), build(ubuntu-20.04, prod). Because context.job is a build job, the method to identify it from the result of the list is complicated.

It is simple to add information like context.jobId.

    const resp = await this.github?.actions.getJobForWorkflowRun({
      owner: context.repo.owner,
      repo: context.repo.repo,
      job_id: context.jobId,
    })
    const currentJob = resp?.data

The job_id information appears to be nowhere to be found, so it may be more than a fix for the toolkit itself.

Additional information

This may not be the issue that opens the issue here. In that case, I'm sorry.

refs: https://github.com/actions/runner/issues/324

8398a7 avatar Aug 07 '20 12:08 8398a7

It looks like you could reference a job by id?

Does this not work?:

const currentJob = resp?.data.jobs.find(job => job.id === context.github.job);

There is the job context, but it lacks information on the job object. So you'd need to get that information and inspect it.

If you need the job id of the current job however, context.github.job provides that.

polarathene avatar May 12 '21 05:05 polarathene

You could use qoomon/actions--context@v4 action to add additional environment variables.It will work also with matrix jobs and within reusable workflows.

jobs:
  example:
    runs-on: ubuntu-latest
    environment: playground
    steps:
      - uses: qoomon/actions--context@v4
      - run: |
          echo "Job Id:         ${GITHUB_JOB_ID}"
          echo "Job Name:       ${GITHUB_JOB_NAME}"
          echo "Job Log URL:    ${GITHUB_JOB_URL}"
          echo "Environment:    ${GITHUB_ENVIRONMENT}"
          echo "Deployment URL: ${GITHUB_DEPLOYMENT_URL}"

This action determines the current job by listing all job of current workflow with current run id and then finds current job by matching the runner name or id.

Source: https://github.com/qoomon/actions--context

qoomon avatar Mar 15 '25 11:03 qoomon

@qoomon's solution is a nice workaround, but it still uses the GitHub runner name, which isn't guaranteed to be unique. I've seen issues with using it for workflows with 10+ jobs. Is there any way to add some context to jobs that we can use to match the IDs we get from the API?

kalverra avatar Apr 17 '25 01:04 kalverra

@kalverra no unfortunately this is the only way, however it was works reliable so far even for professional workload with 100 of jobs.

qoomon avatar Apr 17 '25 20:04 qoomon