Skip to content

Commit 55c6055

Browse files
committed
Add advanced Go use cases
1 parent 93397be commit 55c6055

File tree

1 file changed

+104
-2
lines changed

1 file changed

+104
-2
lines changed

docs/adrs/0000-caching-dependencies.md

Lines changed: 104 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,108 @@ 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+
run: |
123+
echo "cache=$(go env GOCACHE)" >> $GITHUB_ENV
124+
echo "modcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
125+
126+
- name: Set up dependencies cache
127+
uses: actions/cache@v3
128+
with:
129+
path: |
130+
${{ env.cache }}
131+
key: setup-go-deps-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }}
132+
restore-keys: |
133+
setup-go-deps-${{ runner.os }}-go-
134+
135+
- name:
136+
run: echo "$GOOS $GOARCH"> /tmp/env
137+
138+
- name: Set up intermediate built files cache
139+
uses: actions/cache@v3
140+
with:
141+
path: |
142+
${{ env.modcache }}
143+
key: setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}-${{ runner.os }}-go-${{ hashFiles('**/*.go /tmp/env') }}
144+
restore-keys: |
145+
setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}
146+
147+
```
148+
149+
150+
## Restore-only caches
151+
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
152+
others. The action [actions/cache/restore](https://github.com/actions/cache/blob/main/restore/README.md#only-restore-cache)
153+
should be used in this case.
154+
155+
## Include or exclude cached paths
156+
This advanced use case requires the use of
157+
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#automatically-detect-cached-paths)
158+
159+
## Generate different caches
160+
This advanced use case assumes manual definition of cache key and requires the use of
161+
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules)
162+
163+
## Parallel builds
164+
To avoid race conditions during the parallel builds they should either
165+
[generate their own caches](#generate-different-caches), or create the cache
166+
for only one build and [restore](#restore-only-caches) that cache in the other builds.
167+
66168
# Release process
67169

68170
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)