Skip to content

Commit acf8502

Browse files
authored
Adds caching to make CI actions faster when downloading dependencies (#2583)
implements caching for go dependencies Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
1 parent 82c6f84 commit acf8502

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

.github/workflows/lint.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,27 @@ jobs:
1717
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6
1818
with:
1919
go-version-file: 'go.mod'
20-
cache: true
20+
cache: true # Caches go modules
21+
cache-dependency-path: go.sum
22+
23+
# Download all dependencies upfront (will be cached)
24+
- name: Download Go dependencies
25+
run: |
26+
go mod download
27+
go mod verify
28+
29+
# Cache Go build cache for faster compilation during linting
30+
- name: Cache Go build cache
31+
uses: actions/cache@v4
32+
with:
33+
path: ~/.cache/go-build
34+
key: ${{ runner.os }}-go-build-lint-${{ hashFiles('**/go.sum') }}
35+
restore-keys: |
36+
${{ runner.os }}-go-build-lint-
37+
${{ runner.os }}-go-build-
2138
2239
- name: Run golangci-lint
2340
uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0
2441
with:
25-
skip-cache: true
42+
# Enable golangci-lint's built-in caching (removes skip-cache: true)
2643
args: --timeout=5m

.github/workflows/test.yml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,47 @@ jobs:
1717
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6
1818
with:
1919
go-version-file: 'go.mod'
20-
cache: true
20+
cache: true # This caches go modules based on go.sum
21+
cache-dependency-path: go.sum
2122

22-
- name: Set up gotestfmt
23-
uses: gotesttools/gotestfmt-action@v2
23+
# Download all dependencies upfront (will be cached)
24+
- name: Download Go dependencies
25+
run: |
26+
go mod download
27+
go mod verify
28+
29+
# Cache Go build cache for faster compilation
30+
# Note: ~/go/pkg/mod is already cached by actions/setup-go with cache: true
31+
- name: Cache Go build cache
32+
uses: actions/cache@v4
33+
with:
34+
path: ~/.cache/go-build
35+
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}
36+
restore-keys: |
37+
${{ runner.os }}-go-build-
38+
39+
# Cache Go tools (gotestfmt only for tests)
40+
- name: Cache Go tools
41+
id: cache-go-tools
42+
uses: actions/cache@v4
2443
with:
25-
# avoid rate limiting
26-
token: ${{ secrets.GITHUB_TOKEN }}
44+
path: ~/go/bin
45+
key: ${{ runner.os }}-go-tools-${{ hashFiles('go.mod') }}-gotestfmt-v2
46+
restore-keys: |
47+
${{ runner.os }}-go-tools-
48+
49+
# Only install gotestfmt if not cached
50+
- name: Install gotestfmt (if not cached)
51+
if: steps.cache-go-tools.outputs.cache-hit != 'true'
52+
run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
2753

2854
- name: Install Task
2955
uses: arduino/setup-task@v2
3056
with:
3157
version: 3.44.1
3258
repo-token: ${{ secrets.GITHUB_TOKEN }}
3359

60+
# Run tests with all dependencies already cached
3461
- name: Run tests with coverage
3562
run: task test-coverage
3663

.github/workflows/verify-gen.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,37 @@ jobs:
1616
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6
1717
with:
1818
go-version-file: go.mod
19+
cache: true # Cache go modules
20+
# Cache Go tools (mockgen)
21+
- name: Cache Go tools
22+
id: cache-go-tools
23+
uses: actions/cache@v4
24+
with:
25+
path: ~/go/bin
26+
key: ${{ runner.os }}-go-codegen-tools-${{ hashFiles('go.mod') }}-mockgen
27+
restore-keys: |
28+
${{ runner.os }}-go-codegen-tools-
29+
1930
- name: Install Task
2031
uses: arduino/setup-task@v2
2132
with:
2233
version: 3.44.1
2334
repo-token: ${{ secrets.GITHUB_TOKEN }}
24-
- name: Install mockgen
35+
36+
# Only install mockgen if not cached
37+
- name: Install mockgen (if not cached)
38+
if: steps.cache-go-tools.outputs.cache-hit != 'true'
2539
run: task mock-install
40+
2641
- name: Generate code files
2742
run: task gen
2843
- name: Check for changes
2944
run: |
30-
if ! git diff --exit-code docs/server/; then
45+
if ! git diff --exit-code; then
3146
echo "❌ Generated code files are not up to date!"
3247
echo "Please run 'task gen' and commit the changes."
48+
echo "Files changed:"
49+
git diff --name-only
3350
exit 1
3451
else
3552
echo "✅ Generated code files are up to date!"

Taskfile.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ tasks:
4848
platforms: [linux, darwin]
4949
internal: true
5050
cmds:
51-
- go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
52-
# we have to use ldflags to avoid the LC_DYSYMTAB linker error.
51+
# Only install gotestfmt if not already installed
52+
- cmd: which gotestfmt > /dev/null 2>&1 || go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
53+
platforms: [linux, darwin]
54+
# we have to use ldflags to avoid the LC_DYSYMTAB linker error.
5355
# https://github.com/stacklok/toolhive/issues/1687
5456
- go test -ldflags=-extldflags=-Wl,-w -v -json -race $(go list ./... | grep -v '/test/e2e' | grep -v '/cmd/thv-operator/test-integration') | gotestfmt -hide "all"
5557

@@ -79,8 +81,10 @@ tasks:
7981
cmds:
8082
- cmd: mkdir -p coverage
8183
platforms: [linux, darwin]
82-
- go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
83-
# we have to use ldflags to avoid the LC_DYSYMTAB linker error.
84+
# Only install gotestfmt if not already installed
85+
- cmd: which gotestfmt > /dev/null 2>&1 || go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
86+
platforms: [linux, darwin]
87+
# we have to use ldflags to avoid the LC_DYSYMTAB linker error.
8488
# https://github.com/stacklok/toolhive/issues/1687
8589
- go test -ldflags=-extldflags=-Wl,-w -json -race -coverprofile=coverage/coverage.out $(go list ./... | grep -v '/test/e2e' | grep -v '/cmd/thv-operator/test-integration') | gotestfmt -hide "all"
8690
- go tool cover -func=coverage/coverage.out

0 commit comments

Comments
 (0)