|
19 | 19 | types: [created] |
20 | 20 |
|
21 | 21 | jobs: |
| 22 | + # Tests are skipped only when *every* commit since the last run that actually |
| 23 | + # executed them is a docs commit. Looking at the head commit alone would skip a |
| 24 | + # push whose last commit is `docs(…)` but which also carries code changes. |
| 25 | + check: |
| 26 | + if: github.event_name == 'push' |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + actions: read |
| 31 | + outputs: |
| 32 | + should-run: ${{ steps.check.outputs.should-run }} |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@v6 |
| 35 | + with: |
| 36 | + fetch-depth: 0 |
| 37 | + |
| 38 | + - name: Check commits since the last run |
| 39 | + id: check |
| 40 | + env: |
| 41 | + GH_TOKEN: ${{ github.token }} |
| 42 | + BRANCH: ${{ github.ref_name }} |
| 43 | + HEAD_SHA: ${{ github.sha }} |
| 44 | + BEFORE_SHA: ${{ github.event.before }} |
| 45 | + HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }} |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | +
|
| 49 | + decide() { |
| 50 | + echo "$2" |
| 51 | + echo "should-run=$1" >> "$GITHUB_OUTPUT" |
| 52 | + exit 0 |
| 53 | + } |
| 54 | +
|
| 55 | + if grep -qF 'skip ci' <<< "$HEAD_COMMIT_MESSAGE"; then |
| 56 | + decide false "Head commit asks to skip CI." |
| 57 | + fi |
| 58 | +
|
| 59 | + # Base = the commit tested by the most recent run of this workflow on this |
| 60 | + # branch that actually ran. Runs that were skipped or cancelled have a |
| 61 | + # conclusion of `skipped`/`cancelled`, the current run has none yet. |
| 62 | + base="$(gh run list \ |
| 63 | + --workflow tests.yml \ |
| 64 | + --branch "$BRANCH" \ |
| 65 | + --limit 50 \ |
| 66 | + --json headSha,conclusion \ |
| 67 | + --jq 'map(select(.conclusion == "success" or .conclusion == "failure")) | .[0].headSha // empty')" |
| 68 | +
|
| 69 | + if [[ -z "$base" ]] || ! git cat-file -e "${base}^{commit}" 2>/dev/null; then |
| 70 | + # No known previous run, or its commit is gone (force push): fall back |
| 71 | + # to the range of this push. |
| 72 | + base="$BEFORE_SHA" |
| 73 | + fi |
| 74 | +
|
| 75 | + if [[ -z "$base" ]] || ! git cat-file -e "${base}^{commit}" 2>/dev/null; then |
| 76 | + decide true "No usable base commit." |
| 77 | + fi |
| 78 | +
|
| 79 | + subjects="$(git log --format=%s "${base}..${HEAD_SHA}")" |
| 80 | +
|
| 81 | + if [[ -z "$subjects" ]]; then |
| 82 | + decide true "No new commit since ${base}." |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "Commits since ${base}:" |
| 86 | + echo "$subjects" |
| 87 | +
|
| 88 | + while IFS= read -r subject; do |
| 89 | + if [[ "$subject" != docs* ]]; then |
| 90 | + decide true "Non-docs commit found: ${subject}" |
| 91 | + fi |
| 92 | + done <<< "$subjects" |
| 93 | +
|
| 94 | + decide false "Every commit since ${base} is a docs commit." |
| 95 | +
|
22 | 96 | test: |
| 97 | + needs: [check] |
23 | 98 | if: > |
24 | | - (github.event_name == 'push' |
25 | | - && !contains(github.event.head_commit.message, 'skip ci') |
26 | | - && !startsWith(github.event.head_commit.message, 'docs')) || |
| 99 | + always() && |
| 100 | + ((github.event_name == 'push' && needs.check.outputs.should-run == 'true') || |
27 | 101 | (github.event.issue.pull_request && startsWith(github.event.comment.body, '/test') && github.actor == 'JesusTheHun') || |
28 | | - github.event_name == 'workflow_dispatch' |
| 102 | + github.event_name == 'workflow_dispatch') |
29 | 103 | timeout-minutes: 30 |
30 | 104 | runs-on: |
31 | 105 | labels: blacksmith-4vcpu-ubuntu-2404 |
|
0 commit comments