Compare commits

..

6 Commits

Author SHA1 Message Date
Sergey Dolin
d42f44315b
Merge 9b4533e484 into 883490dfd0 2023-08-30 14:30:50 +00:00
Sergey Dolin
9b4533e484 add requested changes 2023-08-30 16:30:41 +02:00
Sergey Dolin
ff99eca7af remove a sample with text file 2023-08-30 16:22:09 +02:00
Sergey Dolin
1ac9d654af Fix glob pattern use 2023-08-30 16:15:18 +02:00
Sergey Dolin
57dc459658 Add nesting 2023-08-30 15:57:32 +02:00
Sergey Dolin
3671db66cb Update README.md with more sample 2023-08-30 15:53:20 +02:00

View File

@ -163,7 +163,7 @@ in different subdirectories. The input supports glob patterns.
If some problem that prevents success caching happens then the action issues the warning in the log and continues the execution of the pipeline. If some problem that prevents success caching happens then the action issues the warning in the log and continues the execution of the pipeline.
**Caching in monorepos** ### Caching in monorepos
```yaml ```yaml
steps: steps:
@ -171,16 +171,11 @@ steps:
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: '1.17' go-version: '1.17'
check-latest: true cache-dependency-path: subdir/go.sum
cache-dependency-path: |
subdir/go.sum
tools/go.sum
# cache-dependency-path: "**/*.sum"
- run: go run hello.go - run: go run hello.go
``` ```
**Caching in multirepos** ### Caching in multirepos
`cache-dependency-path` input assepts glob patterns and multi-line values: `cache-dependency-path` input assepts glob patterns and multi-line values:
```yaml ```yaml
@ -205,10 +200,8 @@ steps:
- run: go run hello.go - run: go run hello.go
``` ```
## Multi-target builds ### Multi-target builds
`cache-dependency-path` input used to generate unuque cache key and it is not limited to only The 'cache-dependency-path' input doesn't limit itself to dependency lock files only. It can also be used with additional files that contain details about the build settings. By using this method, caches for builds created for various operating systems, architectures, etc. can be separated.
dependencies files. The common case is to add a file containing the extr info about the specific
build, for example build target.
```yaml ```yaml
env: env:
@ -216,16 +209,18 @@ env:
GOARCH: ... GOARCH: ...
steps: steps:
- run: echo "$GOOS $GOARCH"> /tmp/env - run: echo "$GOOS $GOARCH" > /tmp/env
- uses: actions/setup-go@v4 - uses: actions/setup-go@v4
with: with:
go-version: '1.17' go-version: '1.17'
cache-dependency-path: go.sum /tmp/env cache-dependency-path: |
go.sum
/tmp/env
- run: go run hello.go - run: go run hello.go
``` ```
## Invalidate cache when source code changes ### Invalidate cache when source code changes
Besides the dependencise the action caches the build outputs Besides the dependencise the action caches the build outputs
([GOCACHE](https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching) directory) but by default this ([GOCACHE](https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching) directory) but by default this
cache is not update in order to avoid unpredictable and frequent cache invaldation. Nevertheless cache is not update in order to avoid unpredictable and frequent cache invaldation. Nevertheless
@ -239,28 +234,15 @@ including the source code files into `cache-dependency-path` input.
- run: go run hello.go - run: go run hello.go
``` ```
But more practically to manage the cache with the text file manually updated according to the amount ### Caching with actions/cache
of changes made in the repo. The caching capabilities of setup-go are limited to the simplest and most popular use cases. If fine-grained tuning of caching is required, it's recommended to disable the built-in caching and use [actions/cache](https://github.com/actions/cache).
```yaml The example workflow below utilizes the `actions/cache` action and adds flexibility, for instance, it:
- uses: actions/setup-go@v4 - allows to configure cache path
with: - allows to have different caches for rare changed dependencies and more often changed intermediate build files
go-version: '1.17' - uses the `restore-key` input to restore the previous cache even if the current key cache has changed
cache-dependency-path: go.sum cache-version.txt - has different caches for the compiler's build outputs for different architectures
- run: go run hello.go - has custom cache keys for parallel builds
```
## Caching with actions/cache
The caching capabilities of the action are limited for the simplest builds and can be ineffective in the real world
use cases. If the build requires fine-grained turning the built-in caching should be disabled and
[actions/cache](https://github.com/actions/cache) should be used.
Here the sample `actions/cache` configuration with the extending options allowing
- configuring paths to cache
- have different caches for rare changes dependencies and more often changed intermediate build files
- use `restore-key` input to restore the previous cache even if the current key cache has changed
- have different caches intermediate build files of different architectures
- have custom cache key for parallel builds
```yaml ```yaml
build: build:
@ -285,8 +267,6 @@ build:
path: | path: |
${{ env.cache }} ${{ env.cache }}
key: setup-go-deps-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }} key: setup-go-deps-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }}
restore-keys: |
setup-go-deps-${{ runner.os }}-go-
- name: - name:
run: echo "$GOOS $GOARCH"> /tmp/env run: echo "$GOOS $GOARCH"> /tmp/env
@ -302,16 +282,16 @@ build:
``` ```
## Restore-only caches ### Restore-only caches
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 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
others. The action [actions/cache/restore](https://github.com/marketplace/actions/cache-restore) others. The action [actions/cache/restore](https://github.com/marketplace/actions/cache-restore)
should be used in this case. should be used in this case.
## Generate different caches ### Generate different caches
This advanced use case assumes manual definition of cache key and requires the use of This advanced use case assumes manual definition of cache key and requires the use of
[actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules) [actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules)
## Parallel builds ### Parallel builds
To avoid race conditions during the parallel builds they should either To avoid race conditions during the parallel builds they should either
[generate their own caches](#generate-different-caches), or create the cache [generate their own caches](#generate-different-caches), or create the cache
for only one build and [restore](#restore-only-caches) that cache in the other builds. for only one build and [restore](#restore-only-caches) that cache in the other builds.