Skip to content

Commit 4dde556

Browse files
committed
add a build cache example to the README
With a test-cache job on the repo's workflow, to show how it works. We add a third party module dependency just to show what modules are downloaded at each run. The GOPATH example in the README was also slightly wrong, as it did not set up the working directory inside GOPATH for the future steps. Fix that. Remove test-gopath from this repository's workflow, as having to download dependencies via 'go get' would make it non-reproducible and perhaps fail in the future. The example in the README is enough moving forward, as everyone should be using modules by now. Fixes #7.
1 parent eb0a1cc commit 4dde556

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ jobs:
1717
- name: Test
1818
run: go test ./...
1919

20-
test-gopath:
21-
env:
22-
GOPATH: ${{ github.workspace }}
23-
GO111MODULE: off
20+
test-cache:
2421
runs-on: ubuntu-latest
2522
steps:
2623
- name: Install Go
@@ -29,7 +26,15 @@ jobs:
2926
go-version: 1.15.x
3027
- name: Checkout code
3128
uses: actions/checkout@v2
29+
- uses: actions/cache@v2
3230
with:
33-
path: ./src/github.com/${{ github.repository }}
31+
path: |
32+
~/go/pkg/mod # Module download cache
33+
~/.cache/go-build # Build cache (Linux)
34+
~/Library/Caches/go-build # Build cache (Mac)
35+
'%LocalAppData%\go-build' # Build cache (Windows)
36+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
37+
restore-keys: |
38+
${{ runner.os }}-go-
3439
- name: Test
3540
run: go test ./...

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ downloaded modules:
8787
${{ runner.os }}-go-
8888
```
8989

90+
You can also include Go's build cache, to improve incremental builds:
91+
92+
```yaml
93+
- uses: actions/cache@v2
94+
with:
95+
path: |
96+
~/go/pkg/mod # Module download cache
97+
~/.cache/go-build # Build cache (Linux)
98+
~/Library/Caches/go-build # Build cache (Mac)
99+
'%LocalAppData%\go-build' # Build cache (Windows)
100+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
101+
restore-keys: |
102+
${{ runner.os }}-go-
103+
```
104+
105+
This is demonstrated via the `test-cache` job [in this very repository](https://github.com/mvdan/github-actions-golang/actions).
106+
107+
See [this guide](https://docs.github.com/en/free-pro-team@latest/actions/guides/caching-dependencies-to-speed-up-workflows)
108+
for more details.
109+
90110
#### How do I run a step conditionally?
91111

92112
You can use `if` conditionals, using their [custom expression
@@ -168,11 +188,14 @@ jobs:
168188
env:
169189
GOPATH: ${{ github.workspace }}
170190
GO111MODULE: off
191+
defaults:
192+
run:
193+
working-directory: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}
171194
steps:
172195
- name: Checkout code
173196
uses: actions/checkout@v2
174197
with:
175-
path: ./src/github.com/${{ github.repository }}
198+
path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}
176199
```
177200

178201
## Quick links

actions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ package actions
66
import (
77
"fmt"
88
"runtime"
9+
10+
"rsc.io/quote"
911
)
1012

1113
func Demo() {
1214
fmt.Printf("Go version: %s\n", runtime.Version())
1315
fmt.Printf("GOOS: %s\n", runtime.GOOS)
1416
fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
17+
18+
fmt.Println(quote.Go())
1519
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module dummy.module/actions
22

33
go 1.14
4+
5+
require rsc.io/quote v1.5.2

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
2+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3+
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
4+
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
5+
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
6+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

0 commit comments

Comments
 (0)