https://github.com/magnetikonline/action-golang-cache
GitHub Action bringing together actions/setup-go and actions/cache.
https://github.com/magnetikonline/action-golang-cache
cache github-actions golang
Last synced: about 1 year ago
JSON representation
GitHub Action bringing together actions/setup-go and actions/cache.
- Host: GitHub
- URL: https://github.com/magnetikonline/action-golang-cache
- Owner: magnetikonline
- License: mit
- Created: 2021-09-26T06:25:29.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-07T22:52:32.000Z (over 2 years ago)
- Last Synced: 2025-04-22T06:10:46.599Z (over 1 year ago)
- Topics: cache, github-actions, golang
- Homepage:
- Size: 12.7 KB
- Stars: 36
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ccamel - magnetikonline/action-golang-cache - GitHub Action bringing together actions/setup-go and actions/cache. (Misc)
README
# Action Golang with cache
Composite GitHub Action combining a perfect pairing of [actions/setup-go](https://github.com/actions/setup-go) with [actions/cache](https://github.com/actions/cache) for caching of both Golang module and build caches.

- [Usage](#usage)
- [Setting unique cache keys](#setting-unique-cache-keys)
- [Action outputs](#action-outputs)
## Usage
Reduce all these workflow steps:
```yaml
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:
```yaml
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:
```yaml
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.
## Setting unique cache keys
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:
```yaml
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-
```
## Action outputs
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`:
```yaml
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 }}
```