Skip to content

Commit

Permalink
workflow: Add check-main-status workflow
Browse files Browse the repository at this point in the history
The workflow checks the result of check runs on the latest commit on
the main branch. When checks on main are failing or pending the
workflow will fail. This prevents introducing more potenisal issues
when main is already broken.

To skip the check when making a fix for main, add the fix-ci label to
the PR.

Signed-off-by: Gregers Gram Rygg <gregers.gram.rygg@nordicsemi.no>
  • Loading branch information
gregersrygg committed Oct 9, 2024
1 parent f39d090 commit 0455c4e
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/check-main-status.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Check Main Branch Status
on:
pull_request:
workflow_dispatch:
inputs:
pr_number:
description: "Pull Request number"
required: true

jobs:
check-main-status:
runs-on: ubuntu-24.04
env:
PR_NUMBER: ${{ github.event.number || github.event.inputs.pr_number }}
steps:
- name: Check if PR has 'fix-ci' label
id: has_fix_ci_label
uses: actions/github-script@v7
with:
retries: 3
script: |
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
});
const hasFixCiLabel = labels.data.map(label => label.name).includes('fix-ci');
console.log(`Has 'fix-ci' label: ${hasFixCiLabel}`);
return hasFixCiLabel;
- name: Ensure Main Branch Check Runs Are Successful
if: ${{ steps.has_fix_ci_label.outputs.result == 'false' }}
uses: actions/github-script@v7
with:
retries: 3
script: |
const mainRef = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'heads/main',
});
const mainSha = mainRef.data.object.sha;
console.log(`Main branch SHA: ${mainSha}`);
const checkRuns = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: mainSha,
});
console.log(`Check runs (${checkRuns.data.total_count}):`);
for (let run of checkRuns.data.check_runs) {
console.log(`- ${run.name}: ${run.conclusion}`);
}
const failedChecks = checkRuns.data.check_runs.filter(run => run.conclusion === 'failure');
const pendingChecks = checkRuns.data.check_runs.filter(run => run.status !== 'completed');
if (failedChecks.length > 0) {
core.setFailed('Cannot merge: The main branch has failing checks.');
} else if (pendingChecks.length > 0) {
core.setFailed('Checks for main branch are pending. Please try again later.');
} else {
console.log('Main branch is healthy.');
}

0 comments on commit 0455c4e

Please sign in to comment.