-
Notifications
You must be signed in to change notification settings - Fork 137
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
[CI-IMPROVEMENT-9] Implement check-required workflow that checks if all required checks are successful and not skipped #3369
Merged
pavlovic-ivan
merged 2 commits into
armadaproject:master
from
pavlovic-ivan:ci-improvements-part-9
Feb 6, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
name: Check required jobs | ||
|
||
# This workflow is triggered when a workflow run for the CI is completed. | ||
# It checks if the "All required checks done" job was actually successful | ||
# (and not just skipped) and creates a check run if that is the case. The | ||
# check run can be used to protect the main branch from being merged if the | ||
# CI is not passing. We need to use a GitHub app token to create the check | ||
# run because otherwise the check suite will be assigned to the first workflow | ||
# run for the CI, which might not be the latest one. See | ||
# https://github.com/orgs/community/discussions/24616#discussioncomment-6088422 | ||
# for more details. | ||
|
||
on: | ||
workflow_run: | ||
workflows: [CI] | ||
|
||
permissions: | ||
actions: read | ||
checks: write | ||
|
||
jobs: | ||
required-jobs: | ||
name: Check required jobs | ||
if: ${{ !github.event.repository.fork }} | ||
environment: create-check | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Generate an app token | ||
id: app-token | ||
uses: actions/create-github-app-token@v1 | ||
with: | ||
app-id: ${{ secrets.APP_ID }} | ||
private-key: ${{ secrets.APP_PRIVATE_KEY }} | ||
|
||
- uses: actions/github-script@v7 | ||
with: | ||
github-token: ${{ steps.app-token.outputs.token }} | ||
script: | | ||
const ghaAppId = 15368; | ||
const ghaName = 'All required checks done'; | ||
const myAppId = ${{ secrets.APP_ID }}; | ||
const myName = 'All required checks succeeded'; | ||
const owner = context.payload.repository.owner.login; | ||
const repo = context.payload.repository.name; | ||
const sha = context.payload.workflow_run.head_sha; | ||
|
||
core.info(`List GitHub Actions check runs for ${sha}.`) | ||
const { data: { check_runs: ghaChecks } } = await github.rest.checks.listForRef({ | ||
owner: owner, | ||
repo: repo, | ||
ref: sha, | ||
app_id: ghaAppId, | ||
check_name: ghaName, | ||
}); | ||
|
||
var newCheck = { | ||
owner: owner, | ||
repo: repo, | ||
name: myName, | ||
head_sha: sha, | ||
status: 'in_progress', | ||
started_at: context.payload.workflow_run.created_at, | ||
output: { | ||
title: 'Not all required checks succeeded', | ||
}, | ||
}; | ||
|
||
core.summary.addHeading('The following required checks have been considered:', 3); | ||
ghaChecks.forEach(check => { | ||
core.summary | ||
.addLink(check.name, check.html_url) | ||
.addCodeBlock(JSON.stringify(check, ['status', 'conclusion', 'started_at', 'completed_at'], 2), 'json'); | ||
|
||
if (check.status === 'completed' && check.conclusion === 'success') { | ||
newCheck.status = 'completed'; | ||
newCheck.conclusion = 'success'; | ||
newCheck.started_at = check.started_at; | ||
newCheck.completed_at = check.completed_at; | ||
newCheck.output.title = 'All required checks succeeded'; | ||
} else if (check.started_at > newCheck.started_at) { | ||
newCheck.started_at = check.started_at; | ||
} | ||
}); | ||
if (ghaChecks.length === 0) { | ||
core.summary.addRaw(`No check runs for ${sha} found.`); | ||
} | ||
newCheck.output.summary = core.summary.stringify(); | ||
await core.summary.write(); | ||
|
||
core.info(`Create own check run for ${sha}: ${JSON.stringify(newCheck, null, 2)}.`) | ||
const { data: { html_url } } = await github.rest.checks.create(newCheck); | ||
|
||
await core.summary | ||
.addHeading('Check run created:', 3) | ||
.addLink(myName, html_url) | ||
.addCodeBlock(JSON.stringify(newCheck, null, 2), 'json') | ||
.write(); |
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
Oops, something went wrong.
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.
Do we want to do any handling of cases where we're not completed successfully here?
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.
This
else
block here is only informational. It's not necessary, but since the code is taken from FML, maybe they benefit from this info, which then makes this contribution identical to what they use. @jgiannuzzi what do you think of this? does FML benefit from it or not? should i remove it, so both are identical (making it easier to extract to separate repo 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.
@dave-gantenbein any other case is handled by
newCheck
's default status that is set toin_progress
. Basically we only create our successful check when we have found a successful check, otherwise we mark it as pending.