diff --git a/.changeset/mighty-beds-grab.md b/.changeset/mighty-beds-grab.md new file mode 100644 index 000000000000..80503b13836b --- /dev/null +++ b/.changeset/mighty-beds-grab.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Include Version Preview URL in Wrangler's output file diff --git a/packages/wrangler/e2e/versions.test.ts b/packages/wrangler/e2e/versions.test.ts index d982e4ff0c13..67a460d60ce5 100644 --- a/packages/wrangler/e2e/versions.test.ts +++ b/packages/wrangler/e2e/versions.test.ts @@ -1,5 +1,7 @@ +import { readFile } from "fs/promises"; +import path from "path"; import dedent from "ts-dedent"; -import { describe, expect, it } from "vitest"; +import { beforeAll, describe, expect, it } from "vitest"; import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id"; import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test"; import { generateResourceName } from "./helpers/generate-resource-name"; @@ -23,9 +25,10 @@ describe("versions deploy", { timeout: TIMEOUT }, () => { let versionId0: string; let versionId1: string; let versionId2: string; - const helper = new WranglerE2ETestHelper(); + let helper: WranglerE2ETestHelper; - it("deploy worker", async () => { + beforeAll(async () => { + helper = new WranglerE2ETestHelper(); await helper.seed({ "wrangler.toml": dedent` name = "${workerName}" @@ -632,6 +635,27 @@ describe("versions deploy", { timeout: TIMEOUT }, () => { `); }); + it("should include version preview url in output file", async () => { + const outputFile = path.join(helper.tmpPath, "output.jsonnd"); + const upload = await helper.run( + `wrangler versions upload --message "Upload via e2e test" --tag "e2e-upload" --x-versions`, + { + env: { + ...process.env, + WRANGLER_OUTPUT_FILE_PATH: outputFile, + }, + } + ); + + versionId1 = matchVersionId(upload.stdout); + + const output = await readFile(outputFile, "utf8"); + + expect(JSON.parse(normalizeOutput(output.split("\n")[1]))).toMatchObject({ + preview_url: "https://tmp-e2e-worker-PREVIEW-URL.SUBDOMAIN.workers.dev", + }); + }); + it("should delete Worker", async () => { const { stdout } = await helper.run(`wrangler delete`); diff --git a/packages/wrangler/src/output.ts b/packages/wrangler/src/output.ts index 7a85768edabb..d4297cc94a9c 100644 --- a/packages/wrangler/src/output.ts +++ b/packages/wrangler/src/output.ts @@ -128,6 +128,8 @@ interface OutputEntryVersionUpload extends OutputEntryBase<"version-upload"> { worker_tag: string | null; /** A GUID that identifies this uploaded, but not yet deployed, version of the Worker. This version will need to be "deployed" to receive traffic. */ version_id: string | null; + /** The preview URL associated with this version upload */ + preview_url: string | undefined; } interface OutputEntryVersionDeployment diff --git a/packages/wrangler/src/versions/index.ts b/packages/wrangler/src/versions/index.ts index 4c91ef2f6d1d..cec8c4793333 100644 --- a/packages/wrangler/src/versions/index.ts +++ b/packages/wrangler/src/versions/index.ts @@ -274,7 +274,7 @@ async function versionsUploadHandler( if (!args.dryRun) { await standardPricingWarning(config); } - const { versionId, workerTag } = await versionsUpload({ + const { versionId, workerTag, versionPreviewUrl } = await versionsUpload({ config, accountId, name, @@ -313,6 +313,7 @@ async function versionsUploadHandler( worker_name: name ?? null, worker_tag: workerTag, version_id: versionId, + preview_url: versionPreviewUrl, }); } diff --git a/packages/wrangler/src/versions/upload.ts b/packages/wrangler/src/versions/upload.ts index b5c0a60d8abc..3811303591da 100644 --- a/packages/wrangler/src/versions/upload.ts +++ b/packages/wrangler/src/versions/upload.ts @@ -112,9 +112,11 @@ function errIsStartupErr(err: unknown): err is ParseError & { code: 10021 } { return false; } -export default async function versionsUpload( - props: Props -): Promise<{ versionId: string | null; workerTag: string | null }> { +export default async function versionsUpload(props: Props): Promise<{ + versionId: string | null; + workerTag: string | null; + versionPreviewUrl?: string | undefined; +}> { // TODO: warn if git/hg has uncommitted changes const { config, accountId, name } = props; let versionId: string | null = null; @@ -565,6 +567,8 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m logger.log("Uploaded", workerName, formatTime(uploadMs)); logger.log("Worker Version ID:", versionId); + let versionPreviewUrl: string | undefined = undefined; + if (versionId && hasPreview) { const { enabled: available_on_subdomain } = await fetchResult<{ enabled: boolean; @@ -573,9 +577,8 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m if (available_on_subdomain) { const userSubdomain = await getWorkersDevSubdomain(accountId); const shortVersion = versionId.slice(0, 8); - logger.log( - `Version Preview URL: https://${shortVersion}-${workerName}.${userSubdomain}.workers.dev` - ); + versionPreviewUrl = `https://${shortVersion}-${workerName}.${userSubdomain}.workers.dev`; + logger.log(`Version Preview URL: ${versionPreviewUrl}`); } } @@ -591,7 +594,7 @@ Changes to triggers (routes, custom domains, cron schedules, etc) must be applie `) ); - return { versionId, workerTag }; + return { versionId, workerTag, versionPreviewUrl }; } function helpIfErrorIsSizeOrScriptStartup(