Skip to content

Commit d56aaa7

Browse files
committed
address feedback: use separate save and restore steps, use os-and-arch-specific key prefixes
1 parent 2236dd4 commit d56aaa7

File tree

4 files changed

+115
-70
lines changed

4 files changed

+115
-70
lines changed

.github/actions/setup-test-cache/action.yaml

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: "Download Test Cache"
2+
description: |
3+
Downloads the test cache and outputs today's cache key.
4+
A PR job can use a cache if it was created by its base branch, its current
5+
branch, or the default branch.
6+
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
7+
outputs:
8+
cache-key:
9+
description: "Today's cache key"
10+
value: ${{ steps.vars.outputs.cache-key }}
11+
inputs:
12+
key-prefix:
13+
description: "Prefix for the cache key"
14+
required: true
15+
cache-path:
16+
description: "Path to the cache directory"
17+
required: true
18+
# This path is defined in testutil/cache.go
19+
default: "~/.cache/coderv2-test"
20+
runs:
21+
using: "composite"
22+
steps:
23+
- name: Get date values and cache key
24+
id: vars
25+
shell: bash
26+
run: |
27+
export YEAR_MONTH=$(date +'%Y-%m')
28+
export PREV_YEAR_MONTH=$(date -d 'last month' +'%Y-%m')
29+
export DAY=$(date +'%d')
30+
echo "year-month=$YEAR_MONTH" >> $GITHUB_OUTPUT
31+
echo "prev-year-month=$PREV_YEAR_MONTH" >> $GITHUB_OUTPUT
32+
echo "cache-key=${{ inputs.key-prefix }}-${YEAR_MONTH}-${DAY}" >> $GITHUB_OUTPUT
33+
34+
# TODO: As a cost optimization, we could remove caches that are older than
35+
# a day or two. By default, depot keeps caches for 14 days, which isn't
36+
# necessary for the test cache.
37+
# https://depot.dev/docs/github-actions/overview#cache-retention-policy
38+
- name: Download test cache
39+
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
40+
with:
41+
path: ${{ inputs.cache-path }}
42+
key: ${{ steps.vars.outputs.cache-key }}
43+
# > If there are multiple partial matches for a restore key, the action returns the most recently created cache.
44+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
45+
# The second restore key allows non-main branches to use the cache from the previous month.
46+
# This prevents PRs from rebuilding the cache on the first day of the month.
47+
# It also makes sure that once a month, the cache is fully reset.
48+
restore-keys: |
49+
${{ inputs.key-prefix }}-${{ steps.vars.outputs.year-month }}-
50+
${{ github.ref != 'refs/heads/main' && format('{0}-{1}-', inputs.key-prefix, steps.vars.outputs.prev-year-month) || '' }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Upload Test Cache"
2+
description: Uploads the test cache. Only works on the main branch.
3+
inputs:
4+
cache-key:
5+
description: "Cache key"
6+
required: true
7+
cache-path:
8+
description: "Path to the cache directory"
9+
required: true
10+
# This path is defined in testutil/cache.go
11+
default: "~/.cache/coderv2-test"
12+
runs:
13+
using: "composite"
14+
steps:
15+
- name: Upload test cache
16+
if: ${{ github.ref == 'refs/heads/main' }}
17+
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
18+
with:
19+
path: ${{ inputs.cache-path }}
20+
key: ${{ inputs.cache-key }}

.github/workflows/ci.yaml

+45-15
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,11 @@ jobs:
346346
- name: Setup Terraform
347347
uses: ./.github/actions/setup-tf
348348

349-
- name: Setup Test Cache
350-
uses: ./.github/actions/setup-test-cache
349+
- name: Download Test Cache
350+
id: download-cache
351+
uses: ./.github/actions/test-cache/download
351352
with:
352-
key-prefix: test-go
353+
key-prefix: test-go-${{ runner.os }}-${{ runner.arch }}
353354

354355
- name: Test with Mock Database
355356
id: test
@@ -375,6 +376,11 @@ jobs:
375376
gotestsum --junitfile="gotests.xml" --jsonfile="gotests.json" \
376377
--packages="./..." -- $PARALLEL_FLAG -short -failfast
377378
379+
- name: Upload Test Cache
380+
uses: ./.github/actions/test-cache/upload
381+
with:
382+
cache-key: ${{ steps.download-cache.outputs.cache-key }}
383+
378384
- name: Upload test stats to Datadog
379385
timeout-minutes: 1
380386
continue-on-error: true
@@ -472,10 +478,11 @@ jobs:
472478
if: runner.os == 'Windows'
473479
uses: ./.github/actions/setup-imdisk
474480

475-
- name: Setup Test Cache
476-
uses: ./.github/actions/setup-test-cache
481+
- name: Download Test Cache
482+
id: download-cache
483+
uses: ./.github/actions/test-cache/download
477484
with:
478-
key-prefix: test-go-pg
485+
key-prefix: test-go-pg-${{ runner.os }}-${{ runner.arch }}
479486

480487
- name: Test with PostgreSQL Database
481488
env:
@@ -491,6 +498,11 @@ jobs:
491498
492499
make test-postgres
493500
501+
- name: Upload Test Cache
502+
uses: ./.github/actions/test-cache/upload
503+
with:
504+
cache-key: ${{ steps.download-cache.outputs.cache-key }}
505+
494506
- name: Upload test stats to Datadog
495507
timeout-minutes: 1
496508
continue-on-error: true
@@ -529,10 +541,11 @@ jobs:
529541
- name: Setup Terraform
530542
uses: ./.github/actions/setup-tf
531543

532-
- name: Setup Test Cache
533-
uses: ./.github/actions/setup-test-cache
544+
- name: Download Test Cache
545+
id: download-cache
546+
uses: ./.github/actions/test-cache/download
534547
with:
535-
key-prefix: test-go-pg-16
548+
key-prefix: test-go-pg-16-${{ runner.os }}-${{ runner.arch }}
536549

537550
- name: Test with PostgreSQL Database
538551
env:
@@ -541,6 +554,11 @@ jobs:
541554
run: |
542555
make test-postgres
543556
557+
- name: Upload Test Cache
558+
uses: ./.github/actions/test-cache/upload
559+
with:
560+
cache-key: ${{ steps.download-cache.outputs.cache-key }}
561+
544562
- name: Upload test stats to Datadog
545563
timeout-minutes: 1
546564
continue-on-error: true
@@ -571,10 +589,11 @@ jobs:
571589
- name: Setup Terraform
572590
uses: ./.github/actions/setup-tf
573591

574-
- name: Setup Test Cache
575-
uses: ./.github/actions/setup-test-cache
592+
- name: Download Test Cache
593+
id: download-cache
594+
uses: ./.github/actions/test-cache/download
576595
with:
577-
key-prefix: test-go-race
596+
key-prefix: test-go-race-${{ runner.os }}-${{ runner.arch }}
578597

579598
# We run race tests with reduced parallelism because they use more CPU and we were finding
580599
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
@@ -584,6 +603,11 @@ jobs:
584603
run: |
585604
gotestsum --junitfile="gotests.xml" -- -race -parallel 4 -p 4 ./...
586605
606+
- name: Upload Test Cache
607+
uses: ./.github/actions/test-cache/upload
608+
with:
609+
cache-key: ${{ steps.download-cache.outputs.cache-key }}
610+
587611
- name: Upload test stats to Datadog
588612
timeout-minutes: 1
589613
continue-on-error: true
@@ -614,10 +638,11 @@ jobs:
614638
- name: Setup Terraform
615639
uses: ./.github/actions/setup-tf
616640

617-
- name: Setup Test Cache
618-
uses: ./.github/actions/setup-test-cache
641+
- name: Download Test Cache
642+
id: download-cache
643+
uses: ./.github/actions/test-cache/download
619644
with:
620-
key-prefix: test-go-race-pg
645+
key-prefix: test-go-race-pg-${{ runner.os }}-${{ runner.arch }}
621646

622647
# We run race tests with reduced parallelism because they use more CPU and we were finding
623648
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
@@ -630,6 +655,11 @@ jobs:
630655
make test-postgres-docker
631656
DB=ci gotestsum --junitfile="gotests.xml" -- -race -parallel 4 -p 4 ./...
632657
658+
- name: Upload Test Cache
659+
uses: ./.github/actions/test-cache/upload
660+
with:
661+
cache-key: ${{ steps.download-cache.outputs.cache-key }}
662+
633663
- name: Upload test stats to Datadog
634664
timeout-minutes: 1
635665
continue-on-error: true

0 commit comments

Comments
 (0)