buildx: split module

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-01-31 21:13:39 +01:00
parent 388280c282
commit cc48ecede1
11 changed files with 428 additions and 309 deletions

View File

@@ -21,9 +21,11 @@ import * as rimraf from 'rimraf';
import * as semver from 'semver';
import {BuildKit} from '../src/buildkit';
import {Builder, BuilderInfo} from '../src/builder';
import {Builder} from '../src/buildx/builder';
import {Context} from '../src/context';
import {BuilderInfo} from '../src/types/builder';
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);

View File

@@ -18,8 +18,12 @@ import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import {Builder, BuilderInfo} from '../src/builder';
import {Context} from '../src/context';
import {Builder} from '../../src/buildx/builder';
import {Context} from '../../src/context';
import {BuilderInfo} from '../../src/types/builder';
const fixturesDir = path.join(__dirname, '..', 'fixtures');
beforeEach(() => {
jest.clearAllMocks();
@@ -196,6 +200,6 @@ describe('parseInspect', () => {
}
]
])('given %p', async (inspectFile, expected) => {
expect(await Builder.parseInspect(fs.readFileSync(path.join(__dirname, 'fixtures', inspectFile)).toString())).toEqual(expected);
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
});
});

View File

@@ -0,0 +1,181 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {describe, expect, it, jest, test, beforeEach, afterEach} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as semver from 'semver';
import * as exec from '@actions/exec';
import {Buildx} from '../../src/buildx/buildx';
import {Context} from '../../src/context';
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
return tmpName;
});
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
rimraf.sync(tmpDir);
});
describe('getRelease', () => {
it('returns latest buildx GitHub release', async () => {
const release = await Buildx.getRelease('latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.10.1 buildx GitHub release', async () => {
const release = await Buildx.getRelease('v0.10.1');
expect(release).not.toBeNull();
expect(release?.id).toEqual(90346950);
expect(release?.tag_name).toEqual('v0.10.1');
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.10.1');
});
it('returns v0.2.2 buildx GitHub release', async () => {
const release = await Buildx.getRelease('v0.2.2');
expect(release).not.toBeNull();
expect(release?.id).toEqual(17671545);
expect(release?.tag_name).toEqual('v0.2.2');
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.2.2');
});
it('unknown release', async () => {
await expect(Buildx.getRelease('foo')).rejects.toThrowError(new Error('Cannot find Buildx release foo in https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json'));
});
});
describe('isAvailable', () => {
it('docker cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
buildx.isAvailable().catch(() => {
// noop
});
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});
it('standalone', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.isAvailable().catch(() => {
// noop
});
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
silent: true,
ignoreReturnCode: true
});
});
});
describe('printInspect', () => {
it('prints builder2 instance', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.printInspect('builder2').catch(() => {
// noop
});
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['inspect', 'builder2'], {
failOnStdErr: false
});
});
});
describe('printVersion', () => {
it('docker cli', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx', 'version'], {
failOnStdErr: false
});
});
it('standalone', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['version'], {
failOnStdErr: false
});
});
});
describe('version', () => {
it('valid', async () => {
const buildx = new Buildx({
context: new Context()
});
expect(semver.valid(await buildx.version)).not.toBeUndefined();
});
});
describe('parseVersion', () => {
test.each([
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
])('given %p', async (stdout, expected) => {
expect(Buildx.parseVersion(stdout)).toEqual(expected);
});
});
describe('versionSatisfies', () => {
test.each([
['0.4.1', '>=0.3.2', true],
['bda4882a65349ca359216b135896bddc1d92461c', '>0.1.0', false],
['f117971', '>0.6.0', true]
])('given %p', async (version, range, expected) => {
const buildx = new Buildx({
context: new Context()
});
expect(await buildx.versionSatisfies(range, version)).toBe(expected);
});
});

View File

@@ -14,16 +14,16 @@
* limitations under the License.
*/
import {describe, expect, it, jest, test, beforeEach, afterEach} from '@jest/globals';
import {afterEach, beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as semver from 'semver';
import * as exec from '@actions/exec';
import {Buildx} from '../src/buildx';
import {Context} from '../src/context';
import {Context} from '../../src/context';
import {Buildx} from '../../src/buildx/buildx';
import {Inputs} from '../../src/buildx/inputs';
const fixturesDir = path.join(__dirname, '..', 'fixtures');
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);
const metadata = `{
@@ -49,122 +49,15 @@ afterEach(() => {
rimraf.sync(tmpDir);
});
describe('isAvailable', () => {
it('docker cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
buildx.isAvailable().catch(() => {
// noop
});
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});
it('standalone', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.isAvailable().catch(() => {
// noop
});
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
silent: true,
ignoreReturnCode: true
});
});
});
describe('printInspect', () => {
it('prints builder2 instance', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.printInspect('builder2').catch(() => {
// noop
});
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['inspect', 'builder2'], {
failOnStdErr: false
});
});
});
describe('printVersion', () => {
it('docker cli', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx', 'version'], {
failOnStdErr: false
});
});
it('standalone', () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['version'], {
failOnStdErr: false
});
});
});
describe('version', () => {
it('valid', async () => {
const buildx = new Buildx({
context: new Context()
});
expect(semver.valid(await buildx.version)).not.toBeUndefined();
});
});
describe('parseVersion', () => {
test.each([
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'],
['github.com/docker/buildx f117971 f11797113e5a9b86bd976329c5dbb8a8bfdfadfa', 'f117971']
])('given %p', async (stdout, expected) => {
expect(Buildx.parseVersion(stdout)).toEqual(expected);
});
});
describe('versionSatisfies', () => {
test.each([
['0.4.1', '>=0.3.2', true],
['bda4882a65349ca359216b135896bddc1d92461c', '>0.1.0', false],
['f117971', '>0.6.0', true]
])('given %p', async (version, range, expected) => {
const buildx = new Buildx({
context: new Context()
});
expect(await buildx.versionSatisfies(range, version)).toBe(expected);
});
});
describe('getBuildImageID', () => {
it('matches', async () => {
const buildx = new Buildx({
context: new Context()
});
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = buildx.getBuildImageIDFilePath();
const imageIDFile = buildx.inputs.getBuildImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID);
const expected = buildx.getBuildImageID();
const expected = buildx.inputs.getBuildImageID();
expect(expected).toEqual(imageID);
});
});
@@ -174,9 +67,9 @@ describe('getBuildMetadata', () => {
const buildx = new Buildx({
context: new Context()
});
const metadataFile = buildx.getBuildMetadataFilePath();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.getBuildMetadata();
const expected = buildx.inputs.getBuildMetadata();
expect(expected).toEqual(metadata);
});
});
@@ -186,9 +79,9 @@ describe('getDigest', () => {
const buildx = new Buildx({
context: new Context()
});
const metadataFile = buildx.getBuildMetadataFilePath();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.getDigest();
const expected = buildx.inputs.getDigest();
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
});
});
@@ -238,7 +131,7 @@ describe('getProvenanceInput', () => {
const buildx = new Buildx({
context: new Context()
});
expect(buildx.getProvenanceInput('provenance')).toEqual(expected);
expect(buildx.inputs.getProvenanceInput('provenance')).toEqual(expected);
});
});
@@ -269,35 +162,7 @@ describe('getProvenanceAttrs', () => {
const buildx = new Buildx({
context: new Context()
});
expect(buildx.getProvenanceAttrs(input)).toEqual(expected);
});
});
describe('getRelease', () => {
it('returns latest buildx GitHub release', async () => {
const release = await Buildx.getRelease('latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.10.1 buildx GitHub release', async () => {
const release = await Buildx.getRelease('v0.10.1');
expect(release).not.toBeNull();
expect(release?.id).toEqual(90346950);
expect(release?.tag_name).toEqual('v0.10.1');
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.10.1');
});
it('returns v0.2.2 buildx GitHub release', async () => {
const release = await Buildx.getRelease('v0.2.2');
expect(release).not.toBeNull();
expect(release?.id).toEqual(17671545);
expect(release?.tag_name).toEqual('v0.2.2');
expect(release?.html_url).toEqual('https://github.com/docker/buildx/releases/tag/v0.2.2');
});
it('unknown release', async () => {
await expect(Buildx.getRelease('foo')).rejects.toThrowError(new Error('Cannot find Buildx release foo in https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json'));
expect(buildx.inputs.getProvenanceAttrs(input)).toEqual(expected);
});
});
@@ -309,7 +174,7 @@ describe('generateBuildSecret', () => {
['aaaaaaaa', false, '', '', new Error('aaaaaaaa is not a valid secret')],
['aaaaaaaa=', false, '', '', new Error('aaaaaaaa= is not a valid secret')],
['=bbbbbbb', false, '', '', new Error('=bbbbbbb is not a valid secret')],
[`foo=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`, true, 'foo', 'bar', null],
[`foo=${path.join(fixturesDir, 'secret.txt').split(path.sep).join(path.posix.sep)}`, true, 'foo', 'bar', null],
[`notfound=secret`, true, '', '', new Error('secret file secret not found')]
])('given %p key and %p secret', async (kvp: string, file: boolean, exKey: string, exValue: string, error: Error) => {
try {
@@ -318,9 +183,9 @@ describe('generateBuildSecret', () => {
});
let secret: string;
if (file) {
secret = buildx.generateBuildSecretFile(kvp);
secret = buildx.inputs.generateBuildSecretFile(kvp);
} else {
secret = buildx.generateBuildSecretString(kvp);
secret = buildx.inputs.generateBuildSecretString(kvp);
}
expect(secret).toEqual(`id=${exKey},src=${tmpName}`);
expect(fs.readFileSync(tmpName, 'utf-8')).toEqual(exValue);
@@ -343,7 +208,7 @@ describe('hasLocalExporter', () => {
[['" type= local" , dest=./release-out'], true],
[['.'], true]
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
expect(Buildx.hasLocalExporter(exporters)).toEqual(expected);
expect(Inputs.hasLocalExporter(exporters)).toEqual(expected);
});
});
@@ -359,7 +224,7 @@ describe('hasTarExporter', () => {
[['" type= local" , dest=./release-out'], false],
[['.'], false]
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
expect(Buildx.hasTarExporter(exporters)).toEqual(expected);
expect(Inputs.hasTarExporter(exporters)).toEqual(expected);
});
});
@@ -375,7 +240,7 @@ describe('hasDockerExporter', () => {
[['" type= local" , dest=./release-out'], false, undefined],
[['.'], true, true],
])('given %p returns %p', async (exporters: Array<string>, expected: boolean, load: boolean | undefined) => {
expect(Buildx.hasDockerExporter(exporters, load)).toEqual(expected);
expect(Inputs.hasDockerExporter(exporters, load)).toEqual(expected);
});
});
@@ -385,7 +250,7 @@ describe('hasGitAuthTokenSecret', () => {
[['A_SECRET=abcdef0123456789'], false],
[['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'], true],
])('given %p secret', async (kvp: Array<string>, expected: boolean) => {
expect(Buildx.hasGitAuthTokenSecret(kvp)).toBe(expected);
expect(Inputs.hasGitAuthTokenSecret(kvp)).toBe(expected);
});
});

View File

@@ -20,8 +20,10 @@ import * as exec from '@actions/exec';
import * as semver from 'semver';
import {Context} from './context';
import {Buildx} from './buildx';
import {Builder, BuilderInfo} from './builder';
import {Buildx} from './buildx/buildx';
import {Builder} from './buildx/builder';
import {BuilderInfo} from './types/builder';
export interface BuildKitOpts {
context: Context;
@@ -42,14 +44,6 @@ export class BuildKit {
});
}
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,
buildx: this.buildx
});
return builder.inspect(name);
}
public async getVersion(builderName: string): Promise<string | undefined> {
const builderInfo = await this.getBuilderInfo(builderName);
if (builderInfo.nodes.length == 0) {
@@ -137,4 +131,12 @@ export class BuildKit {
fs.writeFileSync(configFile, s);
return configFile;
}
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,
buildx: this.buildx
});
return builder.inspect(name);
}
}

View File

@@ -17,24 +17,9 @@
import * as exec from '@actions/exec';
import {Buildx} from './buildx';
import {Context} from './context';
import {Context} from '../context';
export interface BuilderInfo {
name?: string;
driver?: string;
lastActivity?: Date;
nodes: NodeInfo[];
}
export interface NodeInfo {
name?: string;
endpoint?: string;
driverOpts?: Array<string>;
status?: string;
buildkitdFlags?: string;
buildkitVersion?: string;
platforms?: string;
}
import {BuilderInfo, NodeInfo} from '../types/builder';
export interface BuilderOpts {
context: Context;

137
src/buildx/buildx.ts Normal file
View File

@@ -0,0 +1,137 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import * as semver from 'semver';
import {Docker} from '../docker';
import {Context} from '../context';
import {Inputs} from './inputs';
import {GitHubRelease} from '../types/github';
export interface BuildxOpts {
context: Context;
standalone?: boolean;
}
export class Buildx {
private readonly context: Context;
private _version: string | undefined;
public readonly inputs: Inputs;
public readonly standalone: boolean;
constructor(opts: BuildxOpts) {
this.context = opts.context;
this.inputs = new Inputs(this.context);
this.standalone = opts?.standalone ?? !Docker.isAvailable();
}
public static async getRelease(version: string): Promise<GitHubRelease> {
// FIXME: Use https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json when repo public
const url = `https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
const resp: httpm.HttpClientResponse = await http.get(url);
const body = await resp.readBody();
const statusCode = resp.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`);
}
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
if (!releases[version]) {
throw new Error(`Cannot find Buildx release ${version} in ${url}`);
}
return releases[version];
}
public getCommand(args: Array<string>) {
return {
command: this.standalone ? 'buildx' : 'docker',
args: this.standalone ? args : ['buildx', ...args]
};
}
public async isAvailable(): Promise<boolean> {
const cmd = this.getCommand([]);
return await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}
public async printInspect(name: string): Promise<void> {
const cmd = this.getCommand(['inspect', name]);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
get version() {
return (async () => {
if (!this._version) {
const cmd = this.getCommand(['version']);
this._version = await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return Buildx.parseVersion(res.stdout.trim());
});
}
return this._version;
})();
}
public async printVersion() {
const cmd = this.getCommand(['version']);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
public static parseVersion(stdout: string): string {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse buildx version`);
}
return matches[1];
}
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version);
if (!ver) {
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
}
}

View File

@@ -17,111 +17,15 @@
import fs from 'fs';
import path from 'path';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as httpm from '@actions/http-client';
import {parse} from 'csv-parse/sync';
import * as semver from 'semver';
import {Docker} from './docker';
import {Context} from './context';
import {Context} from '../context';
export interface GitHubRelease {
id: number;
tag_name: string;
html_url: string;
assets: Array<string>;
}
export interface BuildxOpts {
context: Context;
standalone?: boolean;
}
export class Buildx {
export class Inputs {
private readonly context: Context;
private _version: string | undefined;
public standalone: boolean;
constructor(opts: BuildxOpts) {
this.context = opts.context;
this.standalone = opts?.standalone ?? !Docker.isAvailable();
}
public getCommand(args: Array<string>) {
return {
command: this.standalone ? 'buildx' : 'docker',
args: this.standalone ? args : ['buildx', ...args]
};
}
public async isAvailable(): Promise<boolean> {
const cmd = this.getCommand([]);
return await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}
public async printInspect(name: string): Promise<void> {
const cmd = this.getCommand(['inspect', name]);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
get version() {
return (async () => {
if (!this._version) {
const cmd = this.getCommand(['version']);
this._version = await exec
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return Buildx.parseVersion(res.stdout.trim());
});
}
return this._version;
})();
}
public async printVersion() {
const cmd = this.getCommand(['version']);
await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
public static parseVersion(stdout: string): string {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse buildx version`);
}
return matches[1];
}
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version);
if (!ver) {
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
constructor(context: Context) {
this.context = context;
}
public getBuildImageIDFilePath(): string {
@@ -228,33 +132,16 @@ export class Buildx {
return `${input},builder-id=${this.context.provenanceBuilderID}`;
}
public static async getRelease(version: string): Promise<GitHubRelease> {
// FIXME: Use https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json when repo public
const url = `https://raw.githubusercontent.com/docker/buildx/master/.github/releases.json`;
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
const resp: httpm.HttpClientResponse = await http.get(url);
const body = await resp.readBody();
const statusCode = resp.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`);
}
const releases = <Record<string, GitHubRelease>>JSON.parse(body);
if (!releases[version]) {
throw new Error(`Cannot find Buildx release ${version} in ${url}`);
}
return releases[version];
}
public static hasLocalExporter(exporters: string[]): boolean {
return Buildx.hasExporterType('local', exporters);
return Inputs.hasExporterType('local', exporters);
}
public static hasTarExporter(exporters: string[]): boolean {
return Buildx.hasExporterType('tar', exporters);
return Inputs.hasExporterType('tar', exporters);
}
public static hasDockerExporter(exporters: string[], load?: boolean): boolean {
return load ?? Buildx.hasExporterType('docker', exporters);
return load ?? Inputs.hasExporterType('docker', exporters);
}
public static hasExporterType(name: string, exporters: string[]): boolean {

View File

@@ -15,19 +15,21 @@
*/
import {Context} from './context';
import {Buildx} from './buildx';
import {Buildx} from './buildx/buildx';
import {BuildKit} from './buildkit';
import {GitHub} from './github';
export {Builder, BuilderOpts, BuilderInfo, NodeInfo} from './builder';
export {Builder, BuilderOpts} from './buildx/builder';
export {BuildKit, BuildKitOpts} from './buildkit';
export {Buildx, BuildxOpts} from './buildx';
export {Buildx, BuildxOpts} from './buildx/buildx';
export {Context} from './context';
export {Docker} from './docker';
export {Git} from './git';
export {GitHub, GitHubRepo, GitHubActionsRuntimeToken} from './github';
export {Util} from './util';
export {BuilderInfo, NodeInfo} from './types/builder';
export interface ToolkitOpts {
/**
* GitHub token to use for authentication.

32
src/types/builder.ts Normal file
View File

@@ -0,0 +1,32 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface BuilderInfo {
name?: string;
driver?: string;
lastActivity?: Date;
nodes: NodeInfo[];
}
export interface NodeInfo {
name?: string;
endpoint?: string;
driverOpts?: Array<string>;
status?: string;
buildkitdFlags?: string;
buildkitVersion?: string;
platforms?: string;
}

22
src/types/github.ts Normal file
View File

@@ -0,0 +1,22 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface GitHubRelease {
id: number;
tag_name: string;
html_url: string;
assets: Array<string>;
}