|
29 | 29 | build: |
30 | 30 | runs-on: ubuntu-latest |
31 | 31 | steps: |
32 | | - - uses: actions/checkout@v3 |
| 32 | + - uses: actions/checkout@v4 |
33 | 33 | - uses: denoland/setup-deno@v2 |
34 | 34 | with: |
35 | 35 | deno-version: v2.x # Run with latest stable Deno. |
@@ -80,7 +80,7 @@ Note: GitHub Actions has a known |
80 | 80 | Windows-style line endings (CRLF). This may cause issues when running `deno fmt` |
81 | 81 | in a pipeline with jobs that run on `windows`. To prevent this, configure the |
82 | 82 | Actions runner to use Linux-style line endings before running the |
83 | | -`actions/checkout@v3` step: |
| 83 | +`actions/checkout@v4` step: |
84 | 84 |
|
85 | 85 | ```sh |
86 | 86 | 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 |
142 | 142 | downloaded anew. |
143 | 143 |
|
144 | 144 | 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`: |
147 | 147 |
|
148 | 148 | ```yaml |
149 | | -# Set DENO_DIR to an absolute or relative path on the runner. |
150 | | -env: |
151 | | - DENO_DIR: my_cache_directory |
152 | | -
|
153 | 149 | steps: |
154 | | - - name: Cache Deno dependencies |
155 | | - uses: actions/cache@v4 |
| 150 | + - uses: actions/checkout@v4 |
| 151 | + - uses: denoland/setup-deno@v2 |
156 | 152 | with: |
157 | | - path: ${{ env.DENO_DIR }} |
158 | | - key: my_cache_key |
| 153 | + cache: true |
159 | 154 | ``` |
160 | 155 |
|
161 | 156 | At first, when this workflow runs the cache is still empty and commands like |
162 | 157 | `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. |
183 | 160 |
|
184 | 161 | To demonstrate, let's say you have a project that uses the logger from |
185 | 162 | [`@std/log`](https://jsr.io/@std/log): |
186 | 163 |
|
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 | +} |
189 | 170 | ``` |
190 | 171 |
|
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: |
193 | 174 |
|
194 | 175 | ```console |
195 | | -deno install --reload --lock=deno.lock --frozen=false --entrypoint deps.ts |
| 176 | +deno install --reload --frozen=false |
196 | 177 | ``` |
197 | 178 |
|
198 | 179 | 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. |
201 | 182 |
|
202 | | -#### Clearing the cache |
| 183 | +By default, the cache is automatically keyed by: |
203 | 184 |
|
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. |
210 | 193 |
|
211 | 194 | ```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') }} |
213 | 200 | ``` |
0 commit comments