From b4191bfed218c6babf1f2b7ade772e2ef4e2fa72 Mon Sep 17 00:00:00 2001 From: Maximo Guk <62088388+Maximo-Guk@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:41:15 -0500 Subject: [PATCH] Moves wrangler output to tmpdir rather than /opt/ since /opt/ is owned by root. - Github self hosted runners may not have permissions to write to /opt/ - Also fallsback to trying to extract the deployment-url and deployment-alias-url from stdout when WRANGLER_OUTPUT_DIR is not specified --- .changeset/brave-wasps-greet.md | 5 +++ package-lock.json | 4 +- src/index.ts | 68 +++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 .changeset/brave-wasps-greet.md diff --git a/.changeset/brave-wasps-greet.md b/.changeset/brave-wasps-greet.md new file mode 100644 index 0000000..1768b88 --- /dev/null +++ b/.changeset/brave-wasps-greet.md @@ -0,0 +1,5 @@ +--- +"wrangler-action": minor +--- + +This reapplies [303](https://github.com/cloudflare/wrangler-action/pull/303) add parity with pages-action for pages deploy outputs diff --git a/package-lock.json b/package-lock.json index 087731f..7a44e29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wrangler-action", - "version": "3.9.0", + "version": "3.11.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "wrangler-action", - "version": "3.9.0", + "version": "3.11.0", "license": "MIT OR Apache-2.0", "dependencies": { "@actions/core": "^1.10.1", diff --git a/src/index.ts b/src/index.ts index f60c63d..2fb5d3f 100755 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,8 @@ import { exec, execShell } from "./exec"; import { getPackageManager } from "./packageManagers"; import { checkWorkingDirectory, semverCompare } from "./utils"; import { getDetailedPagesDeployOutput } from "./wranglerArtifactManager"; +import { join } from "path"; +import { tmpdir } from "os"; const DEFAULT_WRANGLER_VERSION = "3.81.0"; @@ -34,6 +36,7 @@ const config = { COMMANDS: getMultilineInput("command"), QUIET_MODE: getBooleanInput("quiet"), PACKAGE_MANAGER: getInput("packageManager"), + WRANGLER_OUTPUT_DIR: `${join(tmpdir(), "wranglerArtifacts")}`, } as const; const packageManager = getPackageManager(config.PACKAGE_MANAGER, { @@ -277,6 +280,28 @@ async function uploadSecrets() { } } +function extractDeploymentUrlsFromStdout(stdOut: string): { + deploymentUrl?: string; + aliasUrl?: string; +} { + let deploymentUrl = ""; + let aliasUrl = ""; + + // Try to extract the deployment URL + const deploymentUrlMatch = stdOut.match(/https?:\/\/[a-zA-Z0-9-./]+/); + if (deploymentUrlMatch && deploymentUrlMatch[0]) { + deploymentUrl = deploymentUrlMatch[0].trim(); + } + + // And also try to extract the alias URL (since wrangler@3.78.0) + const aliasUrlMatch = stdOut.match(/alias URL: (https?:\/\/[a-zA-Z0-9-./]+)/); + if (aliasUrlMatch && aliasUrlMatch[1]) { + aliasUrl = aliasUrlMatch[1].trim(); + } + + return { deploymentUrl, aliasUrl }; +} + async function wranglerCommands() { startGroup("🚀 Running Wrangler Commands"); try { @@ -313,9 +338,8 @@ async function wranglerCommands() { let stdOut = ""; let stdErr = ""; - // Construct the options for the exec command - const wranglerOutputDir = "/opt/wranglerArtifacts"; - process.env.WRANGLER_OUTPUT_FILE_DIRECTORY = wranglerOutputDir; + // set WRANGLER_OUTPUT_FILE_DIRECTORY env for exec + process.env.WRANGLER_OUTPUT_FILE_DIRECTORY = config.WRANGLER_OUTPUT_DIR; const options = { cwd: config["workingDirectory"], @@ -339,41 +363,35 @@ async function wranglerCommands() { // Check if this command is a workers deployment if (command.startsWith("deploy") || command.startsWith("publish")) { - // Try to extract the deployment URL - let deploymentUrl = ""; - const deploymentUrlMatch = stdOut.match(/https?:\/\/[a-zA-Z0-9-./]+/); - if (deploymentUrlMatch && deploymentUrlMatch[0]) { - deploymentUrl = deploymentUrlMatch[0].trim(); - setOutput("deployment-url", deploymentUrl); - } - - // And also try to extract the alias URL (since wrangler@3.78.0) - const aliasUrlMatch = stdOut.match( - /alias URL: (https?:\/\/[a-zA-Z0-9-./]+)/, - ); - if (aliasUrlMatch && aliasUrlMatch.length == 2 && aliasUrlMatch[1]) { - const aliasUrl = aliasUrlMatch[1].trim(); - setOutput("deployment-alias-url", aliasUrl); - } + const { deploymentUrl, aliasUrl } = + extractDeploymentUrlsFromStdout(stdOut); + setOutput("deployment-url", deploymentUrl); + setOutput("deployment-alias-url", aliasUrl); } // Check if this command is a pages deployment if ( command.startsWith("pages publish") || command.startsWith("pages deploy") ) { - const pagesArtifactFields = - await getDetailedPagesDeployOutput(wranglerOutputDir); + const pagesArtifactFields = await getDetailedPagesDeployOutput( + config.WRANGLER_OUTPUT_DIR, + ); if (pagesArtifactFields) { setOutput("id", pagesArtifactFields.deployment_id); - setOutput("url", pagesArtifactFields.url); - // To ensure parity with pages-action, display url for alias if there is no alias - setOutput("alias", pagesArtifactFields.alias); + setOutput("deployment-url", pagesArtifactFields.url); + setOutput("deployment-alias-url", pagesArtifactFields.alias); setOutput("environment", pagesArtifactFields.environment); } else { info( - "No fields available for output. Have you updated wrangler to version >=3.81.0?", + "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?", ); + // fallback to trying to extract the deployment-url and deployment-alias-url from stdout + const { deploymentUrl, aliasUrl } = + extractDeploymentUrlsFromStdout(stdOut); + + setOutput("deployment-url", deploymentUrl); + setOutput("deployment-alias-url", aliasUrl); } } }