Skip to content

chore: cache terraform providers between CI test runs #17373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/actions/test-cache/download/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Download Test Cache"
description: |
Downloads the test cache and outputs today's cache key.
A PR job can use a cache if it was created by its base branch, its current
branch, or the default branch.
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
outputs:
cache-key:
description: "Today's cache key"
value: ${{ steps.vars.outputs.cache-key }}
inputs:
key-prefix:
description: "Prefix for the cache key"
required: true
cache-path:
description: "Path to the cache directory"
required: true
# This path is defined in testutil/cache.go
default: "~/.cache/coderv2-test"
runs:
using: "composite"
steps:
- name: Get date values and cache key
id: vars
shell: bash
run: |
export YEAR_MONTH=$(date +'%Y-%m')
export PREV_YEAR_MONTH=$(date -d 'last month' +'%Y-%m')
export DAY=$(date +'%d')
echo "year-month=$YEAR_MONTH" >> $GITHUB_OUTPUT
echo "prev-year-month=$PREV_YEAR_MONTH" >> $GITHUB_OUTPUT
echo "cache-key=${{ inputs.key-prefix }}-${YEAR_MONTH}-${DAY}" >> $GITHUB_OUTPUT

# TODO: As a cost optimization, we could remove caches that are older than
# a day or two. By default, depot keeps caches for 14 days, which isn't
# necessary for the test cache.
# https://depot.dev/docs/github-actions/overview#cache-retention-policy
- name: Download test cache
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
with:
path: ${{ inputs.cache-path }}
key: ${{ steps.vars.outputs.cache-key }}
# > If there are multiple partial matches for a restore key, the action returns the most recently created cache.
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
# The second restore key allows non-main branches to use the cache from the previous month.
# This prevents PRs from rebuilding the cache on the first day of the month.
# It also makes sure that once a month, the cache is fully reset.
restore-keys: |
${{ inputs.key-prefix }}-${{ steps.vars.outputs.year-month }}-
${{ github.ref != 'refs/heads/main' && format('{0}-{1}-', inputs.key-prefix, steps.vars.outputs.prev-year-month) || '' }}
20 changes: 20 additions & 0 deletions .github/actions/test-cache/upload/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Upload Test Cache"
description: Uploads the test cache. Only works on the main branch.
inputs:
cache-key:
description: "Cache key"
required: true
cache-path:
description: "Path to the cache directory"
required: true
# This path is defined in testutil/cache.go
default: "~/.cache/coderv2-test"
runs:
using: "composite"
steps:
- name: Upload test cache
if: ${{ github.ref == 'refs/heads/main' }}
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
with:
path: ${{ inputs.cache-path }}
key: ${{ inputs.cache-key }}
55 changes: 55 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ jobs:
- name: Setup Terraform
uses: ./.github/actions/setup-tf

- name: Download Test Cache
id: download-cache
uses: ./.github/actions/test-cache/download
with:
key-prefix: test-go-${{ runner.os }}-${{ runner.arch }}

- name: Test with Mock Database
id: test
shell: bash
Expand All @@ -370,6 +376,11 @@ jobs:
gotestsum --junitfile="gotests.xml" --jsonfile="gotests.json" \
--packages="./..." -- $PARALLEL_FLAG -short -failfast

- name: Upload Test Cache
uses: ./.github/actions/test-cache/upload
with:
cache-key: ${{ steps.download-cache.outputs.cache-key }}

- name: Upload test stats to Datadog
timeout-minutes: 1
continue-on-error: true
Expand Down Expand Up @@ -467,6 +478,12 @@ jobs:
if: runner.os == 'Windows'
uses: ./.github/actions/setup-imdisk

- name: Download Test Cache
id: download-cache
uses: ./.github/actions/test-cache/download
with:
key-prefix: test-go-pg-${{ runner.os }}-${{ runner.arch }}

- name: Test with PostgreSQL Database
env:
POSTGRES_VERSION: "13"
Expand All @@ -481,6 +498,11 @@ jobs:

make test-postgres

- name: Upload Test Cache
uses: ./.github/actions/test-cache/upload
with:
cache-key: ${{ steps.download-cache.outputs.cache-key }}

- name: Upload test stats to Datadog
timeout-minutes: 1
continue-on-error: true
Expand Down Expand Up @@ -519,13 +541,24 @@ jobs:
- name: Setup Terraform
uses: ./.github/actions/setup-tf

- name: Download Test Cache
id: download-cache
uses: ./.github/actions/test-cache/download
with:
key-prefix: test-go-pg-16-${{ runner.os }}-${{ runner.arch }}

- name: Test with PostgreSQL Database
env:
POSTGRES_VERSION: "16"
TS_DEBUG_DISCO: "true"
run: |
make test-postgres

- name: Upload Test Cache
uses: ./.github/actions/test-cache/upload
with:
cache-key: ${{ steps.download-cache.outputs.cache-key }}

- name: Upload test stats to Datadog
timeout-minutes: 1
continue-on-error: true
Expand Down Expand Up @@ -556,6 +589,12 @@ jobs:
- name: Setup Terraform
uses: ./.github/actions/setup-tf

- name: Download Test Cache
id: download-cache
uses: ./.github/actions/test-cache/download
with:
key-prefix: test-go-race-${{ runner.os }}-${{ runner.arch }}

# We run race tests with reduced parallelism because they use more CPU and we were finding
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
# short timeouts are used.
Expand All @@ -564,6 +603,11 @@ jobs:
run: |
gotestsum --junitfile="gotests.xml" -- -race -parallel 4 -p 4 ./...

- name: Upload Test Cache
uses: ./.github/actions/test-cache/upload
with:
cache-key: ${{ steps.download-cache.outputs.cache-key }}

- name: Upload test stats to Datadog
timeout-minutes: 1
continue-on-error: true
Expand Down Expand Up @@ -594,6 +638,12 @@ jobs:
- name: Setup Terraform
uses: ./.github/actions/setup-tf

- name: Download Test Cache
id: download-cache
uses: ./.github/actions/test-cache/download
with:
key-prefix: test-go-race-pg-${{ runner.os }}-${{ runner.arch }}

# We run race tests with reduced parallelism because they use more CPU and we were finding
# instances where tests appear to hang for multiple seconds, resulting in flaky tests when
# short timeouts are used.
Expand All @@ -605,6 +655,11 @@ jobs:
make test-postgres-docker
DB=ci gotestsum --junitfile="gotests.xml" -- -race -parallel 4 -p 4 ./...

- name: Upload Test Cache
uses: ./.github/actions/test-cache/upload
with:
cache-key: ${{ steps.download-cache.outputs.cache-key }}

- name: Upload test stats to Datadog
timeout-minutes: 1
continue-on-error: true
Expand Down
8 changes: 6 additions & 2 deletions provisioner/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type executor struct {
mut *sync.Mutex
binaryPath string
// cachePath and workdir must not be used by multiple processes at once.
cachePath string
workdir string
cachePath string
cliConfigPath string
workdir string
// used to capture execution times at various stages
timings *timingAggregator
}
Expand All @@ -50,6 +51,9 @@ func (e *executor) basicEnv() []string {
if e.cachePath != "" && runtime.GOOS == "linux" {
env = append(env, "TF_PLUGIN_CACHE_DIR="+e.cachePath)
}
if e.cliConfigPath != "" {
env = append(env, "TF_CLI_CONFIG_FILE="+e.cliConfigPath)
}
return env
}

Expand Down
Loading
Loading