Skip to content

Commit 4ff07f4

Browse files
committed
Use wrangler outputs for version upload and wrangler deploy
1 parent 4fb15f8 commit 4ff07f4

File tree

5 files changed

+362
-119
lines changed

5 files changed

+362
-119
lines changed

.changeset/real-bananas-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler-action": minor
3+
---
4+
5+
Use wrangler outputs for version upload and wrangler deploy

src/commandOutputParsing.ts

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { setOutput } from "@actions/core";
2+
import { info, WranglerActionConfig } from "./wranglerAction";
3+
import {
4+
getOutputEntry,
5+
OutputEntryDeployment,
6+
OutputEntryPagesDeployment,
7+
OutputEntryVersionUpload,
8+
} from "./wranglerArtifactManager";
9+
import { createGitHubDeploymentAndJobSummary } from "./service/github";
10+
11+
// fallback to trying to extract the deployment-url and pages-deployment-alias-url from stdout for wranglerVersion < 3.81.0
12+
function extractDeploymentUrlsFromStdout(stdOut: string): {
13+
deploymentUrl?: string;
14+
aliasUrl?: string;
15+
} {
16+
let deploymentUrl = "";
17+
let aliasUrl = "";
18+
19+
// Try to extract the deployment URL
20+
const deploymentUrlMatch = stdOut.match(/https?:\/\/[a-zA-Z0-9-./]+/);
21+
if (deploymentUrlMatch && deploymentUrlMatch[0]) {
22+
deploymentUrl = deploymentUrlMatch[0].trim();
23+
}
24+
25+
// And also try to extract the alias URL (since wrangler@3.78.0)
26+
const aliasUrlMatch = stdOut.match(/alias URL: (https?:\/\/[a-zA-Z0-9-./]+)/);
27+
if (aliasUrlMatch && aliasUrlMatch[1]) {
28+
aliasUrl = aliasUrlMatch[1].trim();
29+
}
30+
31+
return { deploymentUrl, aliasUrl };
32+
}
33+
34+
async function handlePagesDeployOutputEntry(
35+
config: WranglerActionConfig,
36+
pagesDeployOutputEntry: OutputEntryPagesDeployment,
37+
) {
38+
setOutput("deployment-url", pagesDeployOutputEntry.url);
39+
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
40+
setOutput("deployment-alias-url", pagesDeployOutputEntry.alias);
41+
setOutput("pages-deployment-alias-url", pagesDeployOutputEntry.alias);
42+
setOutput("pages-deployment-id", pagesDeployOutputEntry.deployment_id);
43+
setOutput("pages-environment", pagesDeployOutputEntry.environment);
44+
45+
// Create github deployment, if GITHUB_TOKEN is present in config
46+
await createGitHubDeploymentAndJobSummary(config, pagesDeployOutputEntry);
47+
}
48+
49+
/**
50+
* If no wrangler output file found, fallback to extracting deployment-url from stdout.
51+
* @deprecated Use {@link handlePagesDeployOutputEntry} instead.
52+
*/
53+
function handlePagesDeployCommand(
54+
config: WranglerActionConfig,
55+
stdOut: string,
56+
) {
57+
info(
58+
config,
59+
"Unable to find a WRANGLER_OUTPUT_DIR, environment and id fields will be unavailable for output. Have you updated wrangler to version >=3.81.0?",
60+
);
61+
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
62+
const { deploymentUrl, aliasUrl } = extractDeploymentUrlsFromStdout(stdOut);
63+
64+
setOutput("deployment-url", deploymentUrl);
65+
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
66+
setOutput("deployment-alias-url", aliasUrl);
67+
setOutput("pages-deployment-alias-url", aliasUrl);
68+
}
69+
70+
function handleWranglerDeployOutputEntry(
71+
config: WranglerActionConfig,
72+
wranglerDeployOutputEntry: OutputEntryDeployment,
73+
) {
74+
// If no deployment urls found in wrangler output file, log that we couldn't find any urls and return.
75+
if (
76+
!wranglerDeployOutputEntry.targets ||
77+
wranglerDeployOutputEntry.targets.length === 0
78+
) {
79+
info(config, "No deployment-url found in wrangler deploy output file");
80+
return;
81+
}
82+
83+
// If more than 1 deployment url found, log that we're going to set deployment-url to the first match.
84+
// In a future wrangler-action version we should consider how we're going to output multiple deployment-urls
85+
if (wranglerDeployOutputEntry.targets.length > 1) {
86+
info(
87+
config,
88+
"Multiple deployment urls found in wrangler deploy output file, deployment-url will be set to the first url",
89+
);
90+
}
91+
92+
setOutput("deployment-url", wranglerDeployOutputEntry.targets[0]);
93+
}
94+
95+
/**
96+
* If no wrangler output file found, fallback to extracting deployment-url from stdout.
97+
* @deprecated Use {@link handleWranglerDeployOutputEntry} instead.
98+
*/
99+
function handleWranglerDeployCommand(
100+
config: WranglerActionConfig,
101+
stdOut: string,
102+
) {
103+
info(
104+
config,
105+
"Unable to find a WRANGLER_OUTPUT_DIR, deployment-url may have an unreliable output. Have you updated wrangler to version >=3.88.0?",
106+
);
107+
const { deploymentUrl } = extractDeploymentUrlsFromStdout(stdOut);
108+
setOutput("deployment-url", deploymentUrl);
109+
}
110+
111+
function handleVersionsUploadOutputEntry(
112+
versionsOutputEntry: OutputEntryVersionUpload,
113+
) {
114+
setOutput("deployment-url", versionsOutputEntry.preview_url);
115+
}
116+
117+
/**
118+
* If no wrangler output file found, log a message stating deployment-url will be unavailable for output.
119+
* @deprecated Use {@link handleVersionsOutputEntry} instead.
120+
*/
121+
function handleVersionsOutputCommand(config: WranglerActionConfig) {
122+
info(
123+
config,
124+
"Unable to find a WRANGLER_OUTPUT_DIR, deployment-url will be unavailable for output. Have you updated wrangler to version >=3.88.0?",
125+
);
126+
}
127+
128+
function handleDeprectatedStdoutParsing(
129+
config: WranglerActionConfig,
130+
command: string,
131+
stdOut: string,
132+
) {
133+
// Check if this command is a pages deployment
134+
if (
135+
command.startsWith("pages deploy") ||
136+
command.startsWith("pages publish")
137+
) {
138+
handlePagesDeployCommand(config, stdOut);
139+
return;
140+
}
141+
142+
// Check if this command is a workers deployment
143+
if (command.startsWith("deploy") || command.startsWith("publish")) {
144+
handleWranglerDeployCommand(config, stdOut);
145+
return;
146+
}
147+
148+
// Check if this command is a versions deployment
149+
if (command.startsWith("versions upload")) {
150+
handleVersionsOutputCommand(config);
151+
return;
152+
}
153+
}
154+
155+
export async function handleCommandOutputParsing(
156+
config: WranglerActionConfig,
157+
command: string,
158+
stdOut: string,
159+
) {
160+
// get first OutputEntry found within wrangler artifact output directory
161+
const outputEntry = await getOutputEntry(config.WRANGLER_OUTPUT_DIR);
162+
163+
if (outputEntry === null) {
164+
// if no outputEntry found, fallback to deprecated stdOut parsing
165+
handleDeprectatedStdoutParsing(config, command, stdOut);
166+
return;
167+
}
168+
169+
switch (outputEntry.type) {
170+
case "pages-deploy-detailed":
171+
await handlePagesDeployOutputEntry(config, outputEntry);
172+
break;
173+
case "deploy":
174+
handleWranglerDeployOutputEntry(config, outputEntry);
175+
break;
176+
case "version-upload":
177+
handleVersionsUploadOutputEntry(outputEntry);
178+
break;
179+
}
180+
}

src/wranglerAction.ts

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { z } from "zod";
1212
import { exec, execShell } from "./exec";
1313
import { PackageManager } from "./packageManagers";
1414
import { error, info, semverCompare } from "./utils";
15-
import { getDetailedPagesDeployOutput } from "./wranglerArtifactManager";
16-
import { createGitHubDeploymentAndJobSummary } from "./service/github";
15+
import { handleCommandOutputParsing } from "./commandOutputParsing";
1716

1817
export type WranglerActionConfig = z.infer<typeof wranglerActionConfig>;
1918
export const wranglerActionConfig = z.object({
@@ -294,29 +293,6 @@ async function uploadSecrets(
294293
}
295294
}
296295

297-
// fallback to trying to extract the deployment-url and pages-deployment-alias-url from stdout for wranglerVersion < 3.81.0
298-
function extractDeploymentUrlsFromStdout(stdOut: string): {
299-
deploymentUrl?: string;
300-
aliasUrl?: string;
301-
} {
302-
let deploymentUrl = "";
303-
let aliasUrl = "";
304-
305-
// Try to extract the deployment URL
306-
const deploymentUrlMatch = stdOut.match(/https?:\/\/[a-zA-Z0-9-./]+/);
307-
if (deploymentUrlMatch && deploymentUrlMatch[0]) {
308-
deploymentUrl = deploymentUrlMatch[0].trim();
309-
}
310-
311-
// And also try to extract the alias URL (since wrangler@3.78.0)
312-
const aliasUrlMatch = stdOut.match(/alias URL: (https?:\/\/[a-zA-Z0-9-./]+)/);
313-
if (aliasUrlMatch && aliasUrlMatch[1]) {
314-
aliasUrl = aliasUrlMatch[1].trim();
315-
}
316-
317-
return { deploymentUrl, aliasUrl };
318-
}
319-
320296
async function wranglerCommands(
321297
config: WranglerActionConfig,
322298
packageManager: PackageManager,
@@ -379,47 +355,8 @@ async function wranglerCommands(
379355
setOutput("command-output", stdOut);
380356
setOutput("command-stderr", stdErr);
381357

382-
// Check if this command is a workers deployment
383-
if (command.startsWith("deploy") || command.startsWith("publish")) {
384-
const { deploymentUrl } = extractDeploymentUrlsFromStdout(stdOut);
385-
setOutput("deployment-url", deploymentUrl);
386-
}
387-
// Check if this command is a pages deployment
388-
if (
389-
command.startsWith("pages publish") ||
390-
command.startsWith("pages deploy")
391-
) {
392-
const pagesArtifactFields = await getDetailedPagesDeployOutput(
393-
config.WRANGLER_OUTPUT_DIR,
394-
);
395-
396-
if (pagesArtifactFields) {
397-
setOutput("deployment-url", pagesArtifactFields.url);
398-
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
399-
setOutput("deployment-alias-url", pagesArtifactFields.alias);
400-
setOutput("pages-deployment-alias-url", pagesArtifactFields.alias);
401-
setOutput("pages-deployment-id", pagesArtifactFields.deployment_id);
402-
setOutput("pages-environment", pagesArtifactFields.environment);
403-
// Create github deployment, if GITHUB_TOKEN is present in config
404-
await createGitHubDeploymentAndJobSummary(
405-
config,
406-
pagesArtifactFields,
407-
);
408-
} else {
409-
info(
410-
config,
411-
"Unable to find a WRANGLER_OUTPUT_DIR, environment and id fields will be unavailable for output. Have you updated wrangler to version >=3.81.0?",
412-
);
413-
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
414-
const { deploymentUrl, aliasUrl } =
415-
extractDeploymentUrlsFromStdout(stdOut);
416-
417-
setOutput("deployment-url", deploymentUrl);
418-
// DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change
419-
setOutput("deployment-alias-url", aliasUrl);
420-
setOutput("pages-deployment-alias-url", aliasUrl);
421-
}
422-
}
358+
// Handles setting github action outputs and creating github deployment and job summary
359+
await handleCommandOutputParsing(config, command, stdOut);
423360
}
424361
} finally {
425362
endGroup(config);

0 commit comments

Comments
 (0)