Skip to content

Commit f93865d

Browse files
committed
ci: add gradle caching to workflow jobs
1 parent 7f2ae02 commit f93865d

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

.github/workflows/java.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ jobs:
2121
with:
2222
java-version: 21
2323
distribution: "temurin"
24+
cache: "gradle"
2425
- name: Check if tests compile cleanly with starter sources
2526
run: ./gradlew compileStarterTestJava --continue
2627
working-directory: exercises
2728

28-
lint:
29-
name: Lint Java files using Checkstyle
29+
lint-all:
30+
name: Lint all Java files using Checkstyle
31+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
3032
runs-on: ubuntu-24.04
3133
steps:
3234
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
@@ -35,10 +37,28 @@ jobs:
3537
with:
3638
java-version: 21
3739
distribution: "temurin"
40+
cache: "gradle"
3841
- name: Run checkstyle
3942
run: ./gradlew check --exclude-task test --continue
4043
working-directory: exercises
4144

45+
lint-changed:
46+
name: Lint changed Java exercises using Checkstyle
47+
if: github.event_name == 'pull_request'
48+
runs-on: ubuntu-24.04
49+
steps:
50+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
51+
with:
52+
fetch-depth: 0
53+
- name: Set up JDK 21
54+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
55+
with:
56+
java-version: 21
57+
distribution: "temurin"
58+
cache: "gradle"
59+
- name: Lint changed exercises
60+
run: bin/lint-changed-exercise
61+
4262
test-all:
4363
name: Test all exercises using java-test-runner
4464
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
@@ -74,6 +94,12 @@ jobs:
7494
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
7595
with:
7696
fetch-depth: 0
97+
- name: Set up JDK 21
98+
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
99+
with:
100+
java-version: 21
101+
distribution: "temurin"
102+
cache: "gradle"
77103
- name: Test changed exercises using java-test-runner
78104
run: bin/test-changed-exercise
79105
- name: Print summary

bin/lint-changed-exercise

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# Determine the base branch of the PR
5+
BASE_BRANCH=${GITHUB_BASE_REF:-main}
6+
7+
# Fetch full history for proper diff
8+
git fetch origin "$BASE_BRANCH"
9+
10+
# Compute merge base
11+
MERGE_BASE=$(git merge-base HEAD origin/"$BASE_BRANCH")
12+
13+
# Get changed files relative to merge base
14+
changed_files=$(git diff --name-only "$MERGE_BASE" HEAD)
15+
16+
# If any Gradle build file changed, run the full suite and exit
17+
if echo "$changed_files" | grep -qE '\.(gradle|gradlew|bat)$|settings\.gradle'; then
18+
echo "Gradle build files changed, running full lint suite..."
19+
cd exercises && ./gradlew check --exclude-task test --continue
20+
exit 0
21+
fi
22+
23+
# Extract unique exercise directories
24+
changed_exercises=$(echo "$changed_files" | \
25+
grep -E '^exercises/(practice|concept)/[^/]+/.+\.java$' | \
26+
cut -d/ -f1-3 | sort -u)
27+
28+
if [ -z "$changed_exercises" ]; then
29+
echo "No relevant exercises changed, skipping linting."
30+
exit 0
31+
fi
32+
33+
# Print exercises
34+
echo "Changed exercises detected:"
35+
echo "$changed_exercises"
36+
echo "----------------------------------------"
37+
38+
# Run lint checks
39+
exit_code=0
40+
for dir in $changed_exercises; do
41+
slug=$(basename "$dir")
42+
43+
echo "========================================"
44+
echo "=== Running checkstyle for $slug ==="
45+
echo "========================================"
46+
47+
if [[ $dir == exercises/practice/* ]]; then
48+
./exercises/gradlew -p exercises ":practice:$slug:check" --exclude-task test || exit_code=1
49+
elif [[ $dir == exercises/concept/* ]]; then
50+
./exercises/gradlew -p exercises ":concept:$slug:check" --exclude-task test || exit_code=1
51+
fi
52+
done
53+
54+
exit $exit_code

0 commit comments

Comments
 (0)