Skip to content
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

Create "All required checks succeeded" check run when "All required checks done" CI job is actually successful #241

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/check-required.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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.

on:
workflow_run:
types: [completed]
workflows: [CI]

permissions:
actions: read
checks: write

jobs:
required-jobs:
name: Check required jobs
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
// list jobs for worklow run attempt
const { data: { jobs } } = await github.rest.actions.listJobsForWorkflowRunAttempt({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
run_id: context.payload.workflow_run.id,
attempt_number: context.payload.workflow_run.run_attempt,
});
// check if required job was successful
var success = false;
core.info(`Checking jobs for workflow run ${context.payload.workflow_run.html_url}`);
jobs.forEach(job => {
var mark = '-'
if (job.name === 'All required checks done') {
if (job.conclusion === 'success') {
success = true;
mark = '✅';
} else {
mark = '❌';
}
}
core.info(`${mark} ${job.name}: ${job.conclusion}`);
});
// create check run if job was successful
if (success) {
await github.rest.checks.create({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
name: 'All required checks succeeded',
head_sha: context.payload.workflow_run.head_sha,
status: 'completed',
conclusion: 'success',
output: {
title: 'All required checks succeeded',
summary: `See [workflow run](${context.payload.workflow_run.html_url}) for details.`,
},
});
}
13 changes: 3 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ jobs:
path: fasttrackml-oci.tar

# Virtual job that can be configured as a required check before a PR can be merged.
# As GitHub considers a check as successful if it is skipped, we need to check its status in
# another workflow (check-required.yml) and create a check there.
all-required-checks-done:
name: All required checks done
if: ${{ always() }}
needs:
- lint
- golang-unit-tests
Expand All @@ -257,15 +258,7 @@ jobs:
- build-image
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
const results = ${{ toJSON(needs.*.result) }};
if (results.every(res => res === 'success')) {
core.info('All required checks succeeded');
} else {
core.setFailed('Some required checks failed');
}
- run: echo "All required checks done"

# Publish any push to a branch or tag to ghcr.io as a convenience
# Actual release to Docker Hub happens in a different workflow
Expand Down