Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the job_id with toolkit #550

Open
8398a7 opened this issue Aug 7, 2020 · 1 comment
Open

Getting the job_id with toolkit #550

8398a7 opened this issue Aug 7, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@8398a7
Copy link
Contributor

8398a7 commented Aug 7, 2020

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: actions/runner#324

@polarathene
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants