-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimize Go Mod cache (#15007)
* fix: go mod caching * fix: only cache modules
- Loading branch information
Showing
3 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 }}- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |