swap from file to output variable

This commit is contained in:
GrantBirki 2023-12-07 22:26:06 -07:00
parent 0558fc8c04
commit 74a612d924
No known key found for this signature in database
GPG key ID: 96DF969ECBD266FE
3 changed files with 13 additions and 35 deletions

View file

@ -220,9 +220,9 @@ For more advanced usage or to programmatically trigger the workflow from scripts
## Advanced Usage
### Saving Wrangler Command Output to a File
### 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 `outputFile` option to save the output of the command to a file. This file will be available to subsequent steps in the workflow. The format of this file is JSON so that it can be easily parsed by other 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
@ -232,12 +232,11 @@ More advanced workflows may need to parse the resulting output of Wrangler comma
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy --project-name=example
outputFile: "true"
- name: print wrangler command output
env:
OUTPUT_PATH: ${{ steps.deploy.outputs.wranglerCommandOutputFile }}
run: cat $OUTPUT_PATH
CMD_OUTPUT: ${{ steps.deploy.outputs.command-output }}
run: echo $CMD_OUTPUT
```
Now when you run your workflow, you will see the output of the Wrangler command in the logs that should be in the following structure:

View file

@ -44,10 +44,6 @@ 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
outputToFile:
description: "The path to a file to write the output of the Wrangler command to. If not specified, the output will be written to the console."
required: false
default: "false"
outputs:
commandOutput:
command-output:
description: "The output of the Wrangler command"

View file

@ -9,8 +9,6 @@ import {
setFailed,
setOutput,
} from "@actions/core";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils";
import { getPackageManager } from "./packageManagers";
@ -31,7 +29,6 @@ const config = {
COMMANDS: getMultilineInput("command"),
QUIET_MODE: getBooleanInput("quiet"),
PACKAGE_MANAGER: getInput("packageManager"),
OUTPUT_TO_FILE: getBooleanInput("outputToFile"),
} as const;
const packageManager = getPackageManager(config.PACKAGE_MANAGER, {
@ -246,7 +243,7 @@ async function wranglerCommands() {
}
}
// Used for saving the wrangler output to a file
// Used for saving the wrangler output
let stdOut = "";
let stdErr = "";
@ -267,27 +264,13 @@ async function wranglerCommands() {
// Execute the wrangler command
await exec(`${packageManager.exec} wrangler ${command}`, args, options);
// If the user has specified to output the wrangler command to a file save the stdout and stderr
if (config["OUTPUT_TO_FILE"]) {
// Create the output data in a machine readable format
const outputData = {
stdOut: stdOut,
stdErr: stdErr,
};
// Consturct the file output path to use the current working directory
const outputFilePath = join(
config["workingDirectory"],
"wrangler-command-output.json",
);
// Write the output to a JSON file
writeFileSync(outputFilePath, JSON.stringify(outputData));
info(
`✅ wrangler-command-output output saved to ${outputFilePath}`,
true,
);
setOutput("wranglerCommandOutputFile", outputFilePath);
// If stdOut contains data but stdErr does not, then save the stdOut value
if (stdOut && (!stdErr || stdErr === "" || stdErr === undefined)) {
info('saving stdout to "command-output" output')
setOutput("command-output", stdOut);
} else if (stdErr) {
info('saving stderr to "command-output" output')
setOutput("command-output", stdErr);
}
}
} finally {