From 29516d3692c23a84757d2219ee18bd888be80fd8 Mon Sep 17 00:00:00 2001 From: Maximo Guk <62088388+Maximo-Guk@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:02:31 -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 ++ README.md | 8 +-- action.yml | 8 ++- package-lock.json | 4 +- src/index.ts | 79 +++++++++++++++++++---------- src/wranglerArtifactManager.test.ts | 4 -- 6 files changed, 69 insertions(+), 39 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..cc6d436 --- /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. Thanks @courtney-sims! - Support id, environment, url, and alias outputs for Pages deploys when wrangler version is >=3.81.0. deployment-alias-url was also deprecated in favour of pages-deployment-alias. diff --git a/README.md b/README.md index 094c68e..97ef9ec 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,7 @@ Now when you run your workflow, you will see the full output of the Wrangler com > Note: the `command-stderr` output variable is also available if you need to parse the standard error output of the Wrangler command. -### Using the `deployment-url` and `deployment-alias-url` Output Variables +### Using the `deployment-url` and `pages-deployment-alias-url` Output Variables If you are executing a Wrangler command that results in either a Workers or Pages deployment, you can utilize the `deployment-url` output variable to get the URL of the deployment. For example, if you want to print the deployment URL after deploying your application, you can do the following: @@ -287,14 +287,14 @@ The resulting output will look something like this: https://.pages.dev ``` -Pages deployments will also provide their alias URL (since Wrangler v3.78.0). You can use the `deployment-alias-url` output variable to get the URL of the deployment alias. This is useful for, for example, branch aliases for preview deployments. +Pages deployments will also provide their alias URL (since Wrangler v3.78.0). You can use the `pages-deployment-alias-url` output variable to get the URL of the deployment alias. This is useful for, for example, branch aliases for preview deployments. If the sample action above was used to deploy a branch other than main, you could use the following to get the branch URL: ```yaml -- name: print deployment-alias-url +- name: print pages-deployment-alias-url env: - DEPLOYMENT_ALIAS_URL: ${{ steps.deploy.outputs.deployment-alias-url }} + DEPLOYMENT_ALIAS_URL: ${{ steps.deploy.outputs.pages-deployment-alias-url }} run: echo $DEPLOYMENT_ALIAS_URL ``` diff --git a/action.yml b/action.yml index 6094973..e5cc0ff 100644 --- a/action.yml +++ b/action.yml @@ -51,5 +51,9 @@ outputs: description: "The error output of the Wrangler command (comes from stderr)" deployment-url: description: "If the command was a Workers or Pages deployment, this will be the URL of the deployment" - deployment-alias-url: - description: "If the command was a Workers or Pages deployment, this can be the URL of the deployment alias (if it exists) - needs wrangler >= 3.78.0" + pages-deployment-alias-url: + description: "If the command was a Pages deployment, this will be the URL of the deployment alias (if it exists) - needs wrangler >= 3.78.0" + pages-deployment-id: + description: "If the command was a Pages deployment, this will be the ID of the deployment needs wrangler >= 3.81.0" + pages-deployment-environment: + description: "If the command was a Pages deployment, this will be the environment of the deployment needs wrangler >= 3.81.0" 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..92c6d98 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,29 @@ async function uploadSecrets() { } } +// fallback to trying to extract the deployment-url and pages-deployment-alias-url from stdout for wranglerVersion < 3.81.0 +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 +339,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 +364,41 @@ 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); + // DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change + setOutput("deployment-alias-url", aliasUrl); + setOutput("pages-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("environment", pagesArtifactFields.environment); + setOutput("deployment-url", pagesArtifactFields.url); + // DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change + setOutput("deployment-alias-url", pagesArtifactFields.alias); + setOutput("pages-deployment-alias-url", pagesArtifactFields.alias); + setOutput("pages-deployment-id", pagesArtifactFields.deployment_id); + setOutput("pages-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?", ); + // DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change + const { deploymentUrl, aliasUrl } = + extractDeploymentUrlsFromStdout(stdOut); + + setOutput("deployment-url", deploymentUrl); + // DEPRECATED: deployment-alias-url in favour of pages-deployment-alias, drop in next wrangler-action major version change + setOutput("deployment-alias-url", aliasUrl); + setOutput("pages-deployment-alias-url", aliasUrl); } } } diff --git a/src/wranglerArtifactManager.test.ts b/src/wranglerArtifactManager.test.ts index 1ba390a..00a8840 100644 --- a/src/wranglerArtifactManager.test.ts +++ b/src/wranglerArtifactManager.test.ts @@ -25,7 +25,6 @@ describe("wranglerArtifactsManager", () => { expect(artifacts).toEqual([ "./testOutputDir/wrangler-output-2024-10-17_18-48-40_463-2e6e83.json", ]); - //mock.restore(); }); it("Returns an empty list when the output directory doesn't exist", async () => { mock({ @@ -34,7 +33,6 @@ describe("wranglerArtifactsManager", () => { const artifacts = await getWranglerArtifacts("./testOutputDir"); expect(artifacts).toEqual([]); - //mock.restore(); }); }); @@ -60,7 +58,6 @@ describe("wranglerArtifactsManager", () => { deployment_id: "123", alias: "test.com", }); - //mock.restore(); }), it("Skips artifact entries that are not parseable", async () => { mock({ @@ -83,7 +80,6 @@ describe("wranglerArtifactsManager", () => { deployment_id: "123", alias: "test.com", }); - //mock.restore(); }); }); });