Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudbuilder): add error handling to fallback to cli install of buildx builder #6258

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/src/plugins/container/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 22 additions & 12 deletions core/src/plugins/container/cloudbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -335,8 +335,8 @@ class BuildxBuilder {

try {
if (refCount === 1) {
await this.remove_tmpdir()
await this.remove_builder()
await this.removeBuilder()
await this.removeTmpdir()
}
} finally {
// even decrease refcount if removal failed
Expand All @@ -355,7 +355,7 @@ class BuildxBuilder {

await this.writeCertificates()

const success = this.installDirectly()
const success = await this.installDirectly()
if (!success) {
await this.installUsingCLI()
}
Expand All @@ -367,18 +367,18 @@ 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) {
// 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,
Expand Down Expand Up @@ -435,12 +435,22 @@ 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") {
this.ctx.log.debug(`Error checking buildx instance path ${this.buildxInstanceJsonPath}: ${e.message}`)
return false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still log the error though? In case there are other failure modes we're not thinking of which we won't see if the error is swallowed.

}
throw e
}
return false
}

private getBuildxInstanceJson() {
Expand Down