From f4188843d85015d6cc493cb1633bf54f65c5883e Mon Sep 17 00:00:00 2001 From: Anna Mager Date: Fri, 5 Jul 2024 13:49:49 +0200 Subject: [PATCH 1/3] fix(cloudbuilder): add error handling to fallback to cli install of buildx builder --- core/src/plugins/container/build.ts | 8 +++++++ core/src/plugins/container/cloudbuilder.ts | 25 +++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/core/src/plugins/container/build.ts b/core/src/plugins/container/build.ts index 95b0e67a67..50b0c2149b 100644 --- a/core/src/plugins/container/build.ts +++ b/core/src/plugins/container/build.ts @@ -196,6 +196,14 @@ async function buildContainerLocally({ Learn more at https://docs.docker.com/go/build-multi-platform/ `, }) + } else if (error.message.includes("failed to push")) { + throw new ConfigurationError({ + message: dedent` + The Docker daemon failed to push the image to the registry. + Please make sure that you are logged in and that you + have sufficient permissions on this machine to push to the registry. + `, + }) } throw error } diff --git a/core/src/plugins/container/cloudbuilder.ts b/core/src/plugins/container/cloudbuilder.ts index 6c772f2347..654e6f6165 100644 --- a/core/src/plugins/container/cloudbuilder.ts +++ b/core/src/plugins/container/cloudbuilder.ts @@ -8,7 +8,7 @@ import type { PluginContext } from "../../plugin-context.js" import type { Resolved } from "../../actions/types.js" import type { ContainerBuildAction } from "./config.js" -import { ConfigurationError, InternalError } from "../../exceptions.js" +import { ConfigurationError, InternalError, isErrnoException } from "../../exceptions.js" import type { ContainerProvider, ContainerProviderConfig } from "./container.js" import dedent from "dedent" import { styles } from "../../logger/styles.js" @@ -335,8 +335,8 @@ class BuildxBuilder { try { if (refCount === 1) { - await this.remove_tmpdir() await this.remove_builder() + await this.remove_tmpdir() } } finally { // even decrease refcount if removal failed @@ -355,7 +355,7 @@ class BuildxBuilder { await this.writeCertificates() - const success = this.installDirectly() + const success = await this.installDirectly() if (!success) { await this.installUsingCLI() } @@ -435,12 +435,21 @@ class BuildxBuilder { } private async installDirectly() { - const statResult = await stat(dirname(this.buildxInstanceJsonPath)) - if (statResult.isDirectory()) { - await writeFile(this.buildxInstanceJsonPath, JSON.stringify(this.getBuildxInstanceJson())) - return true + try { + const statResult = await stat(dirname(this.buildxInstanceJsonPath)) + if (statResult.isDirectory()) { + await writeFile(this.buildxInstanceJsonPath, JSON.stringify(this.getBuildxInstanceJson())) + return true + } + return false + } catch (e) { + // An error is thrown e.g. if the path does not exist. + // We don't need to handle this error, as we will fall back to the CLI installation. + if (isErrnoException(e) && e.code === "ENOENT") { + return false + } + throw e } - return false } private getBuildxInstanceJson() { From f595e7ee48a042e7d019e7d2825be208b3ccdb88 Mon Sep 17 00:00:00 2001 From: Anna Mager Date: Fri, 5 Jul 2024 14:59:38 +0200 Subject: [PATCH 2/3] chore: use project root instead of cert dir as cwd for docker cmd --- core/src/plugins/container/cloudbuilder.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/plugins/container/cloudbuilder.ts b/core/src/plugins/container/cloudbuilder.ts index 654e6f6165..328f7fd016 100644 --- a/core/src/plugins/container/cloudbuilder.ts +++ b/core/src/plugins/container/cloudbuilder.ts @@ -378,7 +378,7 @@ class BuildxBuilder { } catch (e) { // fall back to docker CLI const result = await containerHelpers.dockerCli({ - cwd: this.certDir, + cwd: this.ctx.projectRoot, args: ["buildx", "rm", this.name], ctx: this.ctx, log: this.ctx.log, @@ -446,6 +446,7 @@ class BuildxBuilder { // An error is thrown e.g. if the path does not exist. // We don't need to handle this error, as we will fall back to the CLI installation. if (isErrnoException(e) && e.code === "ENOENT") { + this.ctx.log.debug(`Error checking buildx instance path ${this.buildxInstanceJsonPath}: ${e.message}`) return false } throw e From 5bf7f946ee7e0fd3c5e45d9559268fb9c7f09409 Mon Sep 17 00:00:00 2001 From: Anna Mager Date: Fri, 5 Jul 2024 15:01:08 +0200 Subject: [PATCH 3/3] chore: camelcase functions --- core/src/plugins/container/cloudbuilder.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/plugins/container/cloudbuilder.ts b/core/src/plugins/container/cloudbuilder.ts index 328f7fd016..871d872b6f 100644 --- a/core/src/plugins/container/cloudbuilder.ts +++ b/core/src/plugins/container/cloudbuilder.ts @@ -335,8 +335,8 @@ class BuildxBuilder { try { if (refCount === 1) { - await this.remove_builder() - await this.remove_tmpdir() + await this.removeBuilder() + await this.removeTmpdir() } } finally { // even decrease refcount if removal failed @@ -367,12 +367,12 @@ class BuildxBuilder { // private: clean - private async remove_tmpdir() { + private async removeTmpdir() { this.ctx.log.debug(`Removing ${this.certDir}...`) await rm(this.certDir, { recursive: true, force: true }) } - private async remove_builder() { + private async removeBuilder() { try { await rm(this.buildxInstanceJsonPath) } catch (e) {