Skip to content
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

feat: add built-in caching via inputs #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

csvn
Copy link

@csvn csvn commented Nov 25, 2024

This PR adds support for built-in caching to denoland/setup-deno. I tried to take inspiration from how actions/setup-node did things, but the code here can be way simpler because we're only supporting downloads by Deno and the default cache location or DENO_DIR.

- uses: denoland/setup-deno@v2
  with:
    cache: true
    cache-hash: ${{ hashFiles('**/deno.lock') }}

TODO's

There is only one thing left to do before this PR is ready. And that's to either install @actions/cache and it's dependencies and push the changes. Though @actions/cache has a large set of dependencies, which is a shame... I'm not sure if just installing them as-is is a good idea, or if the code here now requires a bundling step or not.

I thought it best to open this PR first to see if the Deno team has a preference here. Since I am using Windows, installing and pushing from my machine would make a lot of inconsistent changes with current /node_modules folder. It's probably also better if checked in /node_modules are updated by a Deno team member.

  • Run npm install and commit+push changes, or introduce a build step?

Cache keys

  • Cache restoreKey:
    • deno-cache-${env.RUNNER_OS}-${env.RUNNER_ARCH}
  • Cache save key:
    • ${restoreKey}-${env.GITHUB_JOB}-${input.cache-hash}

One difference compared to the existing setup-node action, is that I included GITHUB_JOB in the cache key. This means it's possible to restore a cache from another job, but each job will have it's own cache key.

The benefit of including the job in the cache key, is we only download necessary files. For instance, test may download @std/testing, while other jobs do not. We also avoid corrupted caches, if e.g. a build job installs all dependencies except testing dependencies, then the cache will miss files needed by a test job, even while using the same cache key.

This is no issue for other package managers in NodeJS, which installs all packages. But with Deno, it's possible to install packages/files automatically while running a program. The user of this action can solve that themselves though, by adding to the cache-hash input (e.g. cache-hash: test-${{ hashFiles(...) }}).

  • Pros
    • Should "just work" as expected when using Deno™️
    • The user of the action only needs to specify cache-hash input for more granular caches
    • Avoids footgun where build/test jobs have the same cache key, thus keeps downloading dependencies for one or the other
  • Cons
    • Creates more caches in total (every job will have it's own cache)
    • Cannot opt-out of including the job in the cache key

Usage comparison

Given the requirements below, here's before/after compared to when being able to use the setup-deno action with changes in this PR:

  • Caches DENO_DIR files
  • Cache dependencies on job failures
  • Uses restore-keys for the following benefits:
    • Can use cache from other jobs
    • Can re-use cache when lock-file changes
# Now
jobs:
  test:
    runs-on: ubuntu-latest
    env:
      DENO_DIR: ${{ github.workspace }}/.deno
    steps:
      - uses: actions/checkout@v4
      - uses: denoland/setup-deno@v2
      - uses: actions/cache/restore@v4
        id: cache
        with:
          path: ${{ env.DENO_DIR }}
          key: deno-cache-${{ runner.os }}-test-${{ hashFiles('deno.lock') }}
          restore-keys: deno-cache-${{ runner.os }}
      - run: deno test
      - uses: actions/cache/save@v4
        if: always() && steps.cache.outputs.cache-hit != 'true'
        with:
          path: ${{ env.DENO_DIR }}
          key: ${{ steps.cache.outputs.cache-primary-key }}
# This PR
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: denoland/setup-deno@v2
        with:
          cache: true
          cache-hash: ${{ hashFiles('deno.lock') }}
      - run: deno test

Making caching of installed dependencies built-in makes it a lot easier and less error prone to get great caching with 1 or 2 extra inputs to denoland/setup-deno.

Closes #31

Sorry, something went wrong.

@csvn csvn requested a review from lucacasonato as a code owner November 25, 2024 20:15
@csvn csvn mentioned this pull request Nov 25, 2024
Comment on lines +22 to +23
cache-hit:
description: A boolean indicating whether the cache was hit.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This output can be used to conditionally run deno install. E.g.:

  - uses: denoland/setup-deno@v2
    id: deno
    with:
      cache: true
  - if: steps.deno.outputs.cache-hit != 'true'
    run: deno install
  - run: deno test

@@ -6,6 +6,7 @@
"license": "MIT",
"type": "module",
"dependencies": {
"@actions/cache": "^3.3.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @actions/cache package unfortunately has a lot of dependencies. Maybe we'd need a build step to avoid regressing clone time for this action, and to speed up execution?

    "@azure/abort-controller": "^1.1.0",
    "@azure/ms-rest-js": "^2.6.0",
    "@azure/storage-blob": "^12.13.0",
    "@protobuf-ts/plugin": "^2.9.4",

@csvn
Copy link
Author

csvn commented Dec 3, 2024

Here's a link to a PR where the Actions passes (after installing node_modules and disabled Windows jobs, since they failed on my fork for some reason).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache deno modules
1 participant