-
Notifications
You must be signed in to change notification settings - Fork 769
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
Add build and push subcommands to cloudchamber #7378
Open
IRCody
wants to merge
1
commit into
cloudflare:main
Choose a base branch
from
IRCody:cloudchamber-build
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
Add build and push helper sub-commands under the cloudchamber command. |
45 changes: 45 additions & 0 deletions
45
packages/wrangler/src/__tests__/cloudchamber/build.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { constructBuildCommand } from "../../cloudchamber/build"; | ||
|
||
describe("cloudchamber build", () => { | ||
describe("build command generation", () => { | ||
it("should work with no build command set", async () => { | ||
const bc = await constructBuildCommand({ | ||
imageTag: "test-registry/no-bc:v1", | ||
pathToDockerfile: "bogus/path", | ||
}); | ||
expect(bc).toEqual( | ||
"docker build -t registry.cloudchamber.cfdata.org/test-registry/no-bc:v1 --platform linux/amd64 bogus/path" | ||
); | ||
}); | ||
|
||
it("should error if dockerfile provided without a tag", async () => { | ||
await expect( | ||
constructBuildCommand({ | ||
pathToDockerfile: "bogus/path", | ||
}) | ||
).rejects.toThrowError(); | ||
}); | ||
|
||
it("should respect a custom path to docker", async () => { | ||
const bc = await constructBuildCommand({ | ||
pathToDocker: "/my/special/path/docker", | ||
imageTag: "test-registry/no-bc:v1", | ||
pathToDockerfile: "bogus/path", | ||
}); | ||
expect(bc).toEqual( | ||
"/my/special/path/docker build -t registry.cloudchamber.cfdata.org/test-registry/no-bc:v1 --platform linux/amd64 bogus/path" | ||
); | ||
}); | ||
|
||
it("should respect passed in platform", async () => { | ||
const bc = await constructBuildCommand({ | ||
imageTag: "test-registry/no-bc:v1", | ||
pathToDockerfile: "bogus/path", | ||
platform: "linux/arm64", | ||
}); | ||
expect(bc).toEqual( | ||
"docker build -t registry.cloudchamber.cfdata.org/test-registry/no-bc:v1 --platform linux/arm64 bogus/path" | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { spawn } from "child_process"; | ||
import { logRaw } from "@cloudflare/cli"; | ||
import { ImageRegistriesService } from "./client"; | ||
import type { Config } from "../config"; | ||
import type { | ||
CommonYargsArgvJSON, | ||
StrictYargsOptionsToInterfaceJSON, | ||
} from "../yargs-types"; | ||
import type { ImageRegistryPermissions } from "./client"; | ||
|
||
// default cloudflare managed registry | ||
const domain = "registry.cloudchamber.cfdata.org"; | ||
|
||
export async function dockerLoginManagedRegistry(options: { | ||
pathToDocker?: string; | ||
}) { | ||
const dockerPath = options.pathToDocker ?? "docker"; | ||
const expirationMinutes = 15; | ||
|
||
await ImageRegistriesService.generateImageRegistryCredentials(domain, { | ||
expiration_minutes: expirationMinutes, | ||
permissions: ["push"] as ImageRegistryPermissions[], | ||
}).then(async (credentials) => { | ||
const child = spawn( | ||
dockerPath, | ||
["login", "--password-stdin", "--username", "v1", domain], | ||
{ stdio: ["pipe", "inherit", "inherit"] } | ||
).on("error", (err) => { | ||
throw err; | ||
}); | ||
child.stdin.write(credentials.password); | ||
child.stdin.end(); | ||
await new Promise((resolve) => { | ||
child.on("close", resolve); | ||
}); | ||
}); | ||
} | ||
|
||
export async function constructBuildCommand(options: { | ||
imageTag?: string; | ||
pathToDocker?: string; | ||
pathToDockerfile?: string; | ||
platform?: string; | ||
}) { | ||
// require a tag if we provide dockerfile | ||
if ( | ||
typeof options.pathToDockerfile !== "undefined" && | ||
options.pathToDockerfile !== "" && | ||
(typeof options.imageTag === "undefined" || options.imageTag === "") | ||
) { | ||
throw new Error("must provide an image tag if providing a docker file"); | ||
} | ||
const dockerFilePath = options.pathToDockerfile; | ||
const dockerPath = options.pathToDocker ?? "docker"; | ||
const imageTag = domain + "/" + options.imageTag; | ||
const platform = options.platform ? options.platform : "linux/amd64"; | ||
const defaultBuildCommand = [ | ||
dockerPath, | ||
"build", | ||
"-t", | ||
imageTag, | ||
"--platform", | ||
platform, | ||
dockerFilePath, | ||
].join(" "); | ||
|
||
return defaultBuildCommand; | ||
} | ||
|
||
// 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); | ||
}); | ||
} | ||
|
||
async function tagImage(original: string, newTag: string, dockerPath: string) { | ||
const child = spawn(dockerPath, ["tag", original, newTag]).on( | ||
"error", | ||
(err) => { | ||
throw err; | ||
} | ||
); | ||
await new Promise((resolve) => { | ||
child.on("close", resolve); | ||
}); | ||
} | ||
|
||
export async function push(options: { | ||
imageTag?: string; | ||
pathToDocker?: string; | ||
}) { | ||
if (typeof options.imageTag === "undefined") { | ||
throw new Error("Must provide an image tag when pushing"); | ||
} | ||
// TODO: handle non-managed registry? | ||
const imageTag = domain + "/" + options.imageTag; | ||
const dockerPath = options.pathToDocker ?? "docker"; | ||
await tagImage(options.imageTag, imageTag, dockerPath); | ||
const child = spawn(dockerPath, ["image", "push", imageTag], { | ||
stdio: "inherit", | ||
}).on("error", (err) => { | ||
throw err; | ||
}); | ||
await new Promise((resolve) => { | ||
child.on("close", resolve); | ||
}); | ||
} | ||
|
||
export function buildYargs(yargs: CommonYargsArgvJSON) { | ||
return yargs | ||
.positional("PATH", { | ||
type: "string", | ||
describe: "Path for the directory containing the Dockerfile to build", | ||
demandOption: true, | ||
}) | ||
.option("tag", { | ||
alias: "t", | ||
type: "string", | ||
demandOption: true, | ||
describe: 'Name and optionally a tag (format: "name:tag")', | ||
}) | ||
.option("path-to-docker", { | ||
type: "string", | ||
default: "docker", | ||
describe: "Path to your docker binary if it's not on $PATH", | ||
demandOption: false, | ||
}) | ||
.option("push", { | ||
alias: "p", | ||
type: "boolean", | ||
describe: "Push the built image to Cloudflare's managed registry", | ||
default: false, | ||
}) | ||
.option("platform", { | ||
type: "string", | ||
default: "linux/amd64", | ||
describe: | ||
"Platform to build for. Defaults to the architecture support by Workers (linux/amd64)", | ||
demandOption: false, | ||
}); | ||
} | ||
|
||
export function pushYargs(yargs: CommonYargsArgvJSON) { | ||
return yargs | ||
.option("path-to-docker", { | ||
type: "string", | ||
default: "docker", | ||
describe: "Path to your docker binary if it's not on $PATH", | ||
demandOption: false, | ||
}) | ||
.positional("TAG", { type: "string", demandOption: true }); | ||
} | ||
|
||
export async function buildCommand( | ||
args: StrictYargsOptionsToInterfaceJSON<typeof buildYargs>, | ||
_: Config | ||
) { | ||
try { | ||
await constructBuildCommand({ | ||
imageTag: args.tag, | ||
pathToDockerfile: args.PATH, | ||
pathToDocker: args.pathToDocker, | ||
}) | ||
.then(async (bc) => dockerBuild({ buildCmd: bc })) | ||
.then(async () => { | ||
if (args.push) { | ||
await dockerLoginManagedRegistry({ | ||
pathToDocker: args.pathToDocker, | ||
}).then(async () => { | ||
await push({ imageTag: args.tag }); | ||
}); | ||
} | ||
}); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
logRaw(error.message); | ||
} else { | ||
logRaw("An unknown error occurred"); | ||
} | ||
} | ||
} | ||
|
||
export async function pushCommand( | ||
args: StrictYargsOptionsToInterfaceJSON<typeof pushYargs>, | ||
_: Config | ||
) { | ||
try { | ||
await dockerLoginManagedRegistry({ | ||
pathToDocker: args.pathToDocker, | ||
}).then(async () => { | ||
await push({ imageTag: args.TAG }); | ||
}); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
logRaw(error.message); | ||
} else { | ||
logRaw("An unknown error occurred"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the default whatever the user is using (e.g.
linux/arm64
on an M1 Mac andlinux/amd64
on x86)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning for defaulting to linux/amd64 is that is the only supported platform in cc today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah +1 to using amd64 as the default.
Shoudl we say in the description of build that it defaults to "architecture supported by Workers (linux/amd64)"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default shows up in the help text:
We could add the extra text to the description I guess?