Skip to content
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

Include Version Preview URL in Wrangler output file #7243

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-beds-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Include Version Preview URL in Wrangler's output file
30 changes: 27 additions & 3 deletions packages/wrangler/e2e/versions.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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}"
Expand Down Expand Up @@ -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`);

Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -313,6 +313,7 @@ async function versionsUploadHandler(
worker_name: name ?? null,
worker_tag: workerTag,
version_id: versionId,
preview_url: versionPreviewUrl,
});
}

Expand Down
17 changes: 10 additions & 7 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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}`);
}
}

Expand All @@ -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(
Expand Down
Loading