mirror of
https://github.com/cloudflare/wrangler-action.git
synced 2024-11-21 17:43:23 +01:00
Merge pull request #213 from GrantBirki/write-results-to-a-file
Save Wrangler Command Output to a Variable
This commit is contained in:
commit
0e62acaf21
4 changed files with 107 additions and 2 deletions
11
.changeset/plenty-seals-cover.md
Normal file
11
.changeset/plenty-seals-cover.md
Normal 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.
|
50
README.md
50
README.md
|
@ -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!"
|
||||
|
|
|
@ -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
src/index.ts
41
src/index.ts
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue