Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
play

GitHub Action

Golang with cache

v2

Golang with cache

play

Golang with cache

Setup requested Golang version with managed module and build caching

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Golang with cache

uses: magnetikonline/action-golang-cache@v2

Learn more about this action in magnetikonline/action-golang-cache

Choose a version

Action Golang with cache

Composite GitHub Action which combines the perfect pairing of actions/setup-go with actions/cache for the caching of both the Golang module and build caches.

nuts and gum

Reducing all these workflow steps:

steps:
  - name: Setup Golang
    uses: actions/setup-go@v3
    with:
      go-version: ~1.17
  - name: Setup Golang caches
    uses: actions/cache@v3
    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@v2
    with:
      go-version: ~1.17

Action correctly sets the build and module cache paths for Linux, macOS and Windows runners as per the Golang cache examples.

In addition an optional cache-key-suffix input can be used to control the generated cache key - handy where a Golang program is compiled multiple times for different binaries across multiple workflow definitions - resulting in different optimized build cache path contents:

steps:
  - name: Testing action
    uses: magnetikonline/action-golang-cache@v2
    with:
      go-version: ~1.17
      cache-key-suffix: -apples

# cache key:   ${{ runner.os }}-golang-apples-${{ hashFiles('**/go.sum') }}
# restore key: ${{ runner.os }}-golang-apples-