Skip to content

Commit

Permalink
Use GITHUB_API_URL for Octokit clients (#100)
Browse files Browse the repository at this point in the history
API calls on GHES can be supported by using the GITHUB_API_URL
environment variable. However artifact client doesn't support GHES so
outputs-from feature can't be used on GHES yet.

Closes #99
  • Loading branch information
yogeshlonkar authored May 23, 2024
1 parent 7635a6a commit b12a68f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ This action has output named `outputs` which is JSON string generated by parsing
echo ${{ fromJSON(steps.wait-for-jobs.outputs.outputs).out2 }}
```

### Github Enterprise Server

`GITHUB_API_URL` environment variable is used for Octokit client to fetch job status, cause of this the action can be used on GitHub Enterprise Server. However artifact client doesn't support GHES so `outputs-from` feature can't be used on GHES yet.

### ⚠️ Billing duration

The purpose of this action is to reduce Duration of workflow by prestarting dependee jobs. But waiting for jobs in a step/ action will increase Run time, Billable time.
If the ttl is set more than 15 it will be overridden to 15 minutes. If depdencies requires more than 15 minutes to finish perhaps the dependee job steps should be split in separate jobs not prestart together but if you still wish to wait more than that you can set `no-max-ttl: 'true'`
[jobs-for-a-workflow-run-attempt]: https://docs.github.com/en/rest/actions/workflow-jobs#list-jobs-for-a-workflow-run-attempt
[with-wait-for-jobs-run]: https://github.com/yogeshlonkar/wait-for-jobs/actions/runs/3077494840
[without-wait-for-jobs-run]: https://github.com/yogeshlonkar/wait-for-jobs/actions/runs/3077494839
4 changes: 2 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion lib/github/jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import jobsResponse from "./__fixtures__/jobs.json";
import { getCurrentJobs } from "./jobs";

const listJobsForWorkflowRunAttempt = jest.fn();
const octokit = jest.fn();

jest.mock("@octokit/rest", () => ({
Octokit: {
Expand All @@ -21,6 +22,7 @@ beforeEach(() => {
process.env.GITHUB_REPOSITORY = "yogeshlonkar/wait-for-job";
process.env.GITHUB_RUN_ID = "10";
process.env.GITHUB_RUN_ATTEMPT = "1";
process.env.GITHUB_API_URL = "https://api.github.com";
});

afterEach(() => {
Expand All @@ -42,11 +44,19 @@ describe("getCurrentJobs", () => {
test("returns Run", async () => {
listJobsForWorkflowRunAttempt.mockResolvedValue(jobsResponse);
await expect(getCurrentJobs("some-auth")).resolves.toEqual(jobsResponse.data);
expect(listJobsForWorkflowRunAttempt).toBeCalledWith({
expect(listJobsForWorkflowRunAttempt).toHaveBeenCalledWith({
attempt_number: 1,
owner: "yogeshlonkar",
repo: "wait-for-job",
run_id: 10
});
});

test("uses Octokit with token and baseUrl", async () => {
getCurrentJobs("some-auth-2");
expect(octokit).not.toHaveBeenCalledWith({
auth: "some-auth-2",
baseUrl: "https://api.github.com"
});
});
});
4 changes: 2 additions & 2 deletions lib/github/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export type Job = components["schemas"]["job"];
*/
export async function getCurrentJobs(token: string): Promise<Jobs> {
// prettier-ignore
const { runId: run_id, runAttempt: attempt_number, repo: { owner, repo } } = new Context();
const { runId: run_id, runAttempt: attempt_number, repo: { owner, repo }, apiUrl } = new Context();
// prettier-ignore
const { actions: { listJobsForWorkflowRunAttempt } } = new MyOctokit({ auth: token });
const { actions: { listJobsForWorkflowRunAttempt } } = new MyOctokit({ auth: token, baseUrl: apiUrl });
debug(`fetching jobs for /repos/${owner}/${repo}/actions/runs/${run_id}/attempts/${attempt_number}/jobs`);
const { status, data } = await listJobsForWorkflowRunAttempt({
attempt_number,
Expand Down
2 changes: 1 addition & 1 deletion lib/miscellaneous/sleep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ jest.mock("@actions/core");
test("sleep debug logs label", async () => {
const controller = new AbortController();
await expect(sleep(2, controller, "label-1")).resolves.toBeUndefined();
expect(debug).toBeCalledWith("label-1 done");
expect(debug).toHaveBeenCalledWith("label-1 done");
});

0 comments on commit b12a68f

Please sign in to comment.