Repository navigation
Conversation
…xts under cf cloudflare.config.ts reads WB_CONTAINER_IMAGE_<NAME> in place of each Dockerfile image. With --containers-rollout=gradual|immediate, wb sets it to the digest-pinned reference of the image built from the committed build context, reusing a published one or building it from git archive and pushing it with cf containers push; a dirty context stops the deploy before any build. With none, or without the flag, wb sets placeholders and deploys with --containers-rollout=none, so the build needs no Docker. Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust system for managing container images in Cloudflare Workers via Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. The Docker build is slow and long, But now our images are strong. With digest pins and committed tree, We deploy with speed and certainty. Footnotes
|
|
ReviewFixLoop run |
There was a problem hiding this comment.
Code Review
This pull request introduces robust handling for Cloudflare Container images built from Dockerfiles during deployment. It adds functionality to resolve, build, and publish container images based on the committed Git tree, preventing uncommitted or untracked files from reaching the image, and reusing already published images to avoid redundant Docker builds. It also updates the documentation and adds comprehensive unit tests. Feedback was provided to improve the robustness of parseJsonObject in containerImages.ts by scanning for the JSON credentials block from the end of stdout instead of the beginning, preventing potential parsing failures if preceding configuration steps output JSON.
| function parseJsonObject(stdout: string): unknown { | ||
| for (const { index } of stdout.matchAll(/\{/gu)) { | ||
| try { | ||
| return JSON.parse(stdout.slice(index)); | ||
| } catch { | ||
| // Not the start of the result. | ||
| } | ||
| } | ||
| throw new Error('cf printed no registry credentials.'); | ||
| } |
There was a problem hiding this comment.
Since cf evaluates the config before printing the credentials, any JSON printed by the config to stdout would appear before the actual credentials JSON. Currently, parseJsonObject scans from the beginning of stdout and returns the first successfully parsed JSON, which could be an unrelated JSON printed by the config, causing schema validation to fail. Scanning from the end of stdout (by reversing the matches) is more robust as the command's output is always the last printed JSON.
function parseJsonObject(stdout: string): unknown {
const matches = Array.from(stdout.matchAll(/\{/gu));
for (let i = matches.length - 1; i >= 0; i--) {
const index = matches[i].index;
try {
return JSON.parse(stdout.slice(index));
} catch {
// Not the start of the result.
}
}
throw new Error('cf printed no registry credentials.');
}…push Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…tted Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…out flag is omitted Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
…s did Co-authored-by: WillBooster (Claude Code) <agent@willbooster.com>
Close #1685
Why
Under the
cfCLI,cf buildbuilds every Dockerfile container image itself from the working tree, andwb deploy --containers-rolloutwas only forwarded tocf deploy. So every deploy of a container-backed Worker ran a Docker build (even with--containers-rollout=none), uncommitted and untracked files reached the image, and an unchanged image was rebuilt and re-pushed. The wrangler-era wb avoided this by rewriting the wrangler JSON config, which is impossible with acloudflare.config.ts.How the references reach the build was settled from cf 1.0.0-rc.6 /
@cloudflare/config1.0.0-rc.4 and the Wrangler build it delegates to (4.168.0):defineContainer()takesimage(and each entry of a Durable Object-managed Container'simages) as either{ dockerfile, buildContext?, buildVars? }or{ reference }. A{ reference }passes through the build unchanged into.cloudflare/output/v1/containers/<name>/container.config.json; only Dockerfile images are built (localReference). A build whose images are all references runs without Docker onPATH.cf buildhas no option to skip image builds, and the Build Output Specification documentslocalReferenceas an already-built local image; rewriting the output beforecf deploy --prebuiltis not a supported contract.<managed registry>/<account-id>/<repository>@sha256:<64 hex>. cf's resolver keeps such an account-qualified reference, inserts the account into a managed-registry reference without one, and rejects one whose 32-hex account segment is another account's.cloudflare.config.tsmay read environment variables.So the config reads a variable wb exports, in place of the Dockerfile. Rewriting Build Output or keeping cf from building were the alternatives, and neither is a cf contract.
Requirements
linux/amd64), looks the image up in Cloudflare's managed registry, and on a miss builds it from the committed tree and pushes it.docker build, and the deployed container references the published digest.--containers-rollout=noneand an omitted flag do no lookup, and deploy a container-backed Worker without Docker.cloudflare.config.tsreadsWB_CONTAINER_IMAGE_<NAME>as{ reference }in place of{ dockerfile }, one variable per image named after cf's repository name for it, because a plain variable is the smallest thing a config can read without wb rewriting it, and per-image names need no parsing in the config.none, or without the flag whenever the config has a container image, wb exports placeholder references and passes--containers-rollout=nonetocf deploy, because without a lookup there is no real image, cf takes one strategy for every Container, andnonemakes cf upload no image (it keeps the deployed Container metadata, or none on a first deploy).{ reference }and never builds them, because such images are built outside wb (e.g. a Dockerfile that needs a BuildKit secret).WB_CONTAINER_IMAGE_*from the environment the config is evaluated under, because an exported stale value would replace a Dockerfile image with an unrelated image.Customer Summary
A project whose Worker builds a container image from a Dockerfile changes its
cloudflare.config.tsonce:Then:
wb deploy --containers-rollout=gradual|immediatereuses the published image of an unchanged committed context with no Docker build, and otherwise builds it from the committed tree, pushes it, and deploys its digest. Uncommitted changes in the build context stop the deploy before anything is built.wb deploy --containers-rollout=none, orwb deploywithout the flag, deploys the Worker without Docker and without touching the deployed Containers, including those declared only by registry reference.wb startandwb testare unchanged: without the variable, the config keeps the Dockerfile.Technical Summary
packages/wb/src/utils/containerImages.ts(new) first evaluates the config with placeholder references and stops when an image ignores its variable, before any lookup or build. It carries over the wrangler-era logic: the same reuse key andwb/<name>:source-<key>tag, the dirty-context check (nowgit status --porcelain --untracked-files=no -- <context>, so staged changes count too),git archiveextraction, anddocker build --load --provenance=false --platform linux/amd64. Under cf it gets pull credentials withcf containers registries credentials generate <registry> --permissions pull(the stdout carrying the password is captured, never printed), finds the digest with aHEADon the manifest, and pushes withcf containers push --tag. All contexts are checked before the first build. The registry host isCLOUDFLARE_CONTAINER_REGISTRYwhen set, because cf pushes and validates against it.packages/wb/src/utils/cloudflareConfig.tsreadscontainersfrom the evaluated config intocontainerImages(named as cf names their repositories: the Container name, or<Container>-<image>).packages/wb/src/commands/deploy.tsresolves the references after secrets and limits are checked and beforegen-codeand the build, passes them only to the build, and passes the effective rollout to bothcf deploycalls.--dry-runchecks the contexts and prints the tag each would use, without registry access.Check hardest: that
nonenever uploads the placeholder. This rests on reading cf's deploy code (withnone, it skips preparing images and sends only Container names and classes, taken from the deployed version or left without images on a first deploy), not on a real deploy.Testing
bun run verify.bun wb test test/unit/cloudflareContainerImages.test.ts test/unit/deploy.test.ts test/unit/cloudflareWorker.test.ts. The new tests runwb deploywith the realcf buildon a Git fixture with a Durable Object-managed Container, against a loopback Cloudflare API (CLOUDFLARE_API_BASE_URL, answered by the realcf containers registries credentials generate) and a loopback HTTPS registry. Onlydocker,cf containers push, andcf deployare stubs. They cover: a miss builds once from the committed tree (untracked file excluded) and the Build Output holds the published digest; the next deploy builds nothing; a committed change builds again; an uncommitted change stops before any build, lookup, orcf build;noneand an omitted flag run no Docker build and no lookup and deploy with--containers-rollout=none; a config ignoring the variable stops before the build.docker buildarguments wb uses produce alinux/amd64image locally, whichcf containers pushrequires. Realcf deploy --prebuilt --dry-runaccepts placeholder references withnoneandgradual.registry.cloudflare.com, a real push, and a real deploy (no Cloudflare resources were touched). To verify on a real project: change itscloudflare.config.tsas above, commit, and runWB_ENV=staging bun wb deploy --containers-rollout=gradualtwice. The first run builds and pusheswb/<name>:source-<key>; the second printsReusing the published container imagewith nodocker build;.cloudflare/output/v1/containers/*/container.config.jsonholds aregistry.cloudflare.com/<account>/wb/...@sha256:reference. Then runbun wb deploy --containers-rollout=nonewith Docker stopped and check in the dashboard that the Container image is unchanged.Notes
--containers-rollout, a Worker with any container image is now deployed with--containers-rollout=noneinstead of cf's default rollout, so its images, including ones declared only by{ reference }, change only withgradualorimmediate.