Repository navigation
feat(build-dependency-cache): share the S3 cache actions for sbt, mise and uv (SC-88184) #20
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| name: build-dependency-cache | ||
|
|
||
| # Tests build-dependency-cache, so a change can be tested before consumers pin it: s3_cache.py with pytest, ruff and | ||
| # ty, and s3-cache.js, which its JavaScript actions run, with node's test runner. ubuntu-latest has GNU tar and zstd, | ||
| # as the self-hosted runners do, so the one test a laptop skips runs here. | ||
| on: | ||
| pull_request: | ||
| paths: | ||
| - build-dependency-cache/** | ||
| - ruff.toml | ||
| - mise.toml | ||
| - .github/workflows/build-dependency-cache.yml | ||
| push: | ||
| branches: [main] | ||
| paths: | ||
| - build-dependency-cache/** | ||
| - ruff.toml | ||
| - mise.toml | ||
| - .github/workflows/build-dependency-cache.yml | ||
|
|
||
| permissions: {} | ||
|
|
||
| defaults: | ||
| run: | ||
| working-directory: build-dependency-cache | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Test build-dependency-cache | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| # python from the folder's mise.toml, uv and node from the root's. Not setup-mise, which needs the S3 bucket | ||
| # that only the self-hosted runners reach. | ||
| - uses: jdx/mise-action@8418fedb1110381a0d3ba51a7a415d7c1b884550 # v5.2.2 | ||
| with: | ||
| version: 2026.11.10 | ||
| working_directory: build-dependency-cache | ||
| - name: Install dependencies | ||
| run: uv sync --locked | ||
| - name: Run the Python tests | ||
| run: uv run --frozen pytest -rs | ||
| - name: Run the JavaScript tests | ||
| run: node --test tests/s3-cache.test.js | ||
| - name: Lint | ||
| run: uv run --frozen ruff check . | ||
| - name: Check formatting | ||
| run: uv run --frozen ruff format --check . | ||
| - name: Type-check | ||
| run: uv run --frozen ty check |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .venv/ | ||
| __pycache__/ | ||
| .pytest_cache/ | ||
| .ruff_cache/ | ||
| # Where the VS Code mise extension links the tools a folder's mise.toml pins. | ||
| .vscode/mise-tools/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| // The mise extension links the tools this folder's mise.toml (and the repository root's) pins into | ||
| // .vscode/mise-tools/: ty and ruff at the versions CI runs. [deps.uv] in mise.toml keeps .venv in sync, and ty | ||
| // finds it there, so imports resolve when this folder is opened on its own or through python.code-workspace. | ||
| "mise.configureExtensionsAutomatically": true, | ||
| "mise.configureExtensionsUseSymLinks": true, | ||
| "mise.configureExtensionsIncludeGlobalTools": false, | ||
| "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python", | ||
| // ty is the language server: Pylance would report the same code again, by other rules. | ||
| "python.languageServer": "None", | ||
| "ty.path": [ | ||
| "${workspaceFolder}/.vscode/mise-tools/ty" | ||
| ], | ||
| "ruff.path": [ | ||
| "${workspaceFolder}/.vscode/mise-tools/ruff" | ||
| ], | ||
| "[python]": { | ||
| "editor.defaultFormatter": "charliermarsh.ruff" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # build-dependency-cache | ||
|
|
||
| Actions that keep CI's dependency and tool caches in S3 instead of GitHub's cache. Our self-hosted runners sit in a | ||
| private subnet: they reach GitHub's cache, which is Azure storage, and package registries through a NAT gateway that | ||
| bills every byte, and S3 in their own region through a gateway endpoint that's free. Moving sbt's caches to S3 removed | ||
| most of the NAT's traffic (SC-87124); mise's tools were most of what was left (SC-88184). | ||
|
|
||
| - [`cache-sbt`](cache-sbt/action.yml): sbt's dependency caches (coursier, ivy, the sbt launcher). | ||
| - [`setup-mise`](setup-mise/action.yml): `jdx/mise-action`, with mise and the tools it installs kept in S3. | ||
| - [`cache-uv`](cache-uv/action.yml): uv's cache, the packages a project's `uv sync` downloads. | ||
| - [`s3_cache.py`](s3_cache.py): the archive logic all three share, and [`s3-cache.js`](s3-cache.js), which the | ||
| JavaScript actions start it with. Callers never run either directly. | ||
|
|
||
| ## Configure | ||
|
|
||
| The actions need the base S3 URL of the cache, `s3://<bucket>/<prefix>`, as their `url` input. It's kept out of this | ||
| public repository, in an organization variable: | ||
|
|
||
| 1. In the organization's settings, under **Secrets and variables → Actions → Variables**, create the variable | ||
| `COMMON_GITHUB_BUILD_DEPENDENCY_CACHE` with that URL. | ||
| 2. Under **Repository access**, give it to every repository that uses these actions. A repository the variable isn't | ||
| shared with gets an empty `url`, and each cache step fails, so no job runs without its cache unnoticed. Make sure | ||
| it's a variable, not a secret: the steps read `vars`, and a secret of the same name leaves `url` empty. | ||
| 3. Give the runners read and write on that prefix, and an expiry rule for it. | ||
|
|
||
| An action can't read the variable itself (its `action.yml` can't refer to `vars`), so every step passes it, as below. | ||
| The bucket, its expiry rule and the runners' access to it are in narrative-devtools (`var/shared/` and | ||
| `github-runner/terraform/iam.tf`). | ||
|
|
||
| ## Use | ||
|
|
||
| Pin a released version by its full commit SHA, as for everything in this repository: | ||
|
|
||
| ```yaml | ||
| - name: Restore the sbt dependency cache | ||
| # Repository variable: "off" turns the cache off. | ||
| if: vars.SBT_DEPENDENCY_CACHE != 'off' | ||
| uses: narrative-io/common-github/build-dependency-cache/cache-sbt@<full-40-char-sha> # vX.Y.Z | ||
| with: | ||
| url: ${{ vars.COMMON_GITHUB_BUILD_DEPENDENCY_CACHE }} | ||
| save: true # only in the job whose dependencies the archive should hold, such as sbt test | ||
|
|
||
| - name: Set up mise | ||
| uses: narrative-io/common-github/build-dependency-cache/setup-mise@<full-40-char-sha> # vX.Y.Z | ||
| with: | ||
| url: ${{ vars.COMMON_GITHUB_BUILD_DEPENDENCY_CACHE }} | ||
| version: 2026.11.10 | ||
| install_args: java asdf:mise-plugins/mise-sbt | ||
| s3_cache: ${{ vars.MISE_TOOLS_CACHE != 'off' }} | ||
|
|
||
| - name: Restore the uv cache | ||
| if: vars.UV_CACHE != 'off' | ||
| uses: narrative-io/common-github/build-dependency-cache/cache-uv@<full-40-char-sha> # vX.Y.Z | ||
| with: | ||
| url: ${{ vars.COMMON_GITHUB_BUILD_DEPENDENCY_CACHE }} | ||
| working_directory: my-project # the folder holding its uv.lock | ||
| save: true # only in the job that syncs everything the project locks, such as its tests | ||
| ``` | ||
|
|
||
| ## How an archive works | ||
|
|
||
| Each call archives one kind of cache, as `zstd`-compressed tar, at `<url>/<repository>/<prefix>/<key>.tar.zst`, with a | ||
| `latest` file beside it naming the key `main` saved last. | ||
|
|
||
| | | sbt (`cache-sbt`) | mise (`setup-mise`) | uv (`cache-uv`) | | ||
| | ---------------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------- | | ||
| | Folders, under `HOME` | `.cache/coursier`, `.ivy2/cache`, `.sbt/boot`, `.sbt/1.0/zinc` | `.local/share/mise`, without `downloads/` | `.cache/uv` (or `UV_CACHE_DIR`), without `builds-v0/` | | ||
| | Prefix | the optional `scope` input | `mise-tools/<hash of the mise version, working_directory and install_args>` | `uv/<working_directory>` | | ||
| | Key: a hash of these tracked files | every `*.sbt`; `*.scala` and `build.properties` under `project/`; `mise.toml` | every `mise.toml`, `.mise.toml`, `mise.lock` and `.tool-versions` | the project's `uv.lock` | | ||
| | Saves | from its post step, when the job ends with every step passed, if `save: true` | right after `mise-action` installs, after `mise prune --yes --tools` | from its post step, as `cache-sbt`, if `save: true` | | ||
|
|
||
| - **The key** hashes each key file's object ID in git's index, not the file on disk, so a job that edits build files | ||
| between restore and save, as `sbt-dependency-submission` does in `project/`, still saves under the key it restored. | ||
| - **Restore** takes the archive for the exact key, else the one `latest` names, and the tool fetches whatever changed | ||
| since. A runner that already restored or saved the key skips the download: runners take several jobs before they're | ||
| replaced. A half-extracted archive is deleted, folders and all, rather than left looking complete. | ||
| - **Save** only acts on a push to `main`, and only for a key that has no archive yet; `latest` moves only after the | ||
| upload. Archives expire with the bucket's lifecycle rule, and the next push to `main` saves the key again. | ||
| - **Failures only warn, except a missing URL.** A restore or save that fails, because S3 can't be reached or an archive | ||
| is broken, logs a warning and the job carries on; the tool then downloads what it needs. An empty `url`, or a | ||
| JavaScript action that can't start Python, fails the step instead, since every job would otherwise quietly run | ||
| without its cache. | ||
| - **Each restore and save reports** a notice with the archive's compressed size and how long the transfer and the tar | ||
| took, then what the folders hold on disk. The step's log breaks it down per folder in a collapsed group and, for | ||
| mise, lists the installed versions. Compare `main`'s saves over time to see whether an archive grows. | ||
|
|
||
| ### Why `cache-sbt` and `cache-uv` are JavaScript and `setup-mise` is composite | ||
|
|
||
| `cache-sbt` and `cache-uv` are JavaScript actions only to get a post step, which saves when the job ends, after the | ||
| build or `uv sync` has fetched everything, as `actions/cache` does. Composite actions can't have one, and without it | ||
| every workflow would need a second step, placed last, to save. Their scripts only start `s3_cache.py`, through | ||
| `s3-cache.js`. | ||
|
|
||
| `setup-mise` has to be composite to call `jdx/mise-action`, and saves right after the install instead: mise's folder is | ||
| complete then, a job that fails later still saves it, and later steps can't add to it. Saving at the end would let the | ||
| job's real work start a few seconds sooner on the pushes that save; its save step's comment explains the trade-off. | ||
|
|
||
| None calls a sibling action: an action here can only reach another one with | ||
| `uses: narrative-io/common-github/<path>@<commit>`, and a commit can't name itself. They run `s3_cache.py` from the | ||
| checkout of this repository the runner made for the action. | ||
|
|
||
| ### What's trusted | ||
|
|
||
| `mise-action` checks the restored mise binary against mise's signed checksums before running it. Nothing checks the | ||
| tools, the jars or the packages. Only a push to `main` saves, but that's the actions' own check, not a permission: | ||
| every job on the runner fleet can write the prefix, so an archive is only as trusted as everything that runs on the | ||
| runners, pull requests included. GitHub's cache, by contrast, is scoped by branch. | ||
|
|
||
| ## Test | ||
|
|
||
| The folder is a uv project, set up with mise: its `mise.toml` pins Python 3.11, the oldest a runner has, and the | ||
| repository's pins uv and node. From the folder: | ||
|
|
||
| ```sh | ||
| uv run pytest # s3_cache.py | ||
| node --test tests/s3-cache.test.js # s3-cache.js, which cache-sbt and cache-uv run | ||
| uv run ruff check . && uv run ruff format --check . && uv run ty check | ||
| ``` | ||
|
|
||
| `s3_cache.py` keeps what it decides in pure functions, which the tests check with plain values, and takes S3, tar, git | ||
| and the clock as parameters, so its restores and saves are tested against an S3 kept in memory and a throwaway `HOME`. | ||
| The actions still run it with the runner's own `python3`: it needs nothing beyond the standard library. One test packs | ||
| and unpacks with the real tar and zstd, and skips where tar isn't GNU tar, as on macOS. The `build-dependency-cache` | ||
| workflow runs all of it on every pull request that changes this folder. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| name: Cache sbt's dependencies | ||
| description: > | ||
| Restore the coursier, ivy and sbt launcher caches from S3 when the step runs, and save them when the job | ||
| ends, through ../s3_cache.py. | ||
|
|
||
| Put the step before anything that runs sbt, or writes into sbt's global directory. It saves from its post | ||
| step, after every other step of the job has passed, only in a job that passes save as true, and only on a | ||
| push to main. | ||
|
|
||
| If a restore or save fails, the step logs a warning and the job carries on; sbt then downloads what it | ||
| needs. Callers keep a kill switch of their own, as a repository variable checked in the step's if; a | ||
| skipped step skips the save too, since a post step only runs when its main step ran. | ||
|
|
||
| inputs: | ||
| scope: | ||
| description: > | ||
| Optional path under the repository, for jobs that resolve different dependencies (an aggregate, a | ||
| separate build) and should keep separate archives. Jobs with the same scope share one. | ||
| default: "" | ||
| save: | ||
| description: > | ||
| "true" in the job whose dependencies the archive should hold, such as `sbt test`, which resolves | ||
| everything a build needs. Other jobs only restore: one that resolves less, such as scalafmt, could | ||
| otherwise finish first and save a thin archive under the key every job shares. | ||
| default: "false" | ||
| url: | ||
| description: > | ||
| Base S3 URL, s3://<bucket>/<prefix>; callers pass the organization variable | ||
| COMMON_GITHUB_BUILD_DEPENDENCY_CACHE. The repository name, scope and key go under it. The runners need | ||
| read and write there. Empty fails the step. | ||
| required: true | ||
|
|
||
| # A JavaScript action rather than a composite one: only JavaScript and Docker actions get a post step, which | ||
| # saves when the job ends, as actions/cache does, so a workflow needs one step instead of a restore and a save. | ||
| # Both scripts start ../s3_cache.py through ../s3-cache.js, so the archive logic stays in one place. | ||
| runs: | ||
| using: node24 | ||
| main: restore.js | ||
| post: save.js | ||
| post-if: success() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // cache-sbt's step: restores the archive. The action is JavaScript only so that save.js can run as its post step; | ||
| // ../s3-cache.js explains. | ||
| const { input, run } = require("../s3-cache.js"); | ||
|
|
||
| process.exitCode = run("sbt", "restore", input("scope")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // cache-sbt's post step, the reason the action is JavaScript (../s3-cache.js explains): it runs when the job ends, | ||
| // and only if every step passed (post-if: success() in action.yml). | ||
| const { input, saveIfAsked } = require("../s3-cache.js"); | ||
|
|
||
| process.exitCode = saveIfAsked("sbt", input("scope")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| name: Cache uv's packages | ||
| description: > | ||
| Restore uv's cache, the packages a project's `uv sync` downloads, from S3 when the step runs, and save it | ||
| when the job ends, through ../s3_cache.py. Each project keeps its own archive, keyed on its uv.lock. | ||
|
|
||
| Put the step before the project's first `uv sync`. It saves from its post step, after every other step of | ||
| the job has passed, only in a job that passes save as true, and only on a push to main. | ||
|
|
||
| If a restore or save fails, the step logs a warning and the job carries on; uv then downloads what it | ||
| needs. Callers keep a kill switch of their own, as a repository variable checked in the step's if; a | ||
| skipped step skips the save too, since a post step only runs when its main step ran. | ||
|
|
||
| inputs: | ||
| working_directory: | ||
| description: > | ||
| The project's folder, the one holding its uv.lock, relative to the repository root. Empty for a project | ||
| at the root. | ||
| default: "" | ||
| save: | ||
| description: > | ||
| "true" in the job that syncs everything the project locks, such as its tests. A job that syncs less, | ||
| with --no-dev say, could otherwise finish first and save a thin archive under the project's key. | ||
| default: "false" | ||
| url: | ||
| description: > | ||
| Base S3 URL, s3://<bucket>/<prefix>; callers pass the organization variable | ||
| COMMON_GITHUB_BUILD_DEPENDENCY_CACHE. The repository name, project and key go under it. The runners need | ||
| read and write there. Empty fails the step. | ||
| required: true | ||
|
|
||
| # A JavaScript action rather than a composite one, for the post step, as cache-sbt; ../s3-cache.js explains. | ||
| runs: | ||
| using: node24 | ||
| main: restore.js | ||
| post: save.js | ||
| post-if: success() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // cache-uv's step: restores the project's archive. The action is JavaScript only so that save.js can run as its | ||
| // post step; ../s3-cache.js explains. | ||
| const { input, run } = require("../s3-cache.js"); | ||
|
|
||
| process.exitCode = run("uv", "restore", input("working_directory")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // cache-uv's post step, the reason the action is JavaScript (../s3-cache.js explains): it runs when the job ends, | ||
| // after uv has synced the project, and only if every step passed (post-if: success() in action.yml). | ||
| const { input, saveIfAsked } = require("../s3-cache.js"); | ||
|
|
||
| process.exitCode = saveIfAsked("uv", input("working_directory")); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: We're doing a lot of work here for what devenv almost gives us for free. No change necessary for this PR, just noting that if we were to lean into devenv more heavily we'd get so much more of this out of the box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear: switching to devenv isn't seemless or painless, just that this kind of caching comes (mostly) for free. :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If devenv could simplify how we do CI / caching / etc (in ways that are complementary to Dagger?), I'm happy with the direction of devenv-ifying CI.