Skip to content

Commit

Permalink
Optimized Github Actions cache for dependency updates
Browse files Browse the repository at this point in the history
Changed to cache by date interval rather than file hash. Most commits to this project are dependency updates, which results in a poor cache hit rate. Better that most of the dependencies are cached than none. Also, better to avoid updating the cache on every commit given most changes are only affect a single dependency.
  • Loading branch information
freemanjp committed Mar 27, 2022
1 parent 5748e1f commit 0d8ad6f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
29 changes: 28 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -29,7 +29,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
cache: 'maven'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Build with Maven
run: .github/scripts/build.sh
env:
Expand Down Expand Up @@ -61,6 +74,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Install GPG key
run: |-
gpg --batch --import-options import-show --import << EOF
Expand Down
43 changes: 42 additions & 1 deletion .github/workflows/release.yml
Expand Up @@ -29,7 +29,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
cache: 'maven'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Build with Maven
run: .github/scripts/build.sh
env:
Expand Down Expand Up @@ -61,6 +74,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Install GPG key
run: |-
gpg --batch --import-options import-show --import << EOF
Expand Down Expand Up @@ -94,6 +121,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Configure Git user
run: >-
git config --global user.name 'John Freeman' &&
Expand Down
15 changes: 14 additions & 1 deletion .github/workflows/verify.yml
Expand Up @@ -29,7 +29,20 @@ jobs:
with:
java-version: 8
distribution: 'temurin'
cache: 'maven'
# Since code changes to this project are infrequent, and most commits
# change the pom.xml, we cache by date interval rather than file hash.
- name: Calculate cache interval
run: |
leveler="$(wc -c <<< "$GITHUB_REPOSITORY")"
echo "::set-output name=CACHE_INTERVAL::$(date +%Y)-$(($(($leveler + $(date +%-j))) / 30))"
id: cache-interval
- name: Cache multiple paths
uses: actions/cache@v3
with:
path: |
~/.m2/repository
!~/.m2/repository/com/github/gantsign/maven
key: ${{ runner.os }}-maven-${{ steps.cache-interval.outputs.CACHE_INTERVAL }}
- name: Build with Maven
run: .github/scripts/build.sh
env:
Expand Down

0 comments on commit 0d8ad6f

Please sign in to comment.