Skip to content

Commit b894f7d

Browse files
committed
fix(action): handle errors and outputs in the action js shim
1 parent 3c0586c commit b894f7d

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

action-template/action.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ function main() {
2424

2525
const cmd = `${__dirname}/${binary}`
2626

27+
try {
28+
fs.chmodSync(cmd, 0o755);
29+
} catch (err) {
30+
console.error(`Error making binary executable: ${err.message}`);
31+
}
32+
2733
const env = { ...process.env };
2834
env.HEADLESS_TOKEN = process.env.INPUT_TOKEN;
2935

3036
const command = process.env.INPUT_COMMAND;
3137

3238
if (!["commit", "push"].includes(command)) {
33-
console.error(`Unknown command ${command}. Must be one of "commit" or "push".`);
39+
console.error(`Unknown command '${command}'. Must be one of "commit" or "push".`);
3440
process.exit(1);
3541
}
3642

@@ -66,6 +72,7 @@ function main() {
6672

6773
const child = childProcess.spawnSync(cmd, args, {
6874
env: env,
75+
// ignore stdin, capture stdout, stream stderr to the parent
6976
stdio: ['ignore', 'pipe', 'inherit'],
7077
})
7178

@@ -77,13 +84,33 @@ function main() {
7784

7885
const delim = `delim_${crypto.randomUUID()}`;
7986
fs.appendFileSync(process.env.GITHUB_OUTPUT, `pushed_ref<<${delim}${os.EOL}${out}${os.EOL}${delim}`, { encoding: "utf8" });
87+
process.exit(0);
8088
}
89+
} else {
90+
console.error(`Child process exited uncleanly with signal ${child.signal || "unknown" }`);
91+
if(child.error) {
92+
console.error(` error: ${child.error}`);
93+
}
94+
exitCode = 128;
95+
}
8196

82-
process.exit(exitCode)
97+
if(child.stdout) {
98+
// commit-headless should never print anything to stdout *except* the pushed reference, but just
99+
// in case we'll print whatever happens here
100+
console.log("Child process output:");
101+
console.log(child.stdout.toString().trim());
102+
console.log();
83103
}
84-
process.exit(1)
104+
105+
process.exit(exitCode);
106+
85107
}
86108

87109
if (require.main === module) {
88-
main()
110+
try {
111+
main()
112+
} catch (exc) {
113+
console.error(`Unhandled exception running action, got: ${exc.message}`);
114+
process.exit(1);
115+
}
89116
}

0 commit comments

Comments
 (0)