|
| 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) || '' }} |
0 commit comments