Merge pull request #213 from GrantBirki/write-results-to-a-file

Save Wrangler Command Output to a Variable
This commit is contained in:
Cina Saffary 2023-12-13 11:17:24 -06:00 committed by GitHub
commit 0e62acaf21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 2 deletions

View file

@ -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.

View file

@ -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!"

View file

@ -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"

View file

@ -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";
@ -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);
// 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();