Skip to content

Commit 2ab657c

Browse files
committed
add cache to CI
1 parent f11fea6 commit 2ab657c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: "Setup Test Cache"
2+
description: |
3+
Downloads the test cache and, if needed, uploads a new cache after the job is complete.
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+
inputs:
8+
key-prefix:
9+
description: "Prefix for the cache key"
10+
required: true
11+
cache-path:
12+
description: "Path to the cache directory"
13+
required: true
14+
# This path is defined in testutil/cache.go
15+
default: "~/.cache/coderv2-test"
16+
runs:
17+
using: "composite"
18+
steps:
19+
- name: Get date values
20+
id: dates
21+
shell: bash
22+
run: |
23+
echo "year-month=$(date +'%Y-%m')" >> $GITHUB_OUTPUT
24+
echo "prev-year-month=$(date -d 'last month' +'%Y-%m')" >> $GITHUB_OUTPUT
25+
echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
26+
27+
# Using this particular key/restore-keys combination ensures that:
28+
# 1. The cache is updated at most once a day for a given key prefix.
29+
# 2. The cache is reset once a month for a given key prefix.
30+
#
31+
# TODO: As a cost optimization, we could remove caches that are older than
32+
# a day or two. By default, depot keeps caches for 14 days, which isn't
33+
# necessary for the test cache.
34+
# https://depot.dev/docs/github-actions/overview#cache-retention-policy
35+
- name: Download and optionally upload test cache
36+
# This is a fork of actions/cache that only saves the cache if the current
37+
# job is running on the main branch.
38+
# Without it, PRs would create one-use caches that would linger until
39+
# expiration and we'd be charged for them. I evaluated a couple of options
40+
# for limiting the cache to the main branch, and forking was the simplest.
41+
uses: coder/actions-cache@283c1b470b0f7f7c6c09e0449f4a261ca04ab249
42+
with:
43+
path: ${{ inputs.cache-path }}
44+
# The key doesn't need to include an OS name. The action already takes
45+
# that into account: https://github.com/actions/cache/tree/5a3ec84eff668545956fd18022155c47e93e2684?tab=readme-ov-file#cache-version
46+
# Cache entries are immutable. If an entry under the key already exists,
47+
# it will not be overwritten.
48+
key: ${{ inputs.key-prefix }}-${{ steps.dates.outputs.year-month }}-${{ steps.dates.outputs.day }}
49+
# > If there are multiple partial matches for a restore key, the action returns the most recently created cache.
50+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
51+
# The second restore key allows non-main branches to use the cache from the previous month.
52+
# This prevents PRs from rebuilding the cache on the first day of the month.
53+
restore-keys: |
54+
${{ inputs.key-prefix }}-${{ steps.dates.outputs.year-month }}-
55+
${{ github.ref != 'refs/heads/main' && format('{0}-{1}-', inputs.key-prefix, steps.dates.outputs.prev-year-month) || '' }}

.github/workflows/ci.yaml

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

349+
- name: Use Test Cache
350+
uses: ./.github/actions/setup-test-cache
351+
with:
352+
key-prefix: test-go
353+
349354
- name: Test with Mock Database
350355
id: test
351356
shell: bash
@@ -467,6 +472,11 @@ jobs:
467472
if: runner.os == 'Windows'
468473
uses: ./.github/actions/setup-imdisk
469474

475+
- name: Use Test Cache
476+
uses: ./.github/actions/setup-test-cache
477+
with:
478+
key-prefix: test-go-pg
479+
470480
- name: Test with PostgreSQL Database
471481
env:
472482
POSTGRES_VERSION: "13"
@@ -519,6 +529,11 @@ jobs:
519529
- name: Setup Terraform
520530
uses: ./.github/actions/setup-tf
521531

532+
- name: Use Test Cache
533+
uses: ./.github/actions/setup-test-cache
534+
with:
535+
key-prefix: test-go-pg-16-tmp1
536+
522537
- name: Test with PostgreSQL Database
523538
env:
524539
POSTGRES_VERSION: "16"
@@ -556,6 +571,11 @@ jobs:
556571
- name: Setup Terraform
557572
uses: ./.github/actions/setup-tf
558573

574+
- name: Use Test Cache
575+
uses: ./.github/actions/setup-test-cache
576+
with:
577+
key-prefix: test-go-race
578+
559579
# We run race tests with reduced parallelism because they use more CPU and we were finding
560580
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
561581
# short timeouts are used.
@@ -594,6 +614,11 @@ jobs:
594614
- name: Setup Terraform
595615
uses: ./.github/actions/setup-tf
596616

617+
- name: Use Test Cache
618+
uses: ./.github/actions/setup-test-cache
619+
with:
620+
key-prefix: test-go-race-pg
621+
597622
# We run race tests with reduced parallelism because they use more CPU and we were finding
598623
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
599624
# short timeouts are used.

0 commit comments

Comments
 (0)