fix buildx standalone and check for docker availability

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-18 10:09:41 +01:00
parent 9b338b58a7
commit 252c717cc3
7 changed files with 80 additions and 59 deletions

View File

@@ -40,7 +40,7 @@ export class Builder {
}
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
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,

View File

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

View File

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