Created
January 24, 2025 16:07
-
-
Save trung/3952101ba60cb82b4e14a0bd6a3aaa3f to your computer and use it in GitHub Desktop.
GitHub Actions workflow for Gradle: Executes the build process first, followed by running unit tests and integration tests in parallel. Leverages caching solutions from GitHub Actions `setup-gradle` and Gradle's local build cache. Configured to apply to both the `main` branch and other branches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code from SCM | |
uses: actions/checkout@v4 | |
- name: Set up Java | |
uses: actions/setup-java@v3 | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
cache-read-only: false # enable caching for branches | |
- name: Build | |
# we want to build for all Java files but not executing any tests | |
# also enable local build cache with --build-cache so the build is cached via `setup-gradle` Github Actions | |
run: ./gradlew build testClasses integrationTestClasses -x test -x integrationTest --parallel --build-cache | |
unit-test: | |
name: Run Unit Tests | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout code from SCM | |
uses: actions/checkout@v4 | |
- name: Set up Java | |
uses: actions/setup-java@v3 | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
# we don't want to cache any for this job | |
# instead, it will reuse the local build cache created in Build job | |
cache-read-only: true | |
- name: Run Unit Tests | |
run: ./gradlew test --parallel --build-cache | |
integartion-test: | |
name: Run Integration Tests | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout code from SCM | |
uses: actions/checkout@v4 | |
- name: Set up Java | |
uses: actions/setup-java@v3 | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v4 | |
with: | |
# we don't want to cache any for this job | |
# instead, it will reuse the local build cache created in Build job | |
cache-read-only: true | |
- name: Run Integration Tests | |
run: ./gradlew integrationTest --parallel --build-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# as we create caches for each branch, need to cleanup when branch is deleted | |
name: Cleanup Caches by a Branch | |
on: delete | |
jobs: | |
cleanup: | |
if: github.event.ref_type == 'branch' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Cleanup | |
run: | | |
echo "Fetching list of cache key for branch $BRANCH" | |
cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id') | |
## Setting this to not fail the workflow while deleting cache keys. | |
set +e | |
echo "Deleting caches..." | |
for cacheKey in $cacheKeysForPR | |
do | |
gh cache delete $cacheKey | |
done | |
echo "Done" | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GH_REPO: ${{ github.repository }} | |
BRANCH: ${{ github.event.ref }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment