From f3fd9fb62660ab902184f7c533f714f15e9cb59f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Wed, 19 Jun 2024 03:54:36 +0200 Subject: [PATCH] Reenable our own cache --- .github/actions/setup-go/action.yml | 34 ++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.yml index 1936ca24..7167b3d9 100644 --- a/.github/actions/setup-go/action.yml +++ b/.github/actions/setup-go/action.yml @@ -7,6 +7,11 @@ runs: uses: actions/setup-go@v5 with: go-version: '1.22' + # unfortunately we cannot use the provided caching because it uses the + # same cache for all workflows/jobs, leading to undesired cache restores + # causing uncached test runs etc ... + # You can see the cache key at https://github.com/actions/setup-go/blob/4e0b6c77c6448caafaff5eed51516cad78e7639a/src/cache-restore.ts#L34 + cache: false # Restore original modification time of files based on the date of the most # recent commit that modified them as mtimes affect the Go test cache. @@ -14,8 +19,35 @@ runs: - name: Restore modification time of checkout files uses: chetan/git-restore-mtime-action@075f9bc9d159805603419d50f794bd9f33252ebe + + # KEY_PREFIX must uniquely identify the specific instance of a job executing. + - shell: bash + run: | + echo 'KEY_PREFIX=${{ github.workflow }}-${{ github.job }}-${{ runner.os }}-${{ inputs.go-version }}-matrix(${{ join(matrix.*,'|') }})' >> $GITHUB_OUTPUT + echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT + echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT + id: variables + + # Cache the Go Modules downloaded during the job. + - uses: actions/cache@v4 + with: + path: ${{ steps.variables.outputs.GOMODCACHE }} + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod- + + # Cache any build and test artifacts during the job, which will speed up + # rebuilds and cause test runs to skip tests that have no reason to rerun. + - uses: actions/cache@v4 + with: + path: ${{ steps.variables.outputs.GOCACHE }} + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}-${{ hashFiles('**', '!.git') }} + restore-keys: | + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}- + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build- + # Reset the cache for master/protected branches, to ensure they build and run the tests from zero # and that the module cache is cleaned (otherwise it accumulates orphan dependencies over time). - if: github.ref_protected shell: bash - run: sudo rm -rf ~/.cache/go-build ~/go/pkg/mod + run: sudo rm -rf ${{ steps.variables.outputs.GOMODCACHE }} ${{ steps.variables.outputs.KEY_PREFIX }} +