From 4db1d4bdd6e264fbd0d33911e60095d1a6e7180f Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Tue, 20 Aug 2024 08:14:49 +0200 Subject: [PATCH 1/3] chore: add issue link --- actions/setup/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions/setup/action.yml b/actions/setup/action.yml index 9c00223e..cc91d592 100644 --- a/actions/setup/action.yml +++ b/actions/setup/action.yml @@ -41,7 +41,7 @@ runs: with: go-version: ${{ inputs.go-version }} check-latest: ${{ inputs.check-latest }} - cache: false # cache is handled by separate actions/cache step. + cache: false # cache is handled by separate actions/cache step, see https://github.com/actions/setup-go/issues/358 - name: Set up cache if: ${{ inputs.disableCache != 'true' }} From 244696ff42faf91902c9a2607a064411de4acbcd Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 16 Aug 2024 14:10:37 +0200 Subject: [PATCH 2/3] fix: cache from branch, fallback to target branch --- actions/setup/action.yml | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/actions/setup/action.yml b/actions/setup/action.yml index cc91d592..5dc77899 100644 --- a/actions/setup/action.yml +++ b/actions/setup/action.yml @@ -43,6 +43,21 @@ runs: check-latest: ${{ inputs.check-latest }} cache: false # cache is handled by separate actions/cache step, see https://github.com/actions/setup-go/issues/358 + - name: Generate cache key + id: cache-key + shell: bash + run: | + + echo "restore_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-default" >> $GITHUB_OUTPUT + + if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then + echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-default" >> $GITHUB_OUTPUT + elif [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-PR" >> $GITHUB_OUTPUT + else + echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}" >> $GITHUB_OUTPUT + fi + - name: Set up cache if: ${{ inputs.disableCache != 'true' }} uses: actions/cache@v4 @@ -53,10 +68,7 @@ runs: /home/runner/.cache/go-build /home/runner/go/pkg/mod /home/runner/go/bin - key: ${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }} + key: ${{ steps.cache-key.outputs.cache_key }} restore-keys: | - ${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}- - ${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}- - ${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}- - ${{ runner.os }}-sage-${{ github.workflow }}- - ${{ runner.os }}-sage- + ${{ steps.cache-key.outputs.restore_key }} + From 79d930d88be769db0ec7d1056caaae07de23f577 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 19 Aug 2024 14:45:16 +0200 Subject: [PATCH 3/3] refactor: simplify cache keys --- actions/setup/action.yml | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/actions/setup/action.yml b/actions/setup/action.yml index 5dc77899..bee6b790 100644 --- a/actions/setup/action.yml +++ b/actions/setup/action.yml @@ -43,21 +43,11 @@ runs: check-latest: ${{ inputs.check-latest }} cache: false # cache is handled by separate actions/cache step, see https://github.com/actions/setup-go/issues/358 - - name: Generate cache key - id: cache-key - shell: bash - run: | - - echo "restore_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-default" >> $GITHUB_OUTPUT - - if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then - echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-default" >> $GITHUB_OUTPUT - elif [[ "${{ github.event_name }}" == "pull_request" ]]; then - echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}-PR" >> $GITHUB_OUTPUT - else - echo "cache_key=${{ runner.os }}-sage-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}" >> $GITHUB_OUTPUT - fi - + # NOTE: cache key and restore key is not the same; + # We want to always re-use the cache created by master in all our PRs. We also want any PR which modifies go.sum to create a new cache for itself. + # Therefore the cache key is set to use GITHUB_REF_NAME and the restore key is set to use GITHUB_BASE_REF. + # For more details, see https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables + # NOTE: you need to build on push to master and on pull request in the repos which uses this reusable workflow. - name: Set up cache if: ${{ inputs.disableCache != 'true' }} uses: actions/cache@v4 @@ -68,7 +58,6 @@ runs: /home/runner/.cache/go-build /home/runner/go/pkg/mod /home/runner/go/bin - key: ${{ steps.cache-key.outputs.cache_key }} + key: ${{ runner.os }}-${{ github.ref_name }}-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }} restore-keys: | - ${{ steps.cache-key.outputs.restore_key }} - + ${{ runner.os }}-${{ github.base_ref }}-${{ github.workflow }}-${{ github.job }}-${{ inputs.cacheKey }}-${{ inputs.go-version }}-${{ hashFiles('**/go.sum') }}