Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom golang setup action for better caching #6144

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/actions/setup-go/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: 'Setup golang with master only caching'
description: 'A composite action that installs golang, but with a caching strategy that only updates the cache on master branch.'
inputs:
go-version:
description: 'Override the version of Go to install.'
required: false

runs:
using: 'composite'
steps:
- uses: actions/setup-go@v5
if: inputs.go-version == ''
with:
go-version-file: 'go.mod' # Just use whatever version is in the go.mod file
cache: ${{ github.ref == 'refs/heads/master' }}

- uses: actions/setup-go@v5
if: inputs.go-version != ''
with:
go-version: ${{ inputs.go-version }}
cache: ${{ github.ref == 'refs/heads/master' }}

- name: Prepare for go cache
if: ${{ github.ref != 'refs/heads/master' }}
shell: bash
run: |
echo "GO_CACHE=$(go env GOCACHE)" | tee -a "$GITHUB_ENV"
echo "GO_MODCACHE=$(go env GOMODCACHE)" | tee -a "$GITHUB_ENV"
echo "GO_VERSION=$(go env GOVERSION | tr -d 'go')" | tee -a "$GITHUB_ENV"

- name: Setup read-only cache
if: ${{ github.ref != 'refs/heads/master' }}
uses: actions/cache/restore@v4
with:
path: |
${{ env.GO_MODCACHE }}
${{ env.GO_CACHE }}
# Match the cache key to the setup-go action https://github.com/actions/setup-go/blob/main/src/cache-restore.ts#L34
key: setup-go-${{ runner.os }}-${{ env.ImageOS }}-go-${{ env.GO_VERSION }}-${{ hashFiles('go.sum') }}
6 changes: 2 additions & 4 deletions .github/workflows/test-suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ jobs:
set +x
VERSION_GOLANG=$(echo $VERSION_GOLANG | sed 's/go//')
echo "VERSION_GOLANG=${VERSION_GOLANG}" >> "$GITHUB_OUTPUT"
- uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ steps.go-finder.outputs.VERSION_GOLANG }}
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Install OS Packages
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/unittest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ jobs:
with:
fetch-depth: 1
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
uses: ./.github/actions/setup-go
- name: Run Unit Tests
run: |
go test -coverpkg=./... -coverprofile=coverage.out ./pkg/... -run Unit
Expand Down
Loading