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 ## 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 ```yaml
- name: Deploy - name: Deploy
@ -232,12 +232,11 @@ More advanced workflows may need to parse the resulting output of Wrangler comma
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy --project-name=example command: pages deploy --project-name=example
outputFile: "true"
- name: print wrangler command output - name: print wrangler command output
env: env:
OUTPUT_PATH: ${{ steps.deploy.outputs.wranglerCommandOutputFile }} CMD_OUTPUT: ${{ steps.deploy.outputs.command-output }}
run: cat $OUTPUT_PATH 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: 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: 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`." 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 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: outputs:
commandOutput: command-output:
description: "The output of the Wrangler command" description: "The output of the Wrangler command"

View file

@ -9,8 +9,6 @@ import {
setFailed, setFailed,
setOutput, setOutput,
} from "@actions/core"; } from "@actions/core";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { exec, execShell } from "./exec"; import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils"; import { checkWorkingDirectory, semverCompare } from "./utils";
import { getPackageManager } from "./packageManagers"; import { getPackageManager } from "./packageManagers";
@ -31,7 +29,6 @@ const config = {
COMMANDS: getMultilineInput("command"), COMMANDS: getMultilineInput("command"),
QUIET_MODE: getBooleanInput("quiet"), QUIET_MODE: getBooleanInput("quiet"),
PACKAGE_MANAGER: getInput("packageManager"), PACKAGE_MANAGER: getInput("packageManager"),
OUTPUT_TO_FILE: getBooleanInput("outputToFile"),
} as const; } as const;
const packageManager = getPackageManager(config.PACKAGE_MANAGER, { 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 stdOut = "";
let stdErr = ""; let stdErr = "";
@ -267,27 +264,13 @@ async function wranglerCommands() {
// Execute the wrangler command // Execute the wrangler command
await exec(`${packageManager.exec} wrangler ${command}`, args, options); 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 stdOut contains data but stdErr does not, then save the stdOut value
if (config["OUTPUT_TO_FILE"]) { if (stdOut && (!stdErr || stdErr === "" || stdErr === undefined)) {
// Create the output data in a machine readable format info('saving stdout to "command-output" output')
const outputData = { setOutput("command-output", stdOut);
stdOut: stdOut, } else if (stdErr) {
stdErr: stdErr, info('saving stderr to "command-output" output')
}; setOutput("command-output", 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);
} }
} }
} finally { } finally {