Skip to content

Commit 13a211d

Browse files
committed
Example for restore-only cache in documentation (actions#696)
* cache-restore-only example * resolve conflicts * example update * format update * format update * update
1 parent 1b3cf6f commit 13a211d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,68 @@ steps:
316316

317317
> **Note**: If caching fails, the action logs a warning but continues execution without interrupting your workflow.
318318

319+
**Restore-Only Cache**
320+
321+
```yaml
322+
# In some workflows, you may want to restore a cache without saving it. This can help reduce cache writes and storage usage in workflows that only need to read from cache
323+
jobs:
324+
build:
325+
runs-on: ${{ matrix.os }}
326+
strategy:
327+
matrix:
328+
os: [ubuntu-latest, macos-latest, windows-latest]
329+
steps:
330+
- uses: actions/checkout@v6
331+
- name: Setup Go
332+
id: setup-go
333+
uses: actions/setup-go@v6
334+
with:
335+
go-version: '1.24.10'
336+
cache: false
337+
# Capture Go cache locations
338+
- name: Set Go cache variables (Linux/macOS)
339+
if: runner.os != 'Windows'
340+
run: |
341+
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" >> $GITHUB_ENV
342+
echo "GO_BUILD_CACHE=$(go env GOCACHE)" >> $GITHUB_ENV
343+
- name: Set Go cache variables (Windows)
344+
if: runner.os == 'Windows'
345+
shell: pwsh
346+
run: |
347+
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" | Out-File $env:GITHUB_ENV -Append
348+
echo "GO_BUILD_CACHE=$(go env GOCACHE)" | Out-File $env:GITHUB_ENV -Append
349+
# Normalize runner.arch to lowercase to ensure consistent cache keys
350+
- name: Normalize runner architecture (Linux/macOS)
351+
if: runner.os != 'Windows'
352+
shell: bash
353+
run: echo "ARCH=$(echo '${{ runner.arch }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
354+
- name: Normalize runner architecture (Windows)
355+
if: runner.os == 'Windows'
356+
shell: pwsh
357+
run: |
358+
$arch = "${{ runner.arch }}".ToLower()
359+
echo "ARCH=$arch" | Out-File $env:GITHUB_ENV -Append
360+
- name: Set cache OS suffix for Linux
361+
if: runner.os == 'Linux'
362+
shell: bash
363+
run: echo "CACHE_OS_SUFFIX=$ImageOS-" >> $GITHUB_ENV
364+
- name: Restore Go cache
365+
id: go-cache
366+
uses: actions/cache/restore@v5
367+
with:
368+
path: |
369+
${{ env.GO_MOD_CACHE }}
370+
${{ env.GO_BUILD_CACHE }}
371+
key: setup-go-${{ runner.os }}-${{ env.ARCH }}-${{ env.CACHE_OS_SUFFIX }}go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.sum') }}
372+
- name: Download modules
373+
run: go mod download
374+
- name: Build
375+
run: go build ./...
376+
```
377+
> 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
378+
others. The action [actions/cache/restore](https://github.com/actions/cache/tree/main/restore#only-restore-cache)
379+
should be used in this case.
380+
319381
### Matrix Testing
320382

321383
Test across multiple Go versions:

0 commit comments

Comments
 (0)