Skip to content

Commit 238d66a

Browse files
authored
Merge pull request #2 from jannekem/check_return_code
Add error handling
2 parents e69b8d1 + 377f84c commit 238d66a

File tree

6 files changed

+125
-5
lines changed

6 files changed

+125
-5
lines changed

.github/workflows/check-build.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Check build
2+
3+
on: push
4+
5+
jobs:
6+
check-build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v2
11+
with:
12+
node-version: "14"
13+
- run: npm install
14+
- run: npm run build
15+
- run: git diff --exit-code

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,45 @@ jobs:
2929
for f in os.listdir():
3030
print(f)
3131
```
32+
33+
## Handling errors
34+
35+
By default, the action will fail if it encounters any errors when trying to run your script. You can override this with the `fail-on-error` input.
36+
37+
The action sets three outputs:
38+
39+
- `stdout` contains any text that your program prints to the console
40+
- `stderr` contains any text that is routed to STDERR, such as exception messages
41+
- `error` is a string with either `"true"` or `"false"` depending on if errors were present or not, use this to check for errors when you opt not to fail the step
42+
43+
Look at the following snippet to see how the error handling works in practice:
44+
45+
```yaml
46+
name: Run Script
47+
48+
on:
49+
push:
50+
51+
jobs:
52+
build:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v2
56+
- uses: actions/setup-python@v2
57+
- uses: jannekem/run-python-script-action@v1
58+
id: script
59+
with:
60+
fail-on-error: false
61+
script: |
62+
print("Doing something that will fail")
63+
a = []
64+
a[10]
65+
- name: Print errors
66+
if: steps.script.outputs.error == 'true'
67+
run: |
68+
printenv "SCRIPT_STDOUT"
69+
printenv "SCRIPT_STDERR"
70+
env:
71+
SCRIPT_STDOUT: ${{ steps.script.outputs.stdout }}
72+
SCRIPT_STDERR: ${{ steps.script.outputs.stderr }}
73+
```

action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ inputs:
44
script:
55
description: "Python script"
66
required: true
7+
fail-on-error:
8+
description: "Fail step if the return code from running the script is non-zero"
9+
required: false
10+
default: true
11+
outputs:
12+
stdout:
13+
description: "Program stdout"
14+
stderr:
15+
description: "Program stderr"
16+
error:
17+
description: "A string of 'true' or 'false' that tells if there were errors, use in conjunction with the fail-on-error input"
718
runs:
819
using: "node12"
920
main: "dist/index.js"

dist/index.js

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@ const fs = require('fs');
44
const tmp = require('tmp');
55

66
async function run() {
7+
const script = core.getInput("script");
8+
const failOnError = core.getInput("fail-on-error") === "true";
9+
10+
let stdout = "";
11+
let stderr = "";
12+
let errorStatus = "false";
13+
const options = {};
14+
options.listeners = {
15+
stdout: (data) => {
16+
stdout += data.toString();
17+
},
18+
stderr: (data) => {
19+
stderr += data.toString();
20+
}
21+
};
22+
723
tmp.setGracefulCleanup();
8-
const script = core.getInput('script');
924
const filename = tmp.tmpNameSync({postfix: '.py'});
1025
fs.writeFileSync(filename, script);
11-
exec.exec('python', [filename])
26+
try {
27+
await exec.exec('python', [filename], options);
28+
} catch (error) {
29+
errorStatus = "true";
30+
if (failOnError) {
31+
core.setFailed(error);
32+
}
33+
} finally {
34+
core.setOutput("stdout", stdout);
35+
core.setOutput("stderr", stderr);
36+
core.setOutput("error", errorStatus);
37+
}
1238
}
1339

1440
run();

0 commit comments

Comments
 (0)