diff --git a/.changeset/preview-build-output-secrets-file.md b/.changeset/preview-build-output-secrets-file.md new file mode 100644 index 00000000000..8004429a555 --- /dev/null +++ b/.changeset/preview-build-output-secrets-file.md @@ -0,0 +1,7 @@ +--- +"@cloudflare/deploy-helpers": minor +--- + +Accept a secrets file when uploading a Preview from Build Output + +`previewBuildOutput()` now takes the same `secretsFile` option as `preview()`. The file's secrets are uploaded with the Preview deployment, so a CLI that deploys Previews from Build Output can set Preview secrets without a separate command or the Cloudflare dashboard. diff --git a/packages/deploy-helpers/src/preview/preview.ts b/packages/deploy-helpers/src/preview/preview.ts index 29f9ce8c88f..6d99c2578d1 100644 --- a/packages/deploy-helpers/src/preview/preview.ts +++ b/packages/deploy-helpers/src/preview/preview.ts @@ -1338,12 +1338,12 @@ export async function preview( * Upload a Preview from resolved Build Output configuration. * * @param accountId Account that owns the parent Worker. - * @param args Preview name and deployment annotations. + * @param args Preview name, deployment annotations, and secrets file. * @param buildOutput Exact configuration and artifacts emitted by the build. */ export async function previewBuildOutput( accountId: string, - args: Pick, + args: Pick, buildOutput: PreviewBuildOutput, options?: PreviewOptions ): Promise { diff --git a/packages/deploy-helpers/tests/preview-build-output.test.ts b/packages/deploy-helpers/tests/preview-build-output.test.ts index a2b4f1702e2..487b14dfcf4 100644 --- a/packages/deploy-helpers/tests/preview-build-output.test.ts +++ b/packages/deploy-helpers/tests/preview-build-output.test.ts @@ -654,6 +654,35 @@ describe("previewBuildOutput", () => { expect(request.env).toEqual({ API_KEY: { type: "inherit" } }); }); + it("uploads secrets from a secrets file", async ({ expect }) => { + writeFileSync(".env.preview", "API_KEY=new-key\nTOKEN=token\n"); + mocks.getPreviewDeployment.mockResolvedValue({ + ...deploymentResource, + env: { API_KEY: { type: "secret_text" } }, + }); + mocks.createPreviewDeployment.mockResolvedValue({ + ...deploymentResource, + env: { TOKEN: { type: "secret_text", text: "token" } }, + }); + + const result = await previewBuildOutput( + "account-id", + { name: "feature", json: true, secretsFile: ".env.preview" }, + { + workerConfig: buildOutputConfig(), + rootConfig: validRootConfig, + buildResult, + } + ); + + const request = mocks.createPreviewDeployment.mock.calls[0]?.[4]; + expect(request.env).toEqual({ + API_KEY: { type: "secret_text", text: "new-key" }, + TOKEN: { type: "secret_text", text: "token" }, + }); + expect(result.deployment.env).toEqual({ TOKEN: { type: "secret_text" } }); + }); + it("does not look up the latest deployment of a new Preview", async ({ expect, }) => {