@ -321,74 +321,68 @@ steps:
```yaml
```yaml
# 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
# 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
jobs:
jobs:
build:
build:
runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
strategy:
matrix:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v5
- name: Setup Go
- name: Setup Go
id: setup-go
id: setup-go
uses: actions/setup-go@v6
uses: actions/setup-go@v6
with:
with:
go-version: '1.24.10'
go-version: '1.24.10'
cache: false
cache: false
# Capture Go cache locations
- name: Set Go cache variables (Linux/macOS)
# Capture Go cache locations
if: runner.os != 'Windows'
- name: Set Go cache variables (Linux/macOS)
run: |
if: runner.os != 'Windows'
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" >> $GITHUB_ENV
run: |
echo "GO_BUILD_CACHE=$(go env GOCACHE)" >> $GITHUB_ENV
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" >> $GITHUB_ENV
- name: Set Go cache variables (Windows)
echo "GO_BUILD_CACHE=$(go env GOCACHE)" >> $GITHUB_ENV
if: runner.os == 'Windows'
shell: pwsh
- name: Set Go cache variables (Windows)
run: |
if: runner.os == 'Windows'
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" | Out-File $env:GITHUB_ENV -Append
shell: pwsh
echo "GO_BUILD_CACHE=$(go env GOCACHE)" | Out-File $env:GITHUB_ENV -Append
run: |
# Normalize runner.arch to lowercase to ensure consistent cache keys
echo "GO_MOD_CACHE=$(go env GOMODCACHE)" | Out-File $env:GITHUB_ENV -Append
- name: Normalize runner architecture (Linux/macOS)
echo "GO_BUILD_CACHE=$(go env GOCACHE)" | Out-File $env:GITHUB_ENV -Append
if: runner.os != 'Windows'
shell: bash
# runner.arch casing differs across platforms (e.g. X64 vs x64).
run: echo "ARCH=$(echo '${{ runner.arch }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
# Normalizing it avoids cache key mismatches.
- name: Normalize runner architecture (Windows)
- name: Normalize runner architecture (Linux/macOS)
if: runner.os == 'Windows'
if: runner.os != 'Windows'
shell: pwsh
shell: bash
run: |
run: echo "ARCH=$(echo '${{ runner.arch }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
$arch = "${{ runner.arch }}".ToLower()
echo "ARCH=$arch" | Out-File $env:GITHUB_ENV -Append
- name: Normalize runner architecture (Windows)
# Sets CACHE_OS_SUFFIX per platform
if: runner.os == 'Windows'
- name: Set cache OS suffix (Linux/macOS)
shell: pwsh
if: runner.os != 'Windows'
run: |
shell: bash
$arch = "${{ runner.arch }}".ToLower()
run: echo "CACHE_OS_SUFFIX=$ImageOS-" >> $GITHUB_ENV
echo "ARCH=$arch" | Out-File $env:GITHUB_ENV -Append
- name: Set cache OS suffix (Windows)
if: runner.os == 'Windows'
# Always define CACHE_OS_SUFFIX so it is safe to use in cache keys
shell: pwsh
- name: Set cache OS suffix
run: echo "CACHE_OS_SUFFIX=" >> $GITHUB_ENV
shell: bash
run: |
- name: Restore Go cache
echo "CACHE_OS_SUFFIX=" >> $GITHUB_ENV
id: go-cache
if [ "${{ runner.os }}" = "Linux" ]; then
uses: actions/cache/restore@v5
echo "CACHE_OS_SUFFIX=$ImageOS-" >> $GITHUB_ENV
with:
fi
path: |
${{ env.GO_MOD_CACHE }}
- name: Restore Go cache
${{ env.GO_BUILD_CACHE }}
id: go-cache
key: setup-go-${{ runner.os }}-${{ env.ARCH }}-${{ env.CACHE_OS_SUFFIX }}go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.sum') }}
uses: actions/cache/restore@v4
with:
- name: Download modules
path: |
run: go mod download
${{ env.GO_MOD_CACHE }}
${{ env.GO_BUILD_CACHE }}
- name: Build
key: setup-go-${{ runner.os }}-${{ env.ARCH }}-${{ env.CACHE_OS_SUFFIX }}go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.sum') }}
run: go build ./...
- name: Download modules
run: go mod download
- name: Build
run: go build ./...
```
```
> 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