Skip to content

Commit

Permalink
Merge pull request OpenAstronomy#7 from pllim/output-no-fail
Browse files Browse the repository at this point in the history
ENH: New NO_FAIL option
  • Loading branch information
pllim authored Jan 7, 2021
2 parents bd536bd + 31bad30 commit 28102f6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 23 deletions.
64 changes: 53 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,38 @@ these directives (case-insensitive):
If it does, the job running this action will fail, thus preventing
downstream jobs or steps from running. Otherwise, jobs will run as usual.

The directives above are the defaults, but they could be customized
using `SKIP_DIRECTIVES` (see example below).

Non-pull request event will not be affected. This is because we want the CI
to run when a PR is merged even though its last commit has a directive to
skip CI for that PR.

Here is a simple example to use this action in your workflow:
*Note: If GitHub Actions ever supports this feature natively for pull requests,
then we do not need this action.*

#### Ways to customize

The behavior described above is the default, but it could be customized
using these options (also see examples below):

* `SKIP_DIRECTIVES` (comma-separated strings) to define your own
directives to skip a job or workflow. This will overwrite the
default directives.
* `NO_FAIL` (boolean, set this to `true`) to prevent this action from failing.
Instead, it would set an output value for `run_next` to `true` or `false`
to be used by downstream jobs.

#### Examples

Here are some simple examples to use this action in your workflows.

This fails `check_skip_ci`, thus preventing `tests` from running.
It is the simplest way to use this action:

```
name: CI
on:
push:
pull_request_target:
pull_request:
jobs:
# This action should be a job before you run your tests.
Expand All @@ -40,26 +57,51 @@ jobs:
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Example to use custom directives.
check_skip_another:
name: Skip something else
# This is placeholder for your real tests.
tests:
name: Run tests
needs: check_skip_ci
...
```

This makes `check_skip_ci` sets an output instead of failing.
It is more elegant (no red "x" in your check status) but
requires knowledge on how to pass output to downstream jobs.
This example also illustrates how to set custom directives,
though they are not required if you are happy with the
default directives:

```
name: CI
on:
push:
pull_request:
jobs:
# This action should be a job before you run your tests.
check_skip_ci:
name: Skip CI
runs-on: ubuntu-latest
outputs:
run_next: ${{ steps.skip_ci_step.outputs.run_next }}
steps:
- name: Some other thing depends on this check (not shown)
- name: Set output to skip CI
uses: pllim/action-skip-ci@main
id: skip_ci_step
with:
NO_FAIL: true
SKIP_DIRECTIVES: '[skip other],[other skip]'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# This is placeholder for your real tests.
tests:
name: Run tests
needs: check_skip_ci
if: needs.check_skip_ci.outputs.run_next == 'true'
...
```

*Note: If GitHub Actions ever supports this feature natively for pull requests, then we do not need this action.*

#### Why does this action not cancel workflow instead of failing?

This is because cancelling the workflow does not work when the command
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: 'Check skip CI'
description: 'Fails job if commit message wants to skip CI.'
description: 'Fails job or set output if commit message wants to skip CI.'
author: 'pllim'
inputs:
SKIP_DIRECTIVES:
description: 'Comma separated terms to skip CI'
default: '[skip ci],[ci skip],[skip action],[action skip],[skip actions],[actions skip]'
required: false
NO_FAIL:
description: 'Set output instead of failing'
default: false
required: false
GITHUB_TOKEN:
description: 'GitHub access token'
required: true
Expand Down
30 changes: 25 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ const github = __importStar(__webpack_require__(438));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
const accepted_flags = accepted_flags_input.split(",");
const pr = github.context.payload.pull_request;
if (!pr) {
core.info("This action only runs for pull request, exiting with no-op");
return;
}
/* Input always parsed as string, so need to convert to bool.
See https://github.com/actions/toolkit/issues/361
*/
const no_fail_input = core.getInput("NO_FAIL", { required: false });
const no_fail = no_fail_input === "true";
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
const accepted_flags = accepted_flags_input.split(",");
const gh_token = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = github.getOctokit(gh_token);
const commit = yield octokit.git.getCommit({
Expand All @@ -61,8 +66,16 @@ function run() {
core.info(` ${accepted_flags[i]}`);
}
if (accepted_flags.some(v => msg.includes(v))) {
core.setFailed(`"${commit.data.message}" contains directive to skip, failing this check`);
/* Instead of failing, can also try to cancel but the token needs write access,
core.info(`"${commit.data.message}" contains directive to skip, so...`);
if (no_fail) {
core.info('setting run_next to false');
core.setOutput('run_next', false);
}
else {
core.setFailed('failing this check');
}
/* Instead of failing or setting output, can also try to cancel but
the token needs write access,
so we cannot implement this for OSS in reality. */
/*
const { GITHUB_RUN_ID } = process.env;
Expand All @@ -76,7 +89,14 @@ function run() {
*/
}
else {
core.info(`No directive to skip found in "${commit.data.message}", moving on...`);
core.info(`No directive to skip found in "${commit.data.message}", so...`);
if (no_fail) {
core.info('setting run_next to true');
core.setOutput('run_next', true);
}
else {
core.info(`moving on...`);
}
}
}
catch (err) {
Expand Down
31 changes: 25 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import * as github from "@actions/github";

async function run() {
try {
const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
const accepted_flags = accepted_flags_input.split(",");

const pr = github.context.payload.pull_request;
if (!pr) {
core.info("This action only runs for pull request, exiting with no-op");
return;
}

/* Input always parsed as string, so need to convert to bool.
See https://github.com/actions/toolkit/issues/361
*/
const no_fail_input = core.getInput("NO_FAIL", { required: false });
const no_fail = no_fail_input === "true";

const accepted_flags_input = core.getInput("SKIP_DIRECTIVES", { required: false });
const accepted_flags = accepted_flags_input.split(",");

const gh_token = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = github.getOctokit(gh_token);

Expand All @@ -27,9 +33,16 @@ async function run() {
}

if (accepted_flags.some(v => msg.includes(v))) {
core.setFailed(`"${commit.data.message}" contains directive to skip, failing this check`);
core.info(`"${commit.data.message}" contains directive to skip, so...`)
if (no_fail) {
core.info('setting run_next to false');
core.setOutput('run_next', false);
} else {
core.setFailed('failing this check');
}

/* Instead of failing, can also try to cancel but the token needs write access,
/* Instead of failing or setting output, can also try to cancel but
the token needs write access,
so we cannot implement this for OSS in reality. */
/*
const { GITHUB_RUN_ID } = process.env;
Expand All @@ -42,7 +55,13 @@ async function run() {
});
*/
} else {
core.info(`No directive to skip found in "${commit.data.message}", moving on...`);
core.info(`No directive to skip found in "${commit.data.message}", so...`);
if (no_fail) {
core.info('setting run_next to true');
core.setOutput('run_next', true);
} else {
core.info(`moving on...`);
}
}
} catch(err) {
core.setFailed(`Action failed with error ${err}`);
Expand Down

0 comments on commit 28102f6

Please sign in to comment.