Skip to content

Commit 3671db6

Browse files
committed
Update README.md with more sample
1 parent 883490d commit 3671db6

File tree

1 file changed

+136
-5
lines changed

1 file changed

+136
-5
lines changed

README.md

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,146 @@ steps:
171171
- uses: actions/setup-go@v4
172172
with:
173173
go-version: '1.17'
174-
check-latest: true
175-
cache-dependency-path: |
176-
subdir/go.sum
177-
tools/go.sum
178-
# cache-dependency-path: "**/*.sum"
174+
cache-dependency-path: subdir/go.sum
175+
- run: go run hello.go
176+
```
179177

178+
**Caching in multirepos**
179+
`cache-dependency-path` input assepts glob patterns and multi-line values:
180+
181+
```yaml
182+
steps:
183+
- uses: actions/checkout@v3
184+
- uses: actions/setup-go@v4
185+
with:
186+
go-version: '1.17'
187+
cache-dependency-path: **/go.sum
180188
- run: go run hello.go
181189
```
182190

191+
```yaml
192+
steps:
193+
- uses: actions/checkout@v3
194+
- uses: actions/setup-go@v4
195+
with:
196+
go-version: '1.17'
197+
cache-dependency-path: |
198+
subdir1/go.sum
199+
subdir2/go.mod
200+
- run: go run hello.go
201+
```
202+
203+
## Multi-target builds
204+
`cache-dependency-path` input used to generate unuque cache key and it is not limited to only
205+
dependencies files. The common case is to add a file containing the extr info about the specific
206+
build, for example build target.
207+
208+
```yaml
209+
env:
210+
GOOS: ...
211+
GOARCH: ...
212+
213+
steps:
214+
- run: echo "$GOOS $GOARCH"> /tmp/env
215+
216+
- uses: actions/setup-go@v4
217+
with:
218+
go-version: '1.17'
219+
cache-dependency-path: go.sum /tmp/env
220+
- run: go run hello.go
221+
```
222+
223+
## Invalidate cache when source code changes
224+
Besides the dependencise the action caches the build outputs
225+
([GOCACHE](https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching) directory) but by default this
226+
cache is not update in order to avoid unpredictable and frequent cache invaldation. Nevertheless
227+
including the source code files into `cache-dependency-path` input.
228+
229+
```yaml
230+
- uses: actions/setup-go@v4
231+
with:
232+
go-version: '1.17'
233+
cache-dependency-path: go.sum **/*.go
234+
- run: go run hello.go
235+
```
236+
237+
But more practically to manage the cache with the text file manually updated according to the amount
238+
of changes made in the repo.
239+
240+
```yaml
241+
- uses: actions/setup-go@v4
242+
with:
243+
go-version: '1.17'
244+
cache-dependency-path: go.sum cache-version.txt
245+
- run: go run hello.go
246+
```
247+
248+
## Caching with actions/cache
249+
The caching capabilities of the action are limited for the simplest builds and can be ineffective in the real world
250+
use cases. If the build requires fine-grained turning the built-in caching should be disabled and
251+
[actions/cache](https://github.com/actions/cache) should be used.
252+
253+
Here the sample `actions/cache` configuration with the extending options allowing
254+
- configuring paths to cache
255+
- have different caches for rare changes dependencies and more often changed intermediate build files
256+
- use `restore-key` input to restore the previous cache even if the current key cache has changed
257+
- have different caches intermediate build files of different architectures
258+
- have custom cache key for parallel builds
259+
260+
```yaml
261+
build:
262+
env:
263+
GOOS: ...
264+
GOARCH: ...
265+
266+
steps:
267+
- uses: actions/setup-go@v4
268+
with:
269+
go-version: "1.17"
270+
cache: false
271+
272+
- name: Get Go cached paths
273+
run: |
274+
echo "cache=$(go env GOCACHE)" >> $GITHUB_ENV
275+
echo "modcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
276+
277+
- name: Set up dependencies cache
278+
uses: actions/cache@v3
279+
with:
280+
path: |
281+
${{ env.cache }}
282+
key: setup-go-deps-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }}
283+
restore-keys: |
284+
setup-go-deps-${{ runner.os }}-go-
285+
286+
- name:
287+
run: echo "$GOOS $GOARCH"> /tmp/env
288+
289+
- name: Set up intermediate built files cache
290+
uses: actions/cache@v3
291+
with:
292+
path: |
293+
${{ env.modcache }}
294+
key: setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}-${{ runner.os }}-go-${{ hashFiles('**/*.go /tmp/env') }}
295+
restore-keys: |
296+
setup-go-build-${{ env.GOOS }}-${{ env.GOARCH }}
297+
298+
```
299+
300+
## Restore-only caches
301+
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
302+
others. The action [actions/cache/restore](https://github.com/marketplace/actions/cache-restore)
303+
should be used in this case.
304+
305+
## Generate different caches
306+
This advanced use case assumes manual definition of cache key and requires the use of
307+
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules)
308+
309+
## Parallel builds
310+
To avoid race conditions during the parallel builds they should either
311+
[generate their own caches](#generate-different-caches), or create the cache
312+
for only one build and [restore](#restore-only-caches) that cache in the other builds.
313+
183314
## Getting go version from the go.mod file
184315

185316
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be

0 commit comments

Comments
 (0)