Composite GitHub Action combining a perfect pairing of actions/setup-go with actions/cache for caching of both Golang module and build caches.
Reduce all these workflow steps:
steps:
- name: Setup Golang
uses: actions/setup-go@v5
with:
go-version: ~1.22
- name: Setup Golang caches
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-golang-
down to this:
steps:
- name: Setup Golang with cache
uses: magnetikonline/action-golang-cache@v5
with:
go-version: ~1.22
or better yet, use go-version-file
for version selection:
steps:
- name: Setup Golang with cache
uses: magnetikonline/action-golang-cache@v5
with:
go-version-file: go.mod
Action correctly saves/restores build and module cache paths for Linux, macOS and Windows runners.
An optional cache-key-suffix
input allows control of the generated cache key. This is handy where different Golang program(s) are tested/compiled across multiple workflow definitions - resulting in unique optimized build cache path contents:
steps:
- name: Cache key suffix
uses: magnetikonline/action-golang-cache@v5
with:
go-version: ~1.22
cache-key-suffix: apples
# cache key: ${{ runner.os }}-golang-apples-${{ hashFiles('**/go.sum') }}
# restore key: ${{ runner.os }}-golang-apples-
Outputs of build-cache-path
, module-cache-path
and cache-key
are provided - handy for use in subsequent workflow steps, such as with actions/cache/save
:
steps:
- name: Setup Golang with cache
id: golang-with-cache
uses: magnetikonline/action-golang-cache@v5
with:
go-version-file: go.mod
# further steps...
- name: Save Golang cache
if: always()
uses: actions/cache/save@v4
with:
path: |
${{ steps.golang-with-cache.outputs.build-cache-path }}
${{ steps.golang-with-cache.outputs.module-cache-path }}
key: ${{ steps.golang-with-cache.outputs.cache-key }}