Compare commits

..

5 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
7 changed files with 58 additions and 51 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;
@@ -46,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);
@@ -92,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) {
@@ -113,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

@@ -41,8 +41,8 @@ export class Buildx {
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 {

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> {

View File

@@ -20,36 +20,47 @@ 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) {
core.debug(`Docker.isAvailable error: ${res.stderr}`);
dockerAvailable = false;
} else {
core.debug(`Docker.isAvailable ok`);
dockerAvailable = res.exitCode == 0;
}
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
core.debug(`Docker.isAvailable failed: ${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): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable;
const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) {
core.debug('Docker.printVersion: Docker is not available, skipping.');
return;
@@ -60,7 +71,7 @@ export class Docker {
}
public static async printInfo(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable;
const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) {
core.debug('Docker.printInfo: Docker is not available, skipping.');
return;

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});