Skip to content

Commit

Permalink
Moves wrangler output to tmpdir rather than /opt/ since /opt/ is owne…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
Maximo-Guk committed Oct 31, 2024
1 parent 44d79ed commit b4191bf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-wasps-greet.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 43 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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, {
Expand Down Expand Up @@ -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 [email protected])
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 {
Expand Down Expand Up @@ -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"],
Expand All @@ -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 [email protected])
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);
}
}
}
Expand Down

0 comments on commit b4191bf

Please sign in to comment.