Compare commits

..

8 Commits

Author SHA1 Message Date
CrazyMax
2038d87306 Merge pull request #43 from crazy-max/fix-standalone
Some checks failed
publish / publish (push) Has been cancelled
fix buildx standalone and check for docker availability
2023-02-18 10:15:26 +01:00
CrazyMax
252c717cc3 fix buildx standalone and check for docker availability
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 10:09:41 +01:00
CrazyMax
9b338b58a7 toolkit: docker property not needed
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 10:08:21 +01:00
CrazyMax
abe37ab72b Merge pull request #42 from crazy-max/buildkit-fix-version
Some checks failed
publish / publish (push) Has been cancelled
buildkit: use node info to retrieve version
2023-02-18 07:34:55 +01:00
CrazyMax
b3a30a9e32 Merge pull request #41 from crazy-max/fix-docker
docker: fix instance
2023-02-18 07:32:33 +01:00
CrazyMax
31e915c017 buildkit: use node info to retrieve version
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 07:31:21 +01:00
CrazyMax
05bbe49fe1 toolkit: add docker
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 07:06:29 +01:00
CrazyMax
c80fedbbae docker: fix instance
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 07:06:20 +01:00
9 changed files with 105 additions and 80 deletions

View File

@@ -15,7 +15,6 @@
*/ */
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals'; import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import * as semver from 'semver';
import {BuildKit} from '../../src/buildkit/buildkit'; import {BuildKit} from '../../src/buildkit/buildkit';
import {Builder} from '../../src/buildx/builder'; import {Builder} from '../../src/buildx/builder';
@@ -48,11 +47,15 @@ jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Bu
describe('getVersion', () => { describe('getVersion', () => {
it('valid', async () => { it('valid', async () => {
const builder = new Builder({
context: new Context()
});
const builderInfo = await builder.inspect('builder2');
const buildkit = new BuildKit({ const buildkit = new BuildKit({
context: new Context() context: new Context()
}); });
const version = await buildkit.getVersion('builder2'); const version = await buildkit.getVersion(builderInfo.nodes[0]);
expect(semver.valid(version)).not.toBeNull(); expect(version).toBe('v0.11.0');
}); });
}); });

View File

@@ -95,9 +95,7 @@ describe('isAvailable', () => {
context: new Context(), context: new Context(),
standalone: false standalone: false
}); });
buildx.isAvailable().catch(() => { await buildx.isAvailable();
// noop
});
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], { expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true, silent: true,
@@ -110,9 +108,7 @@ describe('isAvailable', () => {
context: new Context(), context: new Context(),
standalone: true standalone: true
}); });
buildx.isAvailable().catch(() => { await buildx.isAvailable();
// noop
});
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], { expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
silent: true, silent: true,
@@ -122,13 +118,13 @@ describe('isAvailable', () => {
}); });
describe('printInspect', () => { describe('printInspect', () => {
it('prints builder2 instance', () => { it('prints builder2 instance', async () => {
const execSpy = jest.spyOn(exec, 'exec'); const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({ const buildx = new Buildx({
context: new Context(), context: new Context(),
standalone: true standalone: true
}); });
buildx.printInspect('builder2').catch(() => { await buildx.printInspect('builder2').catch(() => {
// noop // noop
}); });
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['inspect', 'builder2'], { expect(execSpy).toHaveBeenCalledWith(`buildx`, ['inspect', 'builder2'], {
@@ -138,24 +134,24 @@ describe('printInspect', () => {
}); });
describe('printVersion', () => { describe('printVersion', () => {
it('docker cli', () => { it('docker cli', async () => {
const execSpy = jest.spyOn(exec, 'exec'); const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({ const buildx = new Buildx({
context: new Context(), context: new Context(),
standalone: false standalone: false
}); });
buildx.printVersion(); await buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx', 'version'], { expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx', 'version'], {
failOnStdErr: false failOnStdErr: false
}); });
}); });
it('standalone', () => { it('standalone', async () => {
const execSpy = jest.spyOn(exec, 'exec'); const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({ const buildx = new Buildx({
context: new Context(), context: new Context(),
standalone: true standalone: true
}); });
buildx.printVersion(); await buildx.printVersion();
expect(execSpy).toHaveBeenCalledWith(`buildx`, ['version'], { expect(execSpy).toHaveBeenCalledWith(`buildx`, ['version'], {
failOnStdErr: false failOnStdErr: false
}); });

View File

@@ -48,9 +48,9 @@ describe('configDir', () => {
}); });
describe('isAvailable', () => { describe('isAvailable', () => {
it('cli', () => { it('cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput'); const execSpy = jest.spyOn(exec, 'getExecOutput');
Docker.isAvailable; await Docker.getInstance().isAvailable();
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, { expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
silent: true, silent: true,

View File

@@ -23,7 +23,7 @@ import {Buildx} from '../buildx/buildx';
import {Builder} from '../buildx/builder'; import {Builder} from '../buildx/builder';
import {Config} from './config'; import {Config} from './config';
import {BuilderInfo} from '../types/builder'; import {BuilderInfo, NodeInfo} from '../types/builder';
export interface BuildKitOpts { export interface BuildKitOpts {
context: Context; context: Context;
@@ -46,14 +46,7 @@ export class BuildKit {
}); });
} }
public async getVersion(builderName: string): Promise<string | undefined> { public async getVersion(node: NodeInfo): Promise<string | undefined> {
const builderInfo = await this.getBuilderInfo(builderName);
if (builderInfo.nodes.length == 0) {
// a builder always have on node, should not happen.
return undefined;
}
// TODO: get version for all nodes
const node = builderInfo.nodes[0];
if (!node.buildkitVersion && node.name) { if (!node.buildkitVersion && node.name) {
try { try {
return await this.getVersionWithinImage(node.name); return await this.getVersionWithinImage(node.name);
@@ -65,6 +58,7 @@ export class BuildKit {
} }
private async getVersionWithinImage(nodeName: string): Promise<string> { private async getVersionWithinImage(nodeName: string): Promise<string> {
core.debug(`BuildKit.getVersionWithinImage nodeName: ${nodeName}`);
return exec return exec
.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], { .getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], {
ignoreReturnCode: true, ignoreReturnCode: true,
@@ -72,6 +66,7 @@ export class BuildKit {
}) })
.then(bkitimage => { .then(bkitimage => {
if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) { if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) {
core.debug(`BuildKit.getVersionWithinImage image: ${bkitimage.stdout.trim()}`);
return exec return exec
.getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout.trim(), '--version'], { .getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout.trim(), '--version'], {
ignoreReturnCode: true, ignoreReturnCode: true,
@@ -92,17 +87,25 @@ export class BuildKit {
}); });
} }
public async versionSatisfies(builderName: string, range: string): Promise<boolean> { public async versionSatisfies(builderName: string, range: string, builderInfo?: BuilderInfo): Promise<boolean> {
const builderInfo = await this.getBuilderInfo(builderName); if (!builderInfo) {
builderInfo = await new Builder({
context: this.context,
buildx: this.buildx
}).inspect(builderName);
}
for (const node of builderInfo.nodes) { for (const node of builderInfo.nodes) {
core.debug(`BuildKit.versionSatisfies ${node}: ${range}`);
let bkversion = node.buildkitVersion; let bkversion = node.buildkitVersion;
if (!bkversion) { if (!bkversion) {
try { try {
bkversion = await this.getVersionWithinImage(node.name || ''); bkversion = await this.getVersionWithinImage(node.name || '');
} catch (e) { } catch (e) {
core.debug(`BuildKit.versionSatisfies ${node}: can't get version`);
return false; return false;
} }
} }
core.debug(`BuildKit.versionSatisfies ${node}: version ${bkversion}`);
// BuildKit version reported by moby is in the format of `v0.11.0-moby` // BuildKit version reported by moby is in the format of `v0.11.0-moby`
if (builderInfo.driver == 'docker' && !bkversion.endsWith('-moby')) { if (builderInfo.driver == 'docker' && !bkversion.endsWith('-moby')) {
return false; return false;
@@ -113,12 +116,4 @@ export class BuildKit {
} }
return true; return true;
} }
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,
buildx: this.buildx
});
return builder.inspect(name);
}
} }

View File

@@ -40,7 +40,7 @@ export class Builder {
} }
public async inspect(name: string): Promise<BuilderInfo> { public async inspect(name: string): Promise<BuilderInfo> {
const cmd = this.buildx.getCommand(['inspect', name]); const cmd = await this.buildx.getCommand(['inspect', name]);
return await exec return await exec
.getExecOutput(cmd.command, cmd.args, { .getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true, ignoreReturnCode: true,

View File

@@ -32,17 +32,18 @@ export interface BuildxOpts {
} }
export class Buildx { export class Buildx {
private readonly context: Context;
private _version: string | undefined; private _version: string | undefined;
private readonly _standalone: boolean | undefined;
private readonly context: Context;
public readonly inputs: Inputs; public readonly inputs: Inputs;
public readonly standalone: boolean;
public static readonly containerNamePrefix = 'buildx_buildkit_'; public static readonly containerNamePrefix = 'buildx_buildkit_';
constructor(opts: BuildxOpts) { constructor(opts: BuildxOpts) {
this._standalone = opts?.standalone;
this.context = opts.context; this.context = opts.context;
this.inputs = new Inputs(this.context); this.inputs = new Inputs(this.context);
this.standalone = opts?.standalone ?? !Docker.isAvailable;
} }
static get configDir(): string { static get configDir(): string {
@@ -53,15 +54,22 @@ export class Buildx {
return path.join(Buildx.configDir, 'certs'); return path.join(Buildx.configDir, 'certs');
} }
public getCommand(args: Array<string>) { public async isStandalone(): Promise<boolean> {
const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable());
core.debug(`Buildx.isStandalone: ${standalone}`);
return standalone;
}
public async getCommand(args: Array<string>) {
const standalone = await this.isStandalone();
return { return {
command: this.standalone ? 'buildx' : 'docker', command: standalone ? 'buildx' : 'docker',
args: this.standalone ? args : ['buildx', ...args] args: standalone ? args : ['buildx', ...args]
}; };
} }
public async isAvailable(): Promise<boolean> { public async isAvailable(): Promise<boolean> {
const cmd = this.getCommand([]); const cmd = await this.getCommand([]);
return await exec return await exec
.getExecOutput(cmd.command, cmd.args, { .getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true, ignoreReturnCode: true,
@@ -80,7 +88,7 @@ export class Buildx {
} }
public async printInspect(name: string): Promise<void> { public async printInspect(name: string): Promise<void> {
const cmd = this.getCommand(['inspect', name]); const cmd = await this.getCommand(['inspect', name]);
await exec.exec(cmd.command, cmd.args, { await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false failOnStdErr: false
}); });
@@ -89,7 +97,7 @@ export class Buildx {
get version() { get version() {
return (async () => { return (async () => {
if (!this._version) { if (!this._version) {
const cmd = this.getCommand(['version']); const cmd = await this.getCommand(['version']);
this._version = await exec this._version = await exec
.getExecOutput(cmd.command, cmd.args, { .getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true, ignoreReturnCode: true,
@@ -107,7 +115,7 @@ export class Buildx {
} }
public async printVersion() { public async printVersion() {
const cmd = this.getCommand(['version']); const cmd = await this.getCommand(['version']);
await exec.exec(cmd.command, cmd.args, { await exec.exec(cmd.command, cmd.args, {
failOnStdErr: false failOnStdErr: false
}); });

View File

@@ -37,17 +37,19 @@ export interface InstallOpts {
} }
export class Install { export class Install {
private readonly _standalone: boolean | undefined;
private readonly context: Context; private readonly context: Context;
private readonly standalone: boolean;
constructor(opts?: InstallOpts) { constructor(opts?: InstallOpts) {
this.context = opts?.context || new Context(); this.context = opts?.context || new Context();
this.standalone = opts?.standalone ?? !Docker.isAvailable; this._standalone = opts?.standalone;
} }
public async download(version: string, dest?: string): Promise<string> { public async download(version: string, dest?: string): Promise<string> {
const release: GitHubRelease = await Install.getRelease(version); const release: GitHubRelease = await Install.getRelease(version);
const fversion = release.tag_name.replace(/^v+|v+$/g, ''); const fversion = release.tag_name.replace(/^v+|v+$/g, '');
core.debug(`Install.download version: ${fversion}`);
let toolPath: string; let toolPath: string;
toolPath = tc.find('buildx', fversion, this.platform()); toolPath = tc.find('buildx', fversion, this.platform());
@@ -58,9 +60,11 @@ export class Install {
} }
toolPath = await this.fetchBinary(fversion); toolPath = await this.fetchBinary(fversion);
} }
core.debug(`Install.download toolPath: ${toolPath}`);
dest = dest || (this.standalone ? this.context.tmpDir() : Docker.configDir); dest = dest || ((await this.isStandalone()) ? this.context.tmpDir() : Docker.configDir);
if (this.standalone) { core.debug(`Install.download dest: ${dest}`);
if (await this.isStandalone()) {
return this.setStandalone(toolPath, dest); return this.setStandalone(toolPath, dest);
} }
return this.setPlugin(toolPath, dest); return this.setPlugin(toolPath, dest);
@@ -100,7 +104,8 @@ export class Install {
} }
dest = dest || Docker.configDir; dest = dest || Docker.configDir;
if (this.standalone) { core.debug(`Install.build dest: ${dest}`);
if (await this.isStandalone()) {
return this.setStandalone(toolPath, dest); return this.setStandalone(toolPath, dest);
} }
return this.setPlugin(toolPath, dest); return this.setPlugin(toolPath, dest);
@@ -111,10 +116,10 @@ export class Install {
const buildxPluginFound = await new Buildx({context: this.context, standalone: false}).isAvailable(); const buildxPluginFound = await new Buildx({context: this.context, standalone: false}).isAvailable();
let buildStandalone = false; let buildStandalone = false;
if (this.standalone && buildxStandaloneFound) { if ((await this.isStandalone()) && buildxStandaloneFound) {
core.debug(`Install.buildCommand: Buildx standalone found, build with it`); core.debug(`Install.buildCommand: Buildx standalone found, build with it`);
buildStandalone = true; buildStandalone = true;
} else if (!this.standalone && buildxPluginFound) { } else if (!(await this.isStandalone()) && buildxPluginFound) {
core.debug(`Install.buildCommand: Buildx plugin found, build with it`); core.debug(`Install.buildCommand: Buildx plugin found, build with it`);
buildStandalone = false; buildStandalone = false;
} else if (buildxStandaloneFound) { } else if (buildxStandaloneFound) {
@@ -128,7 +133,7 @@ export class Install {
} }
//prettier-ignore //prettier-ignore
return new Buildx({context: this.context, standalone: buildStandalone}).getCommand([ return await new Buildx({context: this.context, standalone: buildStandalone}).getCommand([
'build', 'build',
'--target', 'binaries', '--target', 'binaries',
'--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1', '--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1',
@@ -137,6 +142,12 @@ export class Install {
]); ]);
} }
private async isStandalone(): Promise<boolean> {
const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable());
core.debug(`Install.isStandalone: ${standalone}`);
return standalone;
}
private async setStandalone(toolPath: string, dest: string): Promise<string> { private async setStandalone(toolPath: string, dest: string): Promise<string> {
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
const binDir = path.join(dest, 'bin'); const binDir = path.join(dest, 'bin');
@@ -148,6 +159,7 @@ export class Install {
fs.copyFileSync(toolBinPath, buildxPath); fs.copyFileSync(toolBinPath, buildxPath);
fs.chmodSync(buildxPath, '0755'); fs.chmodSync(buildxPath, '0755');
core.addPath(binDir); core.addPath(binDir);
core.debug(`Install.setStandalone buildxPath: ${buildxPath}`);
return buildxPath; return buildxPath;
} }
@@ -161,6 +173,7 @@ export class Install {
const pluginPath: string = path.join(pluginsDir, filename); const pluginPath: string = path.join(pluginsDir, filename);
fs.copyFileSync(toolBinPath, pluginPath); fs.copyFileSync(toolBinPath, pluginPath);
fs.chmodSync(pluginPath, '0755'); fs.chmodSync(pluginPath, '0755');
core.debug(`Install.setPlugin pluginPath: ${pluginPath}`);
return pluginPath; return pluginPath;
} }

View File

@@ -20,36 +20,46 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
export class Docker { export class Docker {
private static instance?: Docker;
static getInstance = (): Docker => (Docker.instance = Docker.instance ?? new Docker());
private _available: boolean | undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
static get configDir(): string { static get configDir(): string {
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
} }
static get isAvailable(): boolean { public async isAvailable(): Promise<boolean> {
let dockerAvailable = false; if (this._available === undefined) {
exec await exec
.getExecOutput('docker', undefined, { .getExecOutput('docker', undefined, {
ignoreReturnCode: true, ignoreReturnCode: true,
silent: true silent: true
}) })
.then(res => { .then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) { if (res.stderr.length > 0 && res.exitCode != 0) {
core.debug(`Docker.isAvailable error: ${res.stderr}`); core.debug(`Docker.available error: ${res.stderr}`);
dockerAvailable = false; this._available = false;
} else { } else {
core.debug(`Docker.isAvailable ok`); core.debug(`Docker.available ok`);
dockerAvailable = res.exitCode == 0; this._available = res.exitCode == 0;
} }
}) })
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => { .catch(error => {
core.debug(`Docker.isAvailable failed: ${error}`); core.debug(`Docker.available failed: ${error}`);
dockerAvailable = false; this._available = false;
}); });
return dockerAvailable; }
core.debug(`Docker.available: ${this._available}`);
return this._available ?? false;
} }
public static async printVersion(standalone?: boolean): Promise<void> { public static async printVersion(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable; const noDocker = standalone ?? !(await Docker.getInstance().isAvailable());
if (noDocker) { if (noDocker) {
core.debug('Docker.printVersion: Docker is not available, skipping.'); core.debug('Docker.printVersion: Docker is not available, skipping.');
return; return;
@@ -60,7 +70,7 @@ export class Docker {
} }
public static async printInfo(standalone?: boolean): Promise<void> { public static async printInfo(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable; const noDocker = standalone ?? !(await Docker.getInstance().isAvailable());
if (noDocker) { if (noDocker) {
core.debug('Docker.printInfo: Docker is not available, skipping.'); core.debug('Docker.printInfo: Docker is not available, skipping.');
return; return;

View File

@@ -41,7 +41,7 @@ export class Toolkit {
this.context = new Context(); this.context = new Context();
this.github = new GitHub({token: opts.githubToken}); this.github = new GitHub({token: opts.githubToken});
this.buildx = new Buildx({context: this.context}); this.buildx = new Buildx({context: this.context});
this.buildxInstall = new Install({context: this.context, standalone: this.buildx.standalone}); this.buildxInstall = new Install({context: this.context});
this.builder = new Builder({context: this.context, buildx: this.buildx}); this.builder = new Builder({context: this.context, buildx: this.buildx});
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx}); this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});
} }