Skip to content

Commit

Permalink
feat: optimize Go Mod cache (#15007)
Browse files Browse the repository at this point in the history
* fix: go mod caching

* fix: only cache modules
  • Loading branch information
erikburt authored Oct 29, 2024
1 parent 7fc51b8 commit 0bded6d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
26 changes: 25 additions & 1 deletion .github/actions/setup-go/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ inputs:
go-module-file:
description: Set where the go module file is located at
default: "go.sum"
restore-module-cache-only:
description: |
Only restore the module cache, don't automatically update it.
Leave the updating to go-mod-cache.yml.
default: "true"

runs:
using: composite
Expand Down Expand Up @@ -40,14 +45,33 @@ runs:
shell: bash
run: echo "path=./${{ inputs.go-module-file }}" >> $GITHUB_OUTPUT

# By default, restore the cache only.
# If multiple jobs call actions/cache, then only one will get priority to create upon a cache miss.
# We will only restore the cache by default (by calling actions/cache/restore) and let the
# `go-mod-cache.yml` workflow handle the creation.
- uses: actions/cache/[email protected]
if: ${{ inputs.restore-module-cache-only == 'true' }}
name: Cache Go Modules
with:
path: |
${{ steps.go-cache-dir.outputs.gomodcache }}
# The lifetime of go modules is much higher than the build outputs, so we increase cache efficiency
# here by not having the primary key contain the branch name
key: ${{ runner.os }}-gomod-${{ inputs.cache-version }}-${{ hashFiles(steps.go-module-path.outputs.path) }}
restore-keys: |
${{ runner.os }}-gomod-${{ inputs.cache-version }}-
# If this is called, then it will create the cache entry upon a cache miss.
# The cache is created after a cache miss, and after job completes successfully.
- uses: actions/[email protected]
if: ${{ inputs.restore-module-cache-only != 'true' }}
name: Cache Go Modules
with:
path: |
${{ steps.go-cache-dir.outputs.gomodcache }}
# The lifetime of go modules is much higher than the build outputs, so we increase cache efficiency
# here by not having the primary key contain the branch name
key: ${{ runner.os }}-gomod-${{ inputs.cache-version }}-${{ hashFiles(steps.go-module-path.output.path) }}
key: ${{ runner.os }}-gomod-${{ inputs.cache-version }}-${{ hashFiles(steps.go-module-path.outputs.path) }}
restore-keys: |
${{ runner.os }}-gomod-${{ inputs.cache-version }}-
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/ci-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ jobs:
go-directory: core/scripts
go-version-file: core/scripts/go.mod
go-module-file: core/scripts/go.sum
gc-basic-auth: ${{ secrets.GRAFANA_INTERNAL_BASIC_AUTH }}
gc-host: ${{ secrets.GRAFANA_INTERNAL_HOST }}
gc-org-id: ${{ secrets.GRAFANA_INTERNAL_TENANT_ID }}

test-scripts:
runs-on: ubuntu-latest
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/go-mod-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Go Module Cache

# This workflow is responsible for updating the Go Module Cache.
# It will maintain the cache: linux-gomod-1-<hash>
# All other workflows should only restore this cache.

# This workflow is useful because it will:
# 1. Create the cache if it doesn't exist
# - This can be a problem when multiple jobs load the same cache.
# Only one will get priority to create the cache.
# 2. Should not fail, therefore creating a cache
# - When a Job errors/fails it will not upload a new cache.
# So when test/build jobs are responsible for creating the new cache,
# they can fail causing cache misses on subsequent runs. Even though
# the dependencies haven't changed.

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
- develop
pull_request:

jobs:
go-cache:
name: Go Cache
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/[email protected]

- name: Setup Go
uses: ./.github/actions/setup-go
with:
only-modules: "true"
restore-module-cache-only: "false"

- name: Install Dependencies
shell: bash
run: go mod download all

0 comments on commit 0bded6d

Please sign in to comment.