Skip to content

Commit 6e0951f

Browse files
committed
ci(tests): skip only when every commit since the last run is docs
The job condition looked at the head commit alone, so a push ending with a `docs(...)` commit skipped the tests even when it also carried code changes. A new `check` job resolves the last run of this workflow that actually executed, then walks every commit since that one; the tests are skipped only if all of them are docs commits.
1 parent 961f20e commit 6e0951f

1 file changed

Lines changed: 78 additions & 4 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,87 @@ on:
1919
types: [created]
2020

2121
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+
2296
test:
97+
needs: [check]
2398
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') ||
27101
(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')
29103
timeout-minutes: 30
30104
runs-on:
31105
labels: blacksmith-4vcpu-ubuntu-2404

0 commit comments

Comments
 (0)