-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow skip cancel if named job in progress #107
Open
mrbrannan
wants to merge
3
commits into
styfle:main
Choose a base branch
from
mrbrannan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,27 @@ jobs: | |
|
||
_Note_ : This is typical when global access is set to be restrictive. Only this job will elevate those permissions. | ||
|
||
### Advanced: Disqualifying Jobs | ||
|
||
In the case where you may want for an `in_progress` job to stop the cancellation of a workflow you may pass a JSON Array as input to `disqualifying_jobs`. If a job is named in the array and its `status` is `in_progress` the workflow will be removed from the list of jobs to cancel and skipped. | ||
|
||
This is useful for operations such as static site deployment where two jobs have the ability to read/write files simultaneously which could cause downtime or runtime errors. | ||
|
||
```yml | ||
name: Cancel | ||
on: [push] | ||
jobs: | ||
cancel: | ||
name: 'Cancel Previous Runs' | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 3 | ||
steps: | ||
- uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ github.token }} | ||
disqualifying_jobs: '["deploy"]' | ||
``` | ||
|
||
## Contributing | ||
|
||
- Clone this repo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,16 @@ async function main() { | |
console.log({ eventName, sha, headSha, branch, owner, repo, GITHUB_RUN_ID }); | ||
const token = core.getInput('access_token', { required: true }); | ||
const workflow_id = core.getInput('workflow_id', { required: false }); | ||
const disqualifying_jobs_input = core.getInput('disqualifying_jobs', { required: false }); | ||
const disqualifying_jobs = disqualifying_jobs_input ? JSON.parse(disqualifying_jobs_input) : null; | ||
const ignore_sha = core.getBooleanInput('ignore_sha', { required: false }); | ||
const all_but_latest = core.getBooleanInput('all_but_latest', { required: false }); | ||
console.log(`Found token: ${token ? 'yes' : 'no'}`); | ||
console.log( | ||
disqualifying_jobs | ||
? `Skipping cancel if job in ${disqualifying_jobs}` | ||
: 'No disqualifying jobs', | ||
); | ||
const workflow_ids: string[] = []; | ||
const octokit = github.getOctokit(token); | ||
|
||
|
@@ -77,7 +84,45 @@ async function main() { | |
.reduce((a, b) => Math.max(a, b), cancelBefore.getTime()); | ||
cancelBefore = new Date(n); | ||
} | ||
const runningWorkflows = workflow_runs.filter( | ||
|
||
if (disqualifying_jobs && !Array.isArray(disqualifying_jobs)) { | ||
core.setFailed('Disqualifying jobs found but is not array'); | ||
} | ||
|
||
const workflow_jobs = ( | ||
disqualifying_jobs && disqualifying_jobs.length > 0 | ||
? await Promise.all( | ||
workflow_runs.map(async ({ id, jobs_url }) => { | ||
const { | ||
data: { jobs }, | ||
} = await octokit.request(`GET ${jobs_url}`, { | ||
owner, | ||
repo, | ||
run_id: id, | ||
}); | ||
return { | ||
workflow_run_id: id, | ||
jobs: jobs.filter( | ||
({ status, name }: any) => | ||
status === 'in_progress' && disqualifying_jobs.includes(name), | ||
), | ||
}; | ||
}), | ||
) | ||
: [] | ||
).filter(workflow => workflow.jobs.length > 0); | ||
|
||
let workflow_runs_to_cancel = [...workflow_runs]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this cloned? |
||
|
||
if (workflow_jobs.length) { | ||
console.log('Found disqualifying jobs running, skipping cancel', workflow_jobs); | ||
const workflows_to_skip = workflow_jobs.map(({ workflow_run_id }) => workflow_run_id); | ||
workflow_runs_to_cancel = workflow_runs.filter( | ||
({ id }: any) => !workflows_to_skip.includes(id), | ||
); | ||
} | ||
|
||
const runningWorkflows = workflow_runs_to_cancel.filter( | ||
run => | ||
run.head_repository.id === trigger_repo_id && | ||
run.id !== current_run.id && | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use Job ID instead of Job Name? Or at least make both work like we do for workflow name/id?