|
| 1 | +name: 'Setup the Go environment' |
| 2 | +description: 'Installs go and restores/saves the build/module cache' |
| 3 | +runs: |
| 4 | + using: "composite" |
| 5 | + steps: |
| 6 | + - name: Set up Go |
| 7 | + uses: actions/setup-go@v6 |
| 8 | + with: |
| 9 | + go-version: '1.25' |
| 10 | + # unfortunately we cannot use the provided caching because it uses the |
| 11 | + # same cache for all workflows/jobs, leading to undesired cache clashes, |
| 12 | + # causing uncached test runs etc ... |
| 13 | + # You can see the cache key at https://github.com/actions/setup-go/blob/4e0b6c77c6448caafaff5eed51516cad78e7639a/src/cache-restore.ts#L34 |
| 14 | + # There is a ticket to add prefixes for the cache, which should solve it https://github.com/actions/setup-go/issues/358 |
| 15 | + cache: false |
| 16 | + |
| 17 | + # Restore original modification time of files based on the date of the most |
| 18 | + # recent commit that modified them as mtimes affect the Go test cache. |
| 19 | + # See https://github.com/golang/go/issues/58571 for details |
| 20 | + - name: Restore modification time of checkout files |
| 21 | + uses: chetan/git-restore-mtime-action@075f9bc9d159805603419d50f794bd9f33252ebe |
| 22 | + |
| 23 | + |
| 24 | + # KEY_PREFIX must uniquely identify the specific instance of a job executing. |
| 25 | + - shell: bash |
| 26 | + run: | |
| 27 | + echo 'KEY_PREFIX=${{ github.workflow }}-${{ github.job }}-${{ runner.os }}-${{ inputs.go-version }}-matrix(${{ join(matrix.*,'|') }})' >> $GITHUB_OUTPUT |
| 28 | + echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT |
| 29 | + echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT |
| 30 | + if [ ${{ runner.os }} != 'Windows' ]; then echo SUDO='sudo' >> $GITHUB_OUTPUT; fi |
| 31 | + id: variables |
| 32 | + |
| 33 | + # Cache the Go Modules downloaded during the job. |
| 34 | + - uses: actions/cache@v4 |
| 35 | + with: |
| 36 | + path: ${{ steps.variables.outputs.GOMODCACHE }} |
| 37 | + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod-${{ hashFiles('**/go.sum') }} |
| 38 | + restore-keys: ${{ steps.variables.outputs.KEY_PREFIX }}-go-mod- |
| 39 | + |
| 40 | + # Cache any build and test artifacts during the job, which will speed up |
| 41 | + # rebuilds and cause test runs to skip tests that have no reason to rerun. |
| 42 | + - uses: actions/cache@v4 |
| 43 | + with: |
| 44 | + path: ${{ steps.variables.outputs.GOCACHE }} |
| 45 | + key: ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}-${{ hashFiles('**', '!.git') }} |
| 46 | + restore-keys: | |
| 47 | + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build-${{ github.ref }}- |
| 48 | + ${{ steps.variables.outputs.KEY_PREFIX }}-go-build- |
| 49 | +
|
| 50 | + # Reset the cache for master/protected branches, to ensure they build and run the tests from zero |
| 51 | + # and that the module cache is cleaned (otherwise it accumulates orphan dependencies over time). |
| 52 | + - if: github.ref_protected |
| 53 | + shell: bash |
| 54 | + run: ${{ steps.variables.outputs.SUDO }} rm -rf ${{ steps.variables.outputs.GOMODCACHE }} ${{ steps.variables.outputs.GOCACHE }} |
| 55 | + |
0 commit comments