Get the latest workflow run from GitHub Actions using the Octokit API
Asked Answered
W

2

5

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!

Wojcik answered 15/10, 2020 at 3:15 Comment(1)
Also asked here: github.community/t/…Wojcik
G
6

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,
});
Gerhardt answered 15/10, 2020 at 8:18 Comment(0)
W
4

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
});
Wojcik answered 15/10, 2020 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.