Skip to content

Commit 013fabc

Browse files
committed
Add advanced Go use cases
1 parent 93397be commit 013fabc

File tree

1 file changed

+105
-2
lines changed

1 file changed

+105
-2
lines changed

docs/adrs/0000-caching-dependencies.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ We don't pursue the goal to provide wide customization of caching in scope of `a
2828

2929
# Example of real use-cases
3030

31-
- With cache
31+
## With cache
3232

3333
```yml
3434
steps:
@@ -39,7 +39,7 @@ steps:
3939
cache: true
4040
```
4141
42-
- With cache-dependency-path
42+
## With cache-dependency-path
4343
4444
```yml
4545
steps:
@@ -63,6 +63,109 @@ steps:
6363
**/go.mod
6464
```
6565
66+
```yml
67+
steps:
68+
- uses: actions/checkout@v3
69+
- uses: actions/setup-go@v3
70+
with:
71+
go-version: '18'
72+
cache: true
73+
cache-dependency-path: **/go.sum
74+
```
75+
76+
## Multi-target builds
77+
```yaml
78+
env:
79+
GOOS: ...
80+
GOARCH: ...
81+
82+
steps:
83+
- run: echo "$GOOS $GOARCH"> /tmp/env
84+
85+
- uses: actions/setup-go@v4
86+
with:
87+
cache-dependency-path: go.sum /tmp/env
88+
```
89+
90+
## Invalidate cache if source code changes
91+
```yaml
92+
- uses: actions/setup-go@v4
93+
with:
94+
go-version: '1.20'
95+
cache-dependency-path: go.sum **/*.go
96+
```
97+
98+
## Caching with actions/cache
99+
The caching capabilities of the action are limited for the simplest builds and can be ineffective in the real world
100+
use cases. If the build requires fine-grained turning the built-in caching should be disabled and
101+
[actions/cache](https://github.com/actions/cache) should be used.
102+
103+
Here the sample `actions/cache` configuration with the extending options allowing
104+
- configuring paths to cache
105+
- have different caches for rare changes dependencies and more often changed intermediate build files
106+
- have different caches intermediate build files of different architectures
107+
- have custom cache key for parallel builds
108+
109+
```yaml
110+
build:
111+
env:
112+
GOOS: ...
113+
GOARCH: ...
114+
115+
steps:
116+
- uses: actions/setup-go@v4
117+
with:
118+
go-version: "1.20.x"
119+
cache: false
120+
121+
- name: Get Go cached paths
122+
id: find-cached-paths
123+
run: |
124+
echo "cache=$(go env GOCACHE)" >> $GITHUB_ENV
125+
echo "modcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
126+
127+
- name: Set up dependencies cache
128+
uses: actions/cache@v3
129+
with:
130+
path: |
131+
${{ env.cache }}
132+
key: setup-go-deps-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }}
133+
restore-keys: |
134+
setup-go-deps-${{ runner.os }}-go-
135+
136+
- name:
137+
run: echo "$GOOS $GOARCH"> /tmp/env
138+
139+
- name: Set up intermediate built files cache
140+
uses: actions/cache@v3
141+
with:
142+
path: |
143+
${{ env.modcache }}
144+
key: setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}-${{ runner.os }}-go-${{ hashFiles('**/*.go /tmp/env') }}
145+
restore-keys: |
146+
setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}
147+
148+
```
149+
150+
151+
## Restore-only caches
152+
If there are several builds on the same repo it might make sense to create a cache in one build and use it in the
153+
others. The action [actions/cache/restore](https://github.com/actions/cache/blob/main/restore/README.md#only-restore-cache)
154+
should be used in this case.
155+
156+
## Include or exclude cached paths
157+
This advanced use case requires the use of
158+
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#automatically-detect-cached-paths)
159+
160+
## Generate different caches
161+
This advanced use case assumes manual definition of cache key and requires the use of
162+
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules)
163+
164+
## Parallel builds
165+
To avoid race conditions during the parallel builds they should either
166+
[generate their own caches](#generate-different-caches), or create the cache
167+
for only one build and [restore](#restore-only-caches) that cache in the other builds.
168+
66169
# Release process
67170

68171
As soon as functionality is implemented, we will release minor update of action. No need to bump major version since there are no breaking changes for existing users.

0 commit comments

Comments
 (0)