Get the latest workflow run from GitHub Actions using the Octokit API

1.8k views Asked by At

I'm using the Octokit API to request a specific workflow run. I can see in the documentation that I can request a specific workflow run by providing a workflow run ID. However I would like to always request the latest workflow run. How do I achieve this if I don't know what the ID will be?

E.g. the value of workflow-run-latest here would always be changing.

octokit.actions.getWorkflowRun({
    owner: owner,
    repo: repo-name,
    run_id: workflow-run-latest,
});

Thanks!

2

There are 2 answers

0
peterevans On BEST ANSWER

When using the list API it looks to me like the latest is always the first in the list. So one option might be to request with per_page set to 1 so you only get the latest.

octokit.actions.listWorkflowRunsForRepo({
  owner: owner,
  repo: repo-name,
  per_page: 1,
});
0
Courtney Bracefield On

Just posting my code snippet that solved my problem here in case it helps anyone in the future :)

var workflowRuns = octokit.actions.listWorkflowRuns({
    owner: owner,
    repo: 'repo-name',
    workflow_file_name: 'file-name.yaml',
    per_page: 1
});