|
6 | 6 | - main |
7 | 7 |
|
8 | 8 | jobs: |
9 | | - _: |
| 9 | + check-previous-run: |
| 10 | + runs-on: ubuntu-24.04 |
| 11 | + outputs: |
| 12 | + skip_ci: ${{ steps.decide.outputs.skip_ci }} |
| 13 | + steps: |
| 14 | + - name: Checkout the repo |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 # Required for full history / merge-base comparison |
| 18 | + |
| 19 | + - name: Fetch main branch |
| 20 | + run: git fetch origin main |
| 21 | + |
| 22 | + # Note: This is a double-check for the branch protection rule |
| 23 | + # which also ensures the PR is up-to-date with main |
| 24 | + - name: Ensure commit was based on latest main |
| 25 | + id: up_to_date_check |
| 26 | + run: | |
| 27 | + BASE=$(git merge-base $GITHUB_SHA origin/main) |
| 28 | + MAIN=$(git rev-parse origin/main) |
| 29 | +
|
| 30 | + echo "Merge base with main: $BASE" |
| 31 | + echo "Current origin/main: $MAIN" |
| 32 | +
|
| 33 | + if [[ "$BASE" != "$MAIN" ]]; then |
| 34 | + echo "❌ This commit was NOT based on latest main." |
| 35 | + echo "::error title=Outdated Branch::This commit is not based on latest main. CI must run in the PR, not here." |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + echo "✅ This commit was based on the latest main." |
| 40 | +
|
| 41 | + - name: Check if CI already passed for this commit (via REST API) |
| 42 | + id: decide |
| 43 | + env: |
| 44 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + run: | |
| 46 | + COMMIT_SHA=${GITHUB_SHA} |
| 47 | + echo "🔍 Checking combined status for commit: $COMMIT_SHA" |
| 48 | +
|
| 49 | + RESPONSE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 50 | + -H "Accept: application/vnd.github+json" \ |
| 51 | + "https://api.github.com/repos/${{ github.repository }}/commits/$COMMIT_SHA/status") |
| 52 | +
|
| 53 | + STATE=$(echo "$RESPONSE" | jq -r '.state') |
| 54 | +
|
| 55 | + echo "Combined commit status: $STATE" |
| 56 | +
|
| 57 | + if [[ "$STATE" == "success" ]]; then |
| 58 | + echo "✅ CI already passed for this commit. Skipping full CI." |
| 59 | + echo "skip_ci=true" >> $GITHUB_OUTPUT |
| 60 | + else |
| 61 | + echo "❌ CI has not passed (state: $STATE). Will run CI." |
| 62 | + echo "skip_ci=false" >> $GITHUB_OUTPUT |
| 63 | + fi |
| 64 | +
|
| 65 | + run-ci: |
| 66 | + needs: check-previous-run |
| 67 | + if: needs.check-previous-run.outputs.skip_ci == 'false' |
10 | 68 | uses: ./.github/workflows/_build_test.yml |
11 | 69 | secrets: inherit |
0 commit comments