Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

Commit 28102f6

Browse files
authored
Merge pull request #7 from pllim/output-no-fail
ENH: New NO_FAIL option
2 parents bd536bd + 31bad30 commit 28102f6

File tree

4 files changed

+108
-23
lines changed

4 files changed

+108
-23
lines changed

README.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,38 @@ these directives (case-insensitive):
1313
If it does, the job running this action will fail, thus preventing
1414
downstream jobs or steps from running. Otherwise, jobs will run as usual.
1515

16-
The directives above are the defaults, but they could be customized
17-
using `SKIP_DIRECTIVES` (see example below).
18-
1916
Non-pull request event will not be affected. This is because we want the CI
2017
to run when a PR is merged even though its last commit has a directive to
2118
skip CI for that PR.
2219

23-
Here is a simple example to use this action in your workflow:
20+
*Note: If GitHub Actions ever supports this feature natively for pull requests,
21+
then we do not need this action.*
22+
23+
#### Ways to customize
24+
25+
The behavior described above is the default, but it could be customized
26+
using these options (also see examples below):
27+
28+
* `SKIP_DIRECTIVES` (comma-separated strings) to define your own
29+
directives to skip a job or workflow. This will overwrite the
30+
default directives.
31+
* `NO_FAIL` (boolean, set this to `true`) to prevent this action from failing.
32+
Instead, it would set an output value for `run_next` to `true` or `false`
33+
to be used by downstream jobs.
34+
35+
#### Examples
36+
37+
Here are some simple examples to use this action in your workflows.
38+
39+
This fails `check_skip_ci`, thus preventing `tests` from running.
40+
It is the simplest way to use this action:
2441

2542
```
2643
name: CI
2744
2845
on:
2946
push:
30-
pull_request_target:
47+
pull_request:
3148
3249
jobs:
3350
# This action should be a job before you run your tests.
@@ -40,26 +57,51 @@ jobs:
4057
with:
4158
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4259
43-
# Example to use custom directives.
44-
check_skip_another:
45-
name: Skip something else
60+
# This is placeholder for your real tests.
61+
tests:
62+
name: Run tests
63+
needs: check_skip_ci
64+
...
65+
```
66+
67+
This makes `check_skip_ci` sets an output instead of failing.
68+
It is more elegant (no red "x" in your check status) but
69+
requires knowledge on how to pass output to downstream jobs.
70+
This example also illustrates how to set custom directives,
71+
though they are not required if you are happy with the
72+
default directives:
73+
74+
```
75+
name: CI
76+
77+
on:
78+
push:
79+
pull_request:
80+
81+
jobs:
82+
# This action should be a job before you run your tests.
83+
check_skip_ci:
84+
name: Skip CI
4685
runs-on: ubuntu-latest
86+
outputs:
87+
run_next: ${{ steps.skip_ci_step.outputs.run_next }}
4788
steps:
48-
- name: Some other thing depends on this check (not shown)
89+
- name: Set output to skip CI
4990
uses: pllim/action-skip-ci@main
91+
id: skip_ci_step
5092
with:
93+
NO_FAIL: true
5194
SKIP_DIRECTIVES: '[skip other],[other skip]'
5295
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5396
5497
# This is placeholder for your real tests.
5598
tests:
5699
name: Run tests
57100
needs: check_skip_ci
101+
if: needs.check_skip_ci.outputs.run_next == 'true'
58102
...
59103
```
60104

61-
*Note: If GitHub Actions ever supports this feature natively for pull requests, then we do not need this action.*
62-
63105
#### Why does this action not cancel workflow instead of failing?
64106

65107
This is because cancelling the workflow does not work when the command

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
name: 'Check skip CI'
2-
description: 'Fails job if commit message wants to skip CI.'
2+
description: 'Fails job or set output if commit message wants to skip CI.'
33
author: 'pllim'
44
inputs:
55
SKIP_DIRECTIVES:
66
description: 'Comma separated terms to skip CI'
77
default: '[skip ci],[ci skip],[skip action],[action skip],[skip actions],[actions skip]'
88
required: false
9+
NO_FAIL:
10+
description: 'Set output instead of failing'
11+
default: false
12+
required: false
913
GITHUB_TOKEN:
1014
description: 'GitHub access token'
1115
required: true

dist/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ const github = __importStar(__webpack_require__(438));
4141
function run() {
4242
return __awaiter(this, void 0, void 0, function* () {
4343
try {
44-
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
45-
const accepted_flags = accepted_flags_input.split(",");
4644
const pr = github.context.payload.pull_request;
4745
if (!pr) {
4846
core.info("This action only runs for pull request, exiting with no-op");
4947
return;
5048
}
49+
/* Input always parsed as string, so need to convert to bool.
50+
See https://github.com/actions/toolkit/issues/361
51+
*/
52+
const no_fail_input = core.getInput("NO_FAIL", { required: false });
53+
const no_fail = no_fail_input === "true";
54+
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
55+
const accepted_flags = accepted_flags_input.split(",");
5156
const gh_token = core.getInput("GITHUB_TOKEN", { required: true });
5257
const octokit = github.getOctokit(gh_token);
5358
const commit = yield octokit.git.getCommit({
@@ -61,8 +66,16 @@ function run() {
6166
core.info(` ${accepted_flags[i]}`);
6267
}
6368
if (accepted_flags.some(v => msg.includes(v))) {
64-
core.setFailed(`"${commit.data.message}" contains directive to skip, failing this check`);
65-
/* Instead of failing, can also try to cancel but the token needs write access,
69+
core.info(`"${commit.data.message}" contains directive to skip, so...`);
70+
if (no_fail) {
71+
core.info('setting run_next to false');
72+
core.setOutput('run_next', false);
73+
}
74+
else {
75+
core.setFailed('failing this check');
76+
}
77+
/* Instead of failing or setting output, can also try to cancel but
78+
the token needs write access,
6679
so we cannot implement this for OSS in reality. */
6780
/*
6881
const { GITHUB_RUN_ID } = process.env;
@@ -76,7 +89,14 @@ function run() {
7689
*/
7790
}
7891
else {
79-
core.info(`No directive to skip found in "${commit.data.message}", moving on...`);
92+
core.info(`No directive to skip found in "${commit.data.message}", so...`);
93+
if (no_fail) {
94+
core.info('setting run_next to true');
95+
core.setOutput('run_next', true);
96+
}
97+
else {
98+
core.info(`moving on...`);
99+
}
80100
}
81101
}
82102
catch (err) {

src/main.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ import * as github from "@actions/github";
33

44
async function run() {
55
try {
6-
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
7-
const accepted_flags = accepted_flags_input.split(",");
8-
96
const pr = github.context.payload.pull_request;
107
if (!pr) {
118
core.info("This action only runs for pull request, exiting with no-op");
129
return;
1310
}
1411

12+
/* Input always parsed as string, so need to convert to bool.
13+
See https://github.com/actions/toolkit/issues/361
14+
*/
15+
const no_fail_input = core.getInput("NO_FAIL", { required: false });
16+
const no_fail = no_fail_input === "true";
17+
18+
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
19+
const accepted_flags = accepted_flags_input.split(",");
20+
1521
const gh_token = core.getInput("GITHUB_TOKEN", { required: true });
1622
const octokit = github.getOctokit(gh_token);
1723

@@ -27,9 +33,16 @@ async function run() {
2733
}
2834

2935
if (accepted_flags.some(v => msg.includes(v))) {
30-
core.setFailed(`"${commit.data.message}" contains directive to skip, failing this check`);
36+
core.info(`"${commit.data.message}" contains directive to skip, so...`)
37+
if (no_fail) {
38+
core.info('setting run_next to false');
39+
core.setOutput('run_next', false);
40+
} else {
41+
core.setFailed('failing this check');
42+
}
3143

32-
/* Instead of failing, can also try to cancel but the token needs write access,
44+
/* Instead of failing or setting output, can also try to cancel but
45+
the token needs write access,
3346
so we cannot implement this for OSS in reality. */
3447
/*
3548
const { GITHUB_RUN_ID } = process.env;
@@ -42,7 +55,13 @@ async function run() {
4255
});
4356
*/
4457
} else {
45-
core.info(`No directive to skip found in "${commit.data.message}", moving on...`);
58+
core.info(`No directive to skip found in "${commit.data.message}", so...`);
59+
if (no_fail) {
60+
core.info('setting run_next to true');
61+
core.setOutput('run_next', true);
62+
} else {
63+
core.info(`moving on...`);
64+
}
4665
}
4766
} catch(err) {
4867
core.setFailed(`Action failed with error ${err}`);

0 commit comments

Comments
 (0)