Skip to content

Commit 7c965ae

Browse files
committed
use Octokit client to download buildx
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent c252a3b commit 7c965ae

File tree

12 files changed

+200
-18
lines changed

12 files changed

+200
-18
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ jobs:
3030
uses: docker/bake-action@v2
3131
with:
3232
targets: test
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3335
-
3436
name: Upload coverage
3537
uses: codecov/codecov-action@v3

__tests__/buildx.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe('install', () => {
221221
])(
222222
'acquires %p of buildx (standalone: %p)',
223223
async (version, standalone) => {
224-
const buildxBin = await buildx.install(version, tmpDir, standalone);
224+
const buildxBin = await buildx.install(version, process.env.GITHUB_TOKEN || '', tmpDir, standalone);
225225
expect(fs.existsSync(buildxBin)).toBe(true);
226226
},
227227
100000

__tests__/github.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import * as github from '../src/github';
33

44
describe('github', () => {
55
it('returns latest buildx GitHub release', async () => {
6-
const release = await github.getRelease('latest');
6+
const release = await github.getLatestRelease(process.env.GITHUB_TOKEN || '');
77
expect(release).not.toBeNull();
88
expect(release?.tag_name).not.toEqual('');
99
});
1010

1111
it('returns v0.2.2 buildx GitHub release', async () => {
12-
const release = await github.getRelease('v0.2.2');
12+
const release = await github.getReleaseTag('v0.2.2', process.env.GITHUB_TOKEN || '');
1313
expect(release).not.toBeNull();
1414
expect(release?.tag_name).toEqual('v0.2.2');
1515
});
16+
17+
it('unknown release', async () => {
18+
await expect(github.getReleaseTag('foo', process.env.GITHUB_TOKEN || '')).rejects.toThrowError(new Error('Cannot get release foo: HttpError: Not Found'));
19+
});
1620
});

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ inputs:
4444
append:
4545
description: 'Append additional nodes to the builder'
4646
required: false
47+
github_token:
48+
description: 'The GitHub token used to create an authenticated client for GitHub API'
49+
default: ${{ github.token }}
50+
required: false
4751

4852
outputs:
4953
name:

dev.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ RUN --mount=type=bind,target=.,rw \
7272
--mount=type=cache,target=/src/node_modules \
7373
--mount=type=bind,from=docker,source=/usr/local/bin/docker,target=/usr/bin/docker \
7474
--mount=type=bind,from=buildx,source=/buildx,target=/usr/libexec/docker/cli-plugins/docker-buildx \
75-
yarn run test --coverageDirectory=/tmp/coverage
75+
--mount=type=secret,id=GITHUB_TOKEN \
76+
GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) yarn run test --coverageDirectory=/tmp/coverage
7677

7778
FROM scratch AS test-coverage
7879
COPY --from=test /tmp/coverage /

docker-bake.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ target "test" {
5050
dockerfile = "dev.Dockerfile"
5151
target = "test-coverage"
5252
output = ["./coverage"]
53+
secret = ["id=GITHUB_TOKEN,env=GITHUB_TOKEN"]
5354
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"dependencies": {
3030
"@actions/core": "^1.10.0",
3131
"@actions/exec": "^1.1.1",
32-
"@actions/http-client": "^2.0.1",
32+
"@actions/github": "^5.1.1",
3333
"@actions/tool-cache": "^2.0.1",
3434
"csv-parse": "^5.3.3",
3535
"js-yaml": "^4.1.0",

src/buildx.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,12 @@ export async function build(inputBuildRef: string, dest: string, standalone: boo
237237
return setPlugin(toolPath, dest);
238238
}
239239

240-
export async function install(inputVersion: string, dest: string, standalone: boolean): Promise<string> {
241-
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
242-
if (!release) {
243-
throw new Error(`Cannot find buildx ${inputVersion} release`);
240+
export async function install(inputVersion: string, githubToken: string, dest: string, standalone: boolean): Promise<string> {
241+
let release: github.Release;
242+
if (inputVersion == 'latest') {
243+
release = await github.getLatestRelease(githubToken);
244+
} else {
245+
release = await github.getReleaseTag(inputVersion, githubToken);
244246
}
245247
core.debug(`Release ${release.tag_name} found`);
246248
const version = release.tag_name.replace(/^v+|v+$/g, '');

src/context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface Inputs {
3636
config: string;
3737
configInline: string;
3838
append: string;
39+
githubToken: string;
3940
}
4041

4142
export async function getInputs(): Promise<Inputs> {
@@ -51,7 +52,8 @@ export async function getInputs(): Promise<Inputs> {
5152
endpoint: core.getInput('endpoint'),
5253
config: core.getInput('config'),
5354
configInline: core.getInput('config-inline'),
54-
append: core.getInput('append')
55+
append: core.getInput('append'),
56+
githubToken: core.getInput('github_token')
5557
};
5658
}
5759

src/github.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
import * as httpm from '@actions/http-client';
1+
import * as github from '@actions/github';
22

3-
export interface GitHubRelease {
3+
export interface Release {
44
id: number;
55
tag_name: string;
66
}
77

8-
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
9-
const url = `https://github.com/docker/buildx/releases/${version}`;
10-
const http: httpm.HttpClient = new httpm.HttpClient('setup-buildx');
11-
return (await http.getJson<GitHubRelease>(url)).result;
8+
const [owner, repo] = 'docker/buildx'.split('/');
9+
10+
export const getReleaseTag = async (tag: string, githubToken: string): Promise<Release> => {
11+
return (
12+
await github
13+
.getOctokit(githubToken)
14+
.rest.repos.getReleaseByTag({
15+
owner,
16+
repo,
17+
tag
18+
})
19+
.catch(error => {
20+
throw new Error(`Cannot get release ${tag}: ${error}`);
21+
})
22+
).data as Release;
23+
};
24+
25+
export const getLatestRelease = async (githubToken: string): Promise<Release> => {
26+
return (
27+
await github
28+
.getOctokit(githubToken)
29+
.rest.repos.getLatestRelease({
30+
owner,
31+
repo
32+
})
33+
.catch(error => {
34+
throw new Error(`Cannot get latest release: ${error}`);
35+
})
36+
).data as Release;
1237
};

0 commit comments

Comments
 (0)