diff --git a/.changeset/previews-deploy-secrets-file.md b/.changeset/previews-deploy-secrets-file.md new file mode 100644 index 0000000000..5b43365334 --- /dev/null +++ b/.changeset/previews-deploy-secrets-file.md @@ -0,0 +1,7 @@ +--- +"cf": minor +--- + +Add `--secrets-file` to `cf previews deploy` + +Use `cf previews deploy --secrets-file .env.preview` to upload secrets with a Preview deployment, in the same JSON or `.env` format that `cf deploy --secrets-file` accepts. Previously, Preview secrets could only be set in the Cloudflare dashboard or with Wrangler. diff --git a/packages/cli/src/__tests__/commands/hand-written-metadata.test.ts b/packages/cli/src/__tests__/commands/hand-written-metadata.test.ts index a72d2a78b0..3c4ef3926f 100644 --- a/packages/cli/src/__tests__/commands/hand-written-metadata.test.ts +++ b/packages/cli/src/__tests__/commands/hand-written-metadata.test.ts @@ -186,6 +186,17 @@ describe("hand-written command metadata", () => { }); }); + it("publishes Preview secrets-file metadata", () => { + expect(commands.get("cf previews deploy")?.options).toContainEqual({ + name: "secrets-file", + type: "string", + required: false, + description: expect.stringMatching( + /^Path to a file containing secrets to upload with the Preview deployment / + ), + }); + }); + it("publishes Quick Tunnel email protection for completions and tools", () => { expect(commands.get("cf tunnels quick-start")?.options).toContainEqual( expect.objectContaining({ diff --git a/packages/cli/src/__tests__/commands/previews-deploy.test.ts b/packages/cli/src/__tests__/commands/previews-deploy.test.ts index 5cd3bb1ae0..7e5de90b8b 100644 --- a/packages/cli/src/__tests__/commands/previews-deploy.test.ts +++ b/packages/cli/src/__tests__/commands/previews-deploy.test.ts @@ -283,6 +283,38 @@ describe("cf previews deploy", () => { expect(auxiliary.config.name).toBe("auxiliary-worker"); }); + it("uploads secrets from --secrets-file", async () => { + const rootConfig = { buildContext: { isPreview: true } }; + const worker = { + config: { type: "worker", name: "preview-worker" }, + bundleDir: "/project/bundle", + }; + mocks.readBuildOutput.mockResolvedValue({ + rootConfig, + workers: { default: worker }, + }); + mocks.assembleBuildResult.mockReturnValue({ content: "worker code" }); + mocks.getAuthToken.mockResolvedValue("token"); + mocks.getAccountId.mockResolvedValue("account-id"); + mocks.createDeployContext.mockReturnValue({ + logger: { log: vi.fn(), info: vi.fn() }, + }); + mocks.previewBuildOutput.mockResolvedValue(previewResult); + + await runPreviewDeploy({ + "preview-name": "feature", + prebuilt: true, + "secrets-file": ".env.preview", + } as Parameters[0]); + + expect(mocks.previewBuildOutput).toHaveBeenCalledWith( + "account-id", + { name: "feature", json: true, secretsFile: ".env.preview" }, + expect.any(Object), + expect.any(Object) + ); + }); + it("uses the built region to select an account when output omits its ID", async () => { const worker = { config: { diff --git a/packages/cli/src/commands/_generated/_meta/commands.json b/packages/cli/src/commands/_generated/_meta/commands.json index 177b2fbcd6..8695c150c4 100644 --- a/packages/cli/src/commands/_generated/_meta/commands.json +++ b/packages/cli/src/commands/_generated/_meta/commands.json @@ -90303,6 +90303,12 @@ "required": false, "description": "Name of the Worker in the Build Output to use (defaults to the default Worker)" }, + { + "name": "secrets-file", + "type": "string", + "required": false, + "description": "Path to a file containing secrets to upload with the Preview deployment (JSON or .env format). Dotenv values expand $VAR and ${VAR} using process environment values and earlier file values; process environment values override file values, including empty values. Only declared file keys are uploaded. Only use trusted dotenv secrets files: they can read and upload any process environment value. Escape literal dollar signs with \\$ or use JSON." + }, { "name": "dry-run", "type": "boolean", diff --git a/packages/cli/src/commands/_generated/_meta/hand-written-commands.json b/packages/cli/src/commands/_generated/_meta/hand-written-commands.json index 94e052188b..4154b0be04 100644 --- a/packages/cli/src/commands/_generated/_meta/hand-written-commands.json +++ b/packages/cli/src/commands/_generated/_meta/hand-written-commands.json @@ -1561,6 +1561,12 @@ "required": false, "description": "Name of the Worker in the Build Output to use (defaults to the default Worker)" }, + { + "name": "secrets-file", + "type": "string", + "required": false, + "description": "Path to a file containing secrets to upload with the Preview deployment (JSON or .env format). Dotenv values expand $VAR and ${VAR} using process environment values and earlier file values; process environment values override file values, including empty values. Only declared file keys are uploaded. Only use trusted dotenv secrets files: they can read and upload any process environment value. Escape literal dollar signs with \\$ or use JSON." + }, { "name": "dry-run", "type": "boolean", diff --git a/packages/cli/src/commands/deploy/secrets-file-description.ts b/packages/cli/src/commands/deploy/secrets-file-description.ts index 60d17b2d1e..9be91772af 100644 --- a/packages/cli/src/commands/deploy/secrets-file-description.ts +++ b/packages/cli/src/commands/deploy/secrets-file-description.ts @@ -1,2 +1,8 @@ -export const secretsFileDescription = - "Path to a file containing secrets to upload with the version (JSON or .env format). Dotenv values expand $VAR and ${VAR} using process environment values and earlier file values; process environment values override file values, including empty values. Only declared file keys are uploaded. Only use trusted dotenv secrets files: they can read and upload any process environment value. Escape literal dollar signs with \\$ or use JSON."; +function describeSecretsFile(upload: string) { + return `Path to a file containing secrets to upload with the ${upload} (JSON or .env format). Dotenv values expand $VAR and \${VAR} using process environment values and earlier file values; process environment values override file values, including empty values. Only declared file keys are uploaded. Only use trusted dotenv secrets files: they can read and upload any process environment value. Escape literal dollar signs with \\$ or use JSON.`; +} + +export const secretsFileDescription = describeSecretsFile("version"); + +export const previewSecretsFileDescription = + describeSecretsFile("Preview deployment"); diff --git a/packages/cli/src/commands/previews/deploy.ts b/packages/cli/src/commands/previews/deploy.ts index 3b93372ca5..cd9028ea2a 100644 --- a/packages/cli/src/commands/previews/deploy.ts +++ b/packages/cli/src/commands/previews/deploy.ts @@ -34,6 +34,7 @@ import { createContainerDeployConfig, isLiveContainerExport, } from "../deploy/containers.js"; +import { previewSecretsFileDescription } from "../deploy/secrets-file-description.js"; import { prepareStaticAssetsBuildOutput, staticAssetsDirectoryOption, @@ -57,7 +58,12 @@ function builder(yargs: Argv) { "Use existing Preview Build Output Specification files without building", default: false, }) - .option("worker", buildOutputWorkerOption); + .option("worker", buildOutputWorkerOption) + .option("secrets-file", { + type: "string", + description: previewSecretsFileDescription, + requiresArg: true, + }); } type PreviewDeployArgs = InferArgs; @@ -188,7 +194,7 @@ async function deployPreviewBuildOutput( const result = await previewBuildOutput( accountId, - { name: previewName, json: true }, + { name: previewName, json: true, secretsFile: argv["secrets-file"] }, { workerConfig: builtConfig, rootConfig: output.rootConfig, diff --git a/packages/cli/src/commands/previews/deploy/meta.ts b/packages/cli/src/commands/previews/deploy/meta.ts index 8d8b8d5375..099aefb7ea 100644 --- a/packages/cli/src/commands/previews/deploy/meta.ts +++ b/packages/cli/src/commands/previews/deploy/meta.ts @@ -1,3 +1,4 @@ +import { previewSecretsFileDescription } from "../../deploy/secrets-file-description.js"; import type { LeafCommandMeta } from "../../../lib/command-meta.js"; export default { @@ -40,6 +41,12 @@ export default { description: "Name of the Worker in the Build Output to use (defaults to the default Worker)", }, + { + name: "secrets-file", + type: "string", + required: false, + description: previewSecretsFileDescription, + }, { name: "dry-run", type: "boolean",