|
| 1 | +name: Maven Cache Management |
| 2 | + |
| 3 | +on: |
| 4 | + # Every push to develop should trigger cache rejuvenation (dependencies might have changed) |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - develop |
| 8 | + # According to https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy |
| 9 | + # all caches are deleted after 7 days of no access. Make sure we rejuvenate every 7 days to keep it available. |
| 10 | + schedule: |
| 11 | + - cron: '23 2 * * 0' # Run for 'develop' every Sunday at 02:23 UTC (3:23 CET, 21:23 ET) |
| 12 | + # Enable manual cache management |
| 13 | + workflow_dispatch: |
| 14 | + # Delete branch caches once a PR is merged |
| 15 | + pull_request: |
| 16 | + types: |
| 17 | + - closed |
| 18 | + |
| 19 | +env: |
| 20 | + COMMON_CACHE_KEY: "dataverse-maven-cache" |
| 21 | + COMMON_CACHE_PATH: "~/.m2/repository" |
| 22 | + |
| 23 | +jobs: |
| 24 | + seed: |
| 25 | + name: Drop and Re-Seed Local Repository |
| 26 | + runs-on: ubuntu-latest |
| 27 | + if: ${{ github.event_name != 'pull_request' }} |
| 28 | + permissions: |
| 29 | + # Write permission needed to delete caches |
| 30 | + # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id |
| 31 | + actions: write |
| 32 | + contents: read |
| 33 | + steps: |
| 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v4 |
| 36 | + - name: Determine Java version from Parent POM |
| 37 | + run: echo "JAVA_VERSION=$(grep '<target.java.version>' modules/dataverse-parent/pom.xml | cut -f2 -d'>' | cut -f1 -d'<')" >> ${GITHUB_ENV} |
| 38 | + - name: Set up JDK ${{ env.JAVA_VERSION }} |
| 39 | + uses: actions/setup-java@v4 |
| 40 | + with: |
| 41 | + java-version: ${{ env.JAVA_VERSION }} |
| 42 | + distribution: temurin |
| 43 | + - name: Seed common cache |
| 44 | + run: | |
| 45 | + mvn -B -f modules/dataverse-parent dependency:go-offline dependency:resolve-plugins |
| 46 | + # This non-obvious order is due to the fact that the download via Maven above will take a very long time (7-8 min). |
| 47 | + # Jobs should not be left without a cache. Deleting and saving in one go leaves only a small chance for a cache miss. |
| 48 | + - name: Drop common cache |
| 49 | + run: | |
| 50 | + gh extension install actions/gh-actions-cache |
| 51 | + echo "🛒 Fetching list of cache keys" |
| 52 | + cacheKeys=$(gh actions-cache list -R ${{ github.repository }} -B develop | cut -f 1 ) |
| 53 | + |
| 54 | + ## Setting this to not fail the workflow while deleting cache keys. |
| 55 | + set +e |
| 56 | + echo "🗑️ Deleting caches..." |
| 57 | + for cacheKey in $cacheKeys |
| 58 | + do |
| 59 | + gh actions-cache delete $cacheKey -R ${{ github.repository }} -B develop --confirm |
| 60 | + done |
| 61 | + echo "✅ Done" |
| 62 | + env: |
| 63 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + - name: Save the common cache |
| 65 | + uses: actions/cache@v4 |
| 66 | + with: |
| 67 | + path: ${{ env.COMMON_CACHE_PATH }} |
| 68 | + key: ${{ env.COMMON_CACHE_KEY }} |
| 69 | + enableCrossOsArchive: true |
| 70 | + |
| 71 | + # Let's delete feature branch caches once their PR is merged - we only have 10 GB of space before eviction kicks in |
| 72 | + deplete: |
| 73 | + name: Deplete feature branch caches |
| 74 | + runs-on: ubuntu-latest |
| 75 | + if: ${{ github.event_name == 'pull_request' }} |
| 76 | + permissions: |
| 77 | + # `actions:write` permission is required to delete caches |
| 78 | + # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id |
| 79 | + actions: write |
| 80 | + contents: read |
| 81 | + steps: |
| 82 | + - name: Checkout repository |
| 83 | + uses: actions/checkout@v4 |
| 84 | + - name: Cleanup caches |
| 85 | + run: | |
| 86 | + gh extension install actions/gh-actions-cache |
| 87 | +
|
| 88 | + BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge |
| 89 | + echo "🛒 Fetching list of cache keys" |
| 90 | + cacheKeysForPR=$(gh actions-cache list -R ${{ github.repository }} -B $BRANCH | cut -f 1 ) |
| 91 | + |
| 92 | + ## Setting this to not fail the workflow while deleting cache keys. |
| 93 | + set +e |
| 94 | + echo "🗑️ Deleting caches..." |
| 95 | + for cacheKey in $cacheKeysForPR |
| 96 | + do |
| 97 | + gh actions-cache delete $cacheKey -R ${{ github.repository }} -B $BRANCH --confirm |
| 98 | + done |
| 99 | + echo "✅ Done" |
| 100 | + env: |
| 101 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments