Skip to content

Commit 1c6e5de

Browse files
Fix CI caching recommendation (#1698)
Co-authored-by: Phil Hawksworth <phil@deno.com>
1 parent 0418470 commit 1c6e5de

File tree

8 files changed

+42
-52
lines changed

8 files changed

+42
-52
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: denoland/setup-deno@v2
1414
with:
15+
cache: true
1516
deno-version: "2.3.1"
1617

1718
- run: deno fmt --check

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919

2020
- name: Set up Deno
2121
uses: denoland/setup-deno@v2
22+
with:
23+
cache: true
2224

2325
- name: "Reference: install"
2426
working-directory: "reference_gen"

.github/workflows/update_versions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
steps:
1313
- name: Clone repository
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515
with:
1616
token: ${{ secrets.DENOBOT_PAT }}
1717

examples/tutorials/aws_lightsail.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ jobs:
212212
runs-on: ubuntu-latest
213213
steps:
214214
- name: Checkout main
215-
uses: actions/checkout@v2
215+
uses: actions/checkout@v4
216216
217217
- name: Install Utilities
218218
run: |

examples/tutorials/digital_ocean.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ jobs:
239239
runs-on: ubuntu-latest
240240
steps:
241241
- name: Checkout main
242-
uses: actions/checkout@v2
242+
uses: actions/checkout@v4
243243
244244
- name: Set $TAG from shortened sha
245245
run: echo "TAG=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV

examples/tutorials/google_cloud_run.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ jobs:
208208
runs-on: ubuntu-latest
209209
steps:
210210
- name: Checkout
211-
uses: actions/checkout@v3
211+
uses: actions/checkout@v4
212212
213213
- name: Google Auth
214214
id: auth

runtime/fundamentals/linting_and_formatting.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ jobs:
9696
build:
9797
runs-on: ubuntu-latest
9898
steps:
99-
- uses: actions/checkout@v3
99+
- uses: actions/checkout@v4
100100
- uses: denoland/setup-deno@v2
101101
with:
102102
deno-version: v2.x

runtime/reference/continuous_integration.md

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
build:
3030
runs-on: ubuntu-latest
3131
steps:
32-
- uses: actions/checkout@v3
32+
- uses: actions/checkout@v4
3333
- uses: denoland/setup-deno@v2
3434
with:
3535
deno-version: v2.x # Run with latest stable Deno.
@@ -80,7 +80,7 @@ Note: GitHub Actions has a known
8080
Windows-style line endings (CRLF). This may cause issues when running `deno fmt`
8181
in a pipeline with jobs that run on `windows`. To prevent this, configure the
8282
Actions runner to use Linux-style line endings before running the
83-
`actions/checkout@v3` step:
83+
`actions/checkout@v4` step:
8484

8585
```sh
8686
git config --system core.autocrlf false
@@ -142,72 +142,59 @@ speed things up is to cache dependencies so that they do not need to be
142142
downloaded anew.
143143

144144
Deno stores dependencies locally in a cache directory. In a pipeline the cache
145-
can be preserved between workflows by setting the `DENO_DIR` environment
146-
variable and adding a caching step to the workflow:
145+
can be preserved between workflows by turning on the `cache: true` option on
146+
`denoland/setup-deno`:
147147

148148
```yaml
149-
# Set DENO_DIR to an absolute or relative path on the runner.
150-
env:
151-
DENO_DIR: my_cache_directory
152-
153149
steps:
154-
- name: Cache Deno dependencies
155-
uses: actions/cache@v4
150+
- uses: actions/checkout@v4
151+
- uses: denoland/setup-deno@v2
156152
with:
157-
path: ${{ env.DENO_DIR }}
158-
key: my_cache_key
153+
cache: true
159154
```
160155

161156
At first, when this workflow runs the cache is still empty and commands like
162157
`deno test` will still have to download dependencies, but when the job succeeds
163-
the contents of `DENO_DIR` are saved and any subsequent runs can restore them
164-
from cache instead of re-downloading.
165-
166-
There is still an issue in the workflow above: at the moment the name of the
167-
cache key is hardcoded to `my_cache_key`, which is going to restore the same
168-
cache every time, even if one or more dependencies are updated. This can lead to
169-
older versions being used in the pipeline even though you have updated some
170-
dependencies. The solution is to generate a different key each time the cache
171-
needs to be updated, which can be achieved by using a lockfile and by using the
172-
`hashFiles` function provided by GitHub Actions:
173-
174-
```yaml
175-
key: ${{ hashFiles('deno.lock') }}
176-
```
177-
178-
To make this work you will also need a have a lockfile in your Deno project,
179-
which is discussed in detail
180-
[here](/runtime/fundamentals/modules/#integrity-checking-and-lock-files). Now,
181-
if the contents of `deno.lock` are changed, a new cache will be made and used in
182-
subsequent pipeline runs thereafter.
158+
the contents of cached dependencies are saved and any subsequent runs can
159+
restore them from cache instead of re-downloading.
183160

184161
To demonstrate, let's say you have a project that uses the logger from
185162
[`@std/log`](https://jsr.io/@std/log):
186163

187-
```ts
188-
import * as log from "jsr:@std/log@0.224.5";
164+
```json, title="deno.json"
165+
{
166+
"imports": {
167+
"@std/log": "jsr:@std/log@0.224.5"
168+
}
169+
}
189170
```
190171

191-
In order to increment this version, you can update the `import` statement and
192-
then reload the cache and update the lockfile locally:
172+
In order to increment this version, you can update the dependency and then
173+
reload the cache and update the lockfile locally:
193174

194175
```console
195-
deno install --reload --lock=deno.lock --frozen=false --entrypoint deps.ts
176+
deno install --reload --frozen=false
196177
```
197178

198179
You should see changes in the lockfile's contents after running this. When this
199-
is committed and run through the pipeline, you should then see the `hashFiles`
200-
function saving a new cache and using it in any runs that follow.
180+
is committed and run through the pipeline, you should then see a new cache and
181+
using it in any runs that follow.
201182

202-
#### Clearing the cache
183+
By default, the cache is automatically keyed by:
203184

204-
Occasionally you may run into a cache that has been corrupted or malformed,
205-
which can happen for various reasons. It is possible to clear a cache from the
206-
GitHub Actions UI, or you can simply change the name of the cache key. A
207-
practical way of doing so without having to forcefully change your lockfile is
208-
to add a variable to the cache key name, which can be stored as a GitHub secret
209-
and can be changed if a new cache is needed:
185+
- the github
186+
[job_id](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_id)
187+
- the runner os and architecture
188+
- a hash of the `deno.lock` files in the project
189+
190+
It is possible to customize the default hash
191+
(`${{ hashFiles('**/deno.lock') }}`) used as part of the cache key via the
192+
`cache-hash` input.
210193

211194
```yaml
212-
key: ${{ secrets.CACHE_VERSION }}-${{ hashFiles('deno.lock') }}
195+
- uses: denoland/setup-deno@v2
196+
with:
197+
# setting `cache-hash` implies `cache: true` and will replace
198+
# the default cache-hash of `${{ hashFiles('**/deno.lock') }}`
199+
cache-hash: ${{ hashFiles('**/deno.json') }}
213200
```

0 commit comments

Comments
 (0)