Merge pull request #49 from crazy-max/buildx-version

buildx: fix version method
This commit is contained in:
CrazyMax
2023-02-20 10:17:01 +01:00
committed by GitHub
5 changed files with 26 additions and 23 deletions

View File

@@ -157,7 +157,7 @@ describe('printVersion', () => {
describe('version', () => { describe('version', () => {
it('valid', async () => { it('valid', async () => {
const buildx = new Buildx(); const buildx = new Buildx();
expect(semver.valid(await buildx.version)).not.toBeUndefined(); expect(semver.valid(await buildx.version())).not.toBeUndefined();
}); });
}); });

View File

@@ -52,7 +52,7 @@ describe('isAvailable', () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput'); const execSpy = jest.spyOn(Exec, 'getExecOutput');
await Docker.isAvailable(); await Docker.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`, [], {
silent: true, silent: true,
ignoreReturnCode: true ignoreReturnCode: true
}); });

View File

@@ -80,17 +80,17 @@ export class BuildKit {
builderInfo = await new Builder({buildx: this.buildx}).inspect(builderName); builderInfo = await new Builder({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;
core.debug(`BuildKit.versionSatisfies ${bkversion}: ${range}`);
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`); core.debug(`BuildKit.versionSatisfies ${node.name}: can't get version`);
return false; return false;
} }
} }
core.debug(`BuildKit.versionSatisfies ${node}: version ${bkversion}`); core.debug(`BuildKit.versionSatisfies ${node.name}: 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;

View File

@@ -30,7 +30,8 @@ export interface BuildxOpts {
} }
export class Buildx { export class Buildx {
private _version: string | undefined; private _version: string;
private _versionOnce: boolean;
private readonly _standalone: boolean | undefined; private readonly _standalone: boolean | undefined;
public readonly inputs: Inputs; public readonly inputs: Inputs;
@@ -39,6 +40,8 @@ export class Buildx {
constructor(opts?: BuildxOpts) { constructor(opts?: BuildxOpts) {
this._standalone = opts?.standalone; this._standalone = opts?.standalone;
this._version = '';
this._versionOnce = false;
this.inputs = new Inputs(); this.inputs = new Inputs();
} }
@@ -94,22 +97,22 @@ export class Buildx {
}); });
} }
get version() { public async version(): Promise<string> {
return (async () => { if (this._versionOnce) {
if (!this._version) {
const cmd = await 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; return this._version;
})(); }
this._versionOnce = true;
const cmd = await 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() { public async printVersion() {
@@ -128,7 +131,7 @@ export class Buildx {
} }
public async versionSatisfies(range: string, version?: string): Promise<boolean> { public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version); const ver = version ?? (await this.version());
if (!ver) { if (!ver) {
core.debug(`Buildx.versionSatisfies false: undefined version`); core.debug(`Buildx.versionSatisfies false: undefined version`);
return false; return false;

View File

@@ -25,7 +25,7 @@ export class Docker {
} }
public static async isAvailable(): Promise<boolean> { public static async isAvailable(): Promise<boolean> {
const ok: boolean = await Exec.getExecOutput('docker', undefined, { const ok: boolean = await Exec.getExecOutput('docker', [], {
ignoreReturnCode: true, ignoreReturnCode: true,
silent: true silent: true
}) })