mirror of
https://github.com/actions/cache.git
synced 2026-01-13 17:13:06 +08:00
Compare commits
3 Commits
18b88cd846
...
fe99b1551d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe99b1551d | ||
|
|
36f1e144e1 | ||
|
|
42e6e83ca6 |
32
examples.md
32
examples.md
@ -41,6 +41,9 @@
|
|||||||
- [Swift - Swift Package Manager](#swift---swift-package-manager)
|
- [Swift - Swift Package Manager](#swift---swift-package-manager)
|
||||||
- [Swift - Mint](#swift---mint)
|
- [Swift - Mint](#swift---mint)
|
||||||
- [* - Bazel](#---bazel)
|
- [* - Bazel](#---bazel)
|
||||||
|
- [Common use cases](#common-use-cases)
|
||||||
|
- [Restore-only caches](#restore-only-caches)
|
||||||
|
- [Automatically detect cached paths](#automatically-detect-cached-paths)
|
||||||
|
|
||||||
## Bun
|
## Bun
|
||||||
|
|
||||||
@ -712,3 +715,32 @@ steps:
|
|||||||
${{ runner.os }}-bazel-
|
${{ runner.os }}-bazel-
|
||||||
- run: bazelisk test //...
|
- run: bazelisk test //...
|
||||||
```
|
```
|
||||||
|
## Common use cases
|
||||||
|
|
||||||
|
### 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
|
||||||
|
others. The action [actions/cache/restore](https://github.com/actions/cache/blob/main/restore/README.md#only-restore-cache)
|
||||||
|
should be used in this case.
|
||||||
|
|
||||||
|
### Automatically detect cached paths
|
||||||
|
[Defining outputs for jobs](https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs) can be used to
|
||||||
|
automatically detect paths to cache and use them to configure `actions/cache` action.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- name: Get Go cached paths
|
||||||
|
id: find-cached-paths
|
||||||
|
run: |
|
||||||
|
echo "cache=$(go env GOCACHE)" >> $GITHUB_ENV
|
||||||
|
echo "modcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Set up cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
needs: find-cached-paths
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
${{ env.cache }}
|
||||||
|
${{ env.modcache }}
|
||||||
|
key: setup-go-${{ runner.os }}-go-${{ hashFiles('go.sum go.mod') }}
|
||||||
|
restore-keys: |
|
||||||
|
setup-go-${{ runner.os }}-go-
|
||||||
|
```
|
||||||
@ -79,8 +79,10 @@ To avoid saving a cache that already exists, the `cache-hit` output from a resto
|
|||||||
The `cache-primary-key` output from the restore step should also be used to ensure
|
The `cache-primary-key` output from the restore step should also be used to ensure
|
||||||
the cache key does not change during the build if it's calculated based on file contents.
|
the cache key does not change during the build if it's calculated based on file contents.
|
||||||
|
|
||||||
|
Here's an example where we imagine we're calculating a lot of prime numbers and want to cache them:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
name: Always Caching Primes
|
name: Always Caching Prime Numbers
|
||||||
|
|
||||||
on: push
|
on: push
|
||||||
|
|
||||||
@ -91,23 +93,23 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Restore cached Primes
|
- name: Restore cached Prime Numbers
|
||||||
id: cache-primes-restore
|
id: cache-prime-numbers-restore
|
||||||
uses: actions/cache/restore@v4
|
uses: actions/cache/restore@v4
|
||||||
with:
|
with:
|
||||||
key: ${{ runner.os }}-primes
|
key: ${{ runner.os }}-prime-numbers
|
||||||
path: |
|
path: |
|
||||||
path/to/dependencies
|
path/to/dependencies
|
||||||
some/other/dependencies
|
some/other/dependencies
|
||||||
|
|
||||||
# Intermediate workflow steps
|
# Intermediate workflow steps
|
||||||
|
|
||||||
- name: Always Save Primes
|
- name: Always Save Prime Numbers
|
||||||
id: cache-primes-save
|
id: cache-prime-numbers-save
|
||||||
if: always() && steps.cache-primes-restore.outputs.cache-hit != 'true'
|
if: always() && steps.cache-prime-numbers-restore.outputs.cache-hit != 'true'
|
||||||
uses: actions/cache/save@v4
|
uses: actions/cache/save@v4
|
||||||
with:
|
with:
|
||||||
key: ${{ steps.cache-primes-restore.outputs.cache-primary-key }}
|
key: ${{ steps.cache-prime-numbers-restore.outputs.cache-primary-key }}
|
||||||
path: |
|
path: |
|
||||||
path/to/dependencies
|
path/to/dependencies
|
||||||
some/other/dependencies
|
some/other/dependencies
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user