Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Wrangler Command Output to a Variable #213

Merged
merged 16 commits into from
Dec 13, 2023
Merged
11 changes: 11 additions & 0 deletions .changeset/plenty-seals-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler-action": minor
---

This change introduces three new GitHub Actions output variables. These variables are as follows:

- `command-output` - contains the string results of `stdout`
- `command-stderr` - contains the string results of `stderr`
- `deployment-url` - contains the string results of the URL that was deployed (ex: `https://<your_pages_site>.pages.dev`)

These output variables are intended to be used by more advanced workflows that require the output results or deployment url from Wrangler commands in subsequent workflow steps.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,56 @@ jobs:

For more advanced usage or to programmatically trigger the workflow from scripts, refer to [the GitHub documentation](https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event) for making API calls.

## Advanced Usage

### Using Wrangler Command Output in Subsequent Steps

More advanced workflows may need to parse the resulting output of Wrangler commands. To do this, you can use the `command-output` output variable in subsequent steps. For example, if you want to print the output of the Wrangler command, you can do the following:

```yaml
- name: Deploy
id: deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy --project-name=example

- name: print wrangler command output
env:
CMD_OUTPUT: ${{ steps.deploy.outputs.command-output }}
run: echo $CMD_OUTPUT
```

Now when you run your workflow, you will see the full output of the Wrangler command in your workflow logs. You can also use this output in subsequent workflow steps to parse the output for specific values.

> 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` Output Variable

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:

```yaml
- name: Deploy
id: deploy
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy --project-name=example

- name: print deployment-url
env:
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
run: echo $DEPLOYMENT_URL
```

The resulting output will look something like this:

```text
https://<your_pages_site>.pages.dev
```

## Troubleshooting

### "I just started using Workers/Wrangler and I don't know what this is!"
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ inputs:
packageManager:
description: "The package manager you'd like to use to install and run wrangler. If not specified, the preferred package manager will be inferred based on the presence of a lockfile or fallback to using npm if no lockfile is found. Valid values are `npm` | `pnpm` | `yarn` | `bun`."
required: false
outputs:
command-output:
description: "The output of the Wrangler command (comes from stdout)"
command-stderr:
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"
41 changes: 39 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
info as originalInfo,
startGroup as originalStartGroup,
setFailed,
setOutput,
} from "@actions/core";
import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils";
Expand Down Expand Up @@ -242,10 +243,46 @@ async function wranglerCommands() {
}
}

await exec(`${packageManager.exec} wrangler ${command}`, args, {
// Used for saving the wrangler output
let stdOut = "";
let stdErr = "";

// Construct the options for the exec command
const options = {
cwd: config["workingDirectory"],
silent: config["QUIET_MODE"],
});
listeners: {
stdout: (data: Buffer) => {
stdOut += data.toString();
},
stderr: (data: Buffer) => {
stdErr += data.toString();
},
},
};

// Execute the wrangler command
await exec(`${packageManager.exec} wrangler ${command}`, args, options);

// Set the outputs for the command
setOutput("command-output", stdOut);
setOutput("command-stderr", stdErr);
1000hz marked this conversation as resolved.
Show resolved Hide resolved

// Check if this command is a workers or pages deployment
if (
command.startsWith("deploy") ||
command.startsWith("publish") ||
command.startsWith("pages publish") ||
command.startsWith("pages deploy")
) {
// If this is a workers or pages deployment, 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);
}
}
}
} finally {
endGroup();
Expand Down
Loading