Compare commits

...

9 Commits

Author SHA1 Message Date
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
CrazyMax
2c60cad840 Merge pull request #40 from crazy-max/debug
Some checks failed
publish / publish (push) Has been cancelled
some debug logs
2023-02-18 06:00:54 +01:00
CrazyMax
847887b312 some debug logs
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 04:36:32 +01:00
CrazyMax
d9984214c9 Merge pull request #39 from crazy-max/buildkit-ctn-prefix
buildx: make containerNamePrefix static and public
2023-02-18 02:20:37 +01:00
CrazyMax
33b4390bc2 buildx: make containerNamePrefix static and public
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-18 02:12:09 +01:00
7 changed files with 75 additions and 58 deletions

View File

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

View File

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

View File

@@ -23,7 +23,7 @@ import {Buildx} from '../buildx/buildx';
import {Builder} from '../buildx/builder';
import {Config} from './config';
import {BuilderInfo} from '../types/builder';
import {BuilderInfo, NodeInfo} from '../types/builder';
export interface BuildKitOpts {
context: Context;
@@ -33,7 +33,6 @@ export interface BuildKitOpts {
export class BuildKit {
private readonly context: Context;
private readonly buildx: Buildx;
private containerNamePrefix = 'buildx_buildkit_';
public readonly config: Config;
@@ -47,14 +46,7 @@ export class BuildKit {
});
}
public async getVersion(builderName: string): 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];
public async getVersion(node: NodeInfo): Promise<string | undefined> {
if (!node.buildkitVersion && node.name) {
try {
return await this.getVersionWithinImage(node.name);
@@ -67,7 +59,7 @@ export class BuildKit {
private async getVersionWithinImage(nodeName: string): Promise<string> {
return exec
.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', `${this.containerNamePrefix}${nodeName}`], {
.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', `${Buildx.containerNamePrefix}${nodeName}`], {
ignoreReturnCode: true,
silent: true
})
@@ -93,8 +85,13 @@ export class BuildKit {
});
}
public async versionSatisfies(builderName: string, range: string): Promise<boolean> {
const builderInfo = await this.getBuilderInfo(builderName);
public async versionSatisfies(builderName: string, range: string, builderInfo?: BuilderInfo): Promise<boolean> {
if (!builderInfo) {
builderInfo = await new Builder({
context: this.context,
buildx: this.buildx
}).inspect(builderName);
}
for (const node of builderInfo.nodes) {
let bkversion = node.buildkitVersion;
if (!bkversion) {
@@ -114,12 +111,4 @@ export class BuildKit {
}
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

@@ -16,6 +16,7 @@
import fs from 'fs';
import path from 'path';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as semver from 'semver';
@@ -36,11 +37,12 @@ export class Buildx {
public readonly inputs: Inputs;
public readonly standalone: boolean;
public static readonly containerNamePrefix = 'buildx_buildkit_';
constructor(opts: BuildxOpts) {
this.context = opts.context;
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
this.inputs = new Inputs(this.context);
this.standalone = opts?.standalone ?? !Docker.isAvailable;
}
static get configDir(): string {
@@ -122,9 +124,12 @@ export class Buildx {
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version);
if (!ver) {
core.debug(`Buildx.versionSatisfies false: undefined version`);
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
const res = semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
core.debug(`Buildx.versionSatisfies ${ver} statisfies ${range}: ${res}`);
return res;
}
public static resolveCertsDriverOpts(driver: string, endpoint: string, cert: Cert): Array<string> {

View File

@@ -42,7 +42,7 @@ export class Install {
constructor(opts?: InstallOpts) {
this.context = opts?.context || new Context();
this.standalone = opts?.standalone ?? !Docker.isAvailable;
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
}
public async download(version: string, dest?: string): Promise<string> {
@@ -80,7 +80,7 @@ export class Install {
} else {
vspec = await Git.getRemoteSha(repo, ref);
}
core.debug(`Tool version spec ${vspec}`);
core.debug(`Install.build: tool version spec ${vspec}`);
let toolPath: string;
toolPath = tc.find('buildx', vspec);
@@ -112,16 +112,16 @@ export class Install {
let buildStandalone = false;
if (this.standalone && buildxStandaloneFound) {
core.debug(`Buildx standalone found, build with it`);
core.debug(`Install.buildCommand: Buildx standalone found, build with it`);
buildStandalone = true;
} else if (!this.standalone && buildxPluginFound) {
core.debug(`Buildx plugin found, build with it`);
core.debug(`Install.buildCommand: Buildx plugin found, build with it`);
buildStandalone = false;
} else if (buildxStandaloneFound) {
core.debug(`Buildx plugin not found, but standalone found so trying to build with it`);
core.debug(`Install.buildCommand: Buildx plugin not found, but standalone found so trying to build with it`);
buildStandalone = true;
} else if (buildxPluginFound) {
core.debug(`Buildx standalone not found, but plugin found so trying to build with it`);
core.debug(`Install.buildCommand: Buildx standalone not found, but plugin found so trying to build with it`);
buildStandalone = false;
} else {
throw new Error(`Neither buildx standalone or plugin have been found to build from ref ${gitContext}`);

View File

@@ -16,37 +16,53 @@
import os from 'os';
import path from 'path';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
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 {
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
}
static get isAvailable(): boolean {
let dockerAvailable = false;
exec
.getExecOutput('docker', undefined, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
dockerAvailable = false;
} else {
dockerAvailable = res.exitCode == 0;
}
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
dockerAvailable = false;
});
return dockerAvailable;
get available() {
return (async () => {
if (!this._available) {
this._available = await exec
.getExecOutput('docker', undefined, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.debug(`Docker.isAvailable error: ${res.stderr}`);
return false;
} else {
core.debug(`Docker.isAvailable ok`);
return res.exitCode == 0;
}
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
core.debug(`Docker.isAvailable failed: ${error}`);
return false;
});
}
return this._available;
})();
}
public static async printVersion(standalone?: boolean) {
const noDocker = standalone ?? !Docker.isAvailable;
public static async printVersion(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) {
core.debug('Docker.printVersion: Docker is not available, skipping.');
return;
}
await exec.exec('docker', ['version'], {
@@ -54,9 +70,10 @@ export class Docker {
});
}
public static async printInfo(standalone?: boolean) {
const noDocker = standalone ?? !Docker.isAvailable;
public static async printInfo(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) {
core.debug('Docker.printInfo: Docker is not available, skipping.');
return;
}
await exec.exec('docker', ['info'], {

View File

@@ -20,6 +20,7 @@ import {Install} from './buildx/install';
import {Builder} from './buildx/builder';
import {BuildKit} from './buildkit/buildkit';
import {GitHub} from './github';
import {Docker} from './docker';
export interface ToolkitOpts {
/**
@@ -32,6 +33,7 @@ export interface ToolkitOpts {
export class Toolkit {
public context: Context;
public github: GitHub;
public docker: Docker;
public buildx: Buildx;
public buildxInstall: Install;
public builder: Builder;
@@ -40,7 +42,8 @@ export class Toolkit {
constructor(opts: ToolkitOpts = {}) {
this.context = new Context();
this.github = new GitHub({token: opts.githubToken});
this.buildx = new Buildx({context: this.context});
this.docker = Docker.getInstance();
this.buildx = new Buildx({context: this.context, standalone: !this.docker.available});
this.buildxInstall = new Install({context: this.context, standalone: this.buildx.standalone});
this.builder = new Builder({context: this.context, buildx: this.buildx});
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});