Skip to content

Commit

Permalink
Stop cloudchamber build from trying to push image when build fails
Browse files Browse the repository at this point in the history
  • Loading branch information
IRCody committed Feb 21, 2025
1 parent 459fbd7 commit d5e3fc7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-knives-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Fix a bug in cloudchamber build where it would still attempt to push an image if the build failed.
50 changes: 33 additions & 17 deletions packages/wrangler/src/cloudchamber/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from "child_process";
import { stat } from "fs/promises";
import { logRaw } from "@cloudflare/cli";
import { ImageRegistriesService } from "./client";
import type { Config } from "../config";
Expand Down Expand Up @@ -68,17 +69,20 @@ export async function constructBuildCommand(options: {
}

// Function for building
export async function dockerBuild(options: { buildCmd: string }) {
const buildCmd = options.buildCmd.split(" ").slice(1);
const buildExec = options.buildCmd.split(" ").shift();
const child = spawn(String(buildExec), buildCmd, { stdio: "inherit" }).on(
"error",
(err) => {
throw err;
}
);
await new Promise((resolve) => {
child.on("close", resolve);
export async function dockerBuild(options: {
buildCmd: string;
}): Promise<void> {
return new Promise((resolve, reject) => {
const buildCmd = options.buildCmd.split(" ").slice(1);
const buildExec = options.buildCmd.split(" ").shift();
const child = spawn(String(buildExec), buildCmd, { stdio: "inherit" });
child.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Build exited with code: ${code}`));
}
});
});
}

Expand Down Expand Up @@ -160,17 +164,33 @@ export function pushYargs(yargs: CommonYargsArgvJSON) {
.positional("TAG", { type: "string", demandOption: true });
}

async function isDir(path: string): Promise<boolean> {
const stats = await stat(path);
return await stats.isDirectory();
}

export async function buildCommand(
args: StrictYargsOptionsToInterfaceJSON<typeof buildYargs>,
_: Config
) {
try {
const dir = await isDir(args.PATH);
if (!dir) {
logRaw(`PATH must be a directory`);
return;
}
} catch (error) {
logRaw(`Error when checking ${args.PATH}: ${error}`);
return;
}

try {
await constructBuildCommand({
imageTag: args.tag,
pathToDockerfile: args.PATH,
pathToDocker: args.pathToDocker,
})
.then(async (bc) => dockerBuild({ buildCmd: bc }))
.then(async (bc) => await dockerBuild({ buildCmd: bc }))
.then(async () => {
if (args.push) {
await dockerLoginManagedRegistry({
Expand All @@ -181,11 +201,7 @@ export async function buildCommand(
}
});
} catch (error) {
if (error instanceof Error) {
logRaw(error.message);
} else {
logRaw("An unknown error occurred");
}
logRaw(error);
}
}

Expand Down

0 comments on commit d5e3fc7

Please sign in to comment.