|
| 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 | +} |
0 commit comments