Skip to content

Commit

Permalink
feat: allow to ignore cancelled runs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Nassri committed Feb 22, 2021
1 parent e334e90 commit 2ff4213
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 46 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ However by itself, this doesn't quite work as expected.

3. Your workflow will trigger as many times as you have workflow dependencies

> *in the previous example, our `deploy` workflow, will run 3 times!*
> _in the previous example, our `deploy` workflow, will run 3 times!_

All this makes the `workflow_run` event fundamentally broken for any advanced usage, this Action aims to remedy that.
All this makes the `workflow_run` event fundamentally broken for any advanced usage, this Action aims to remedy that.

> ***Note**: See this [Community discussion](https://github.community/t/workflow-run-completed-event-triggered-by-failed-workflow/128001/5) for more info on the topic*
> ***Note**: See this [Community discussion](https://github.community/t/workflow-run-completed-event-triggered-by-failed-workflow/128001/5) for more info on the topic*

</details>

Expand All @@ -79,12 +79,13 @@ jobs:

### Inputs

| input | required | default | description |
|----------------|----------|----------------|-------------------------------------------------|
| `github-token` | ❌ | `github.token` | The GitHub token used to call the GitHub API |
| `timeout` | ❌ | `30000` | timeout before we stop trying (in milliseconds) |
| `delay` | ❌ | `5000` | delay between status checks (in milliseconds) |
| `sha` | ❌ | `github.sha` | Git SHA, if it's different from `github.sha` |
| input | required | default | description |
|--------------------|----------|----------------|-------------------------------------------------|
| `github-token` | ❌ | `github.token` | The GitHub token used to call the GitHub API |
| `timeout` | ❌ | `30000` | timeout before we stop trying (in milliseconds) |
| `delay` | ❌ | `5000` | delay between status checks (in milliseconds) |
| `sha` | ❌ | `github.sha` | Git SHA, if it's different from `github.sha` |
| `ignore-cancelled` | ❌ | `false` | ignore cancelled workflow runs |

----
> Author: [Ahmad Nassri](https://www.ahmadnassri.com/) &bull;
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: delay between status checks (in milliseconds)
default: 5000

ignore-cancelled:
description: ignore cancelled workflow runs
default: false

sha:
description: SHA to use (defaults to github.sha)
default: ${{ github.sha }}
Expand Down
6 changes: 4 additions & 2 deletions action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ if (github.context.eventName !== 'workflow_run') {
// parse inputs
const inputs = {
token: core.getInput('github-token', { required: true }),
delay: Number(core.getInput('delay', { required: true })),
timeout: Number(core.getInput('timeout', { required: true })),
sha: core.getInput('sha', { required: true }),

delay: Number(core.getInput('delay', { required: false })),
timeout: Number(core.getInput('timeout', { required: false })),
ignore: core.getInput('ignore-cancelled', { required: false }) === 'true',
}

// error handler
Expand Down
7 changes: 5 additions & 2 deletions action/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import workflows from './workflows.js'
// sleep function
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

export default async function ({ token, delay, timeout, sha }) {
export default async function ({ token, delay, timeout, sha, ignore }) {
let timer = 0

// init octokit
Expand Down Expand Up @@ -45,7 +45,10 @@ export default async function ({ token, delay, timeout, sha }) {
process.exit(1)
}

while (result.find(run => run.conclusion !== 'success')) {
const successful = ['success']
if (ignore) successful.push('cancelled')

while (result.find(run => !successful.includes(run.conclusion))) {
// exit early
const failed = result.find(run => run.conclusion === 'failure')

Expand Down
72 changes: 39 additions & 33 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,46 @@

The [`workflow_run`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run) event occurs when a workflow run is requested or completed, and allows you to execute a workflow based on the finished result of another workflow.

###### example
```yaml
on:
workflow_run:
workflows: [ test ]
types:
- completed
```
###### example

```yaml
on:
workflow_run:
workflows: [ test ]
types:
- completed
```
However by itself, this doesn't quite work as expected.
1. The `completed` type, does not indicate success, for that you'd have to include the following in each job of your workflow:
```yaml
if: ${{ github.event.workflow_run.conclusion == 'success' }}
```
1. The `completed` type, does not indicate success, for that you'd have to include the following in each job of your workflow:
```yaml
if: ${{ github.event.workflow_run.conclusion == 'success' }}
```

2. If you're depending on more than one workflow, then ANY of them completing, will trigger the event

###### example

```yaml
name: deploy
on:
workflow_run:
workflows: [ test, lint, compile ]
types:
- completed
```

2. If you're depending on more than one workflow, then ANY of them completing, will trigger the event
###### example
```yaml
name: deploy
> _if your `test` workflow fails, but `lint` completed successfully, `github.event.workflow_run.conclusion == 'success'` will still be true_

on:
workflow_run:
workflows: [ test, lint, compile ]
types:
- completed
```
> _if your `test` workflow fails, but `lint` completed successfully, `github.event.workflow_run.conclusion == 'success'` will still be true_
3. Your workflow will trigger as many times as you have workflow dependencies

3. Your workflow will trigger as many times as you have workflow dependencies
> _in the previous example, our `deploy` workflow, will run 3 times!_
> _in the previous example, our `deploy` workflow, will run 3 times!_

All this makes the `workflow_run` event fundamentally broken for any advanced usage, this Action aims to remedy that.
All this makes the `workflow_run` event fundamentally broken for any advanced usage, this Action aims to remedy that.

> _**Note**: See this [Community discussion](https://github.community/t/workflow-run-completed-event-triggered-by-failed-workflow/128001/5) for more info on the topic_
> _**Note**: See this [Community discussion](https://github.community/t/workflow-run-completed-event-triggered-by-failed-workflow/128001/5) for more info on the topic_

</details>

Expand All @@ -63,9 +68,10 @@ jobs:

### Inputs

| input | required | default | description |
| -------------- | -------- | -------------- | ----------------------------------------------- |
| `github-token` | ❌ | `github.token` | The GitHub token used to call the GitHub API |
| `timeout` | ❌ | `30000` | timeout before we stop trying (in milliseconds) |
| `delay` | ❌ | `5000` | delay between status checks (in milliseconds) |
| `sha` | ❌ | `github.sha` | Git SHA, if it's different from `github.sha` |
| input | required | default | description |
| ------------------ | -------- | -------------- | ----------------------------------------------- |
| `github-token` | ❌ | `github.token` | The GitHub token used to call the GitHub API |
| `timeout` | ❌ | `30000` | timeout before we stop trying (in milliseconds) |
| `delay` | ❌ | `5000` | delay between status checks (in milliseconds) |
| `sha` | ❌ | `github.sha` | Git SHA, if it's different from `github.sha` |
| `ignore-cancelled` | ❌ | `false` | ignore cancelled workflow runs |

0 comments on commit 2ff4213

Please sign in to comment.