# Cache files between runs

> **About this page**
>
> - Kind: How-to guide
> - Summary: Use a repository tag as a cache for dependencies and build outputs that a workflow would otherwise rebuild on every run.
> - Surface: CLI only
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/how-to-guides/cache-files-between-runs
> - CLI reference for its commands: [`swale pull`](https://docs.swale.io/reference/cli-reference.md#pull), [`swale push`](https://docs.swale.io/reference/cli-reference.md#push), [`swale tag delete`](https://docs.swale.io/reference/cli-reference.md#tag-delete)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

:::note[CLI-only]
Caching is built on push and pull, which are done exclusively with the CLI. There is no UI equivalent.
:::

This guide shows how to use a repository as a cache for files your workflows would otherwise rebuild on every run: dependency trees, build outputs, incremental checkpoints. Both patterns below rely on tag names being reusable: pushing to a name that already exists replaces that tag's snapshot, and only what changed is uploaded. [Why tags make a good cache](https://docs.swale.io/explanation/repositories-and-storage.md#why-tags-make-a-good-cache) explains why that makes caching cheap.

## Give the task the CLI's credentials

The recipes below run inside a task, where the CLI has no stored login. It reads the account from `SWALE_ACCOUNT_NAME` and the credential from `SWALE_ACCOUNT_TOKEN`, so the task definition declares both as inputs and sets them in its `exec.env`:

```md
---
name: Build with cache
description: Restores node_modules from a cache tag, installs, and refreshes the cache.
category: languages
inputs:
  account_name:
    required: true
  token:
    required: true
exec:
  image: my-registry.example.com/node-with-swale:1.0.0
  env:
    SWALE_ACCOUNT_NAME: ${{inputs.account_name}}
    SWALE_ACCOUNT_TOKEN: ${{inputs.token}}
  args:
    - sh
    - -c
    - |
      # One of the recipes below
---

# Build with cache

Restores the dependency cache, installs, and refreshes the cache.
```

The workflow passes the account's name and the workflow run token, `${{secrets.WORKFLOW_TOKEN}}`, as the task's arguments:

```yaml
build:
  name: Build
  uses: my-account/build-with-cache@1.0.0
  args:
    account_name: my-account
    token: ${{secrets.WORKFLOW_TOKEN}}
```

A task definition's `exec` resolves its own inputs and nothing else, so the token has to arrive as an input rather than as a secret named in `exec`; see [What resolves inside `exec`](https://docs.swale.io/reference/task-definition-syntax.md#what-resolves-inside-exec). The workflow run token reaches only the run's own project, and pushing, pulling and deleting tags there is what it is for; see [Workflow run token](https://docs.swale.io/reference/permissions-and-restrictions.md#workflow-run-token). The image needs the CLI installed; see [Install the CLI](https://docs.swale.io/reference/install-cli.md).

Both recipes restore the cache into the run's shared workspace, reached through `WORKFLOW_STORAGE`. If only the task doing the install needs the restored files, use `WORKFLOW_TASK_STORAGE` instead: it is local disk and faster for the write-heavy install step, but nothing on it survives the task. See [Runtime storage](https://docs.swale.io/reference/task-definition-syntax.md#runtime-storage).

## Keep a cache warm with a rolling tag

Push to the same tag name on every run:

```bash
# Work in the run's shared workspace: the image's own directories are read only.
cd "$WORKFLOW_STORAGE/my-app"

# Restore the cache, then refresh it for the next run.
# The first run has no snapshot to restore, and a missing tag is a failure, so
# tolerate it explicitly — otherwise the miss stops the job under `set -e`.
swale pull my-project/node-modules:latest --path ./node_modules || true
npm install
swale push my-project/node-modules:latest --path ./node_modules
```

Each push uploads only what changed since the last one. Use this when the freshest copy is always the one you want and an occasional miss is harmless: if two runs push the tag at the same time, it ends up holding one of the two snapshots.

## Make a cache entry reproducible with a hash-keyed tag

Derive the tag name from the inputs the cached content depends on, and push only when that name is absent:

```bash
cd "$WORKFLOW_STORAGE/my-app"

KEY="deps-$(sha256sum package-lock.json | cut -c1-16)"

# A successful pull is a cache hit; any failure — including a missing key — falls
# through to rebuilding and pushing it.
if swale pull "my-project/node-modules:$KEY" --path ./node_modules; then
  echo "Cache hit for $KEY"
else
  npm ci
  swale push "my-project/node-modules:$KEY" --path ./node_modules
fi
```

Branching on the pull itself costs one round trip instead of two, and treats a transport failure as a miss, so the run rebuilds instead of going on without its dependencies. Because the name changes whenever the inputs change, pulling a given key returns the same content every time, and two runs that push the same key at once push the same content.

This pattern accumulates tags as inputs change, so delete the keys you no longer build against.

## Clean up old cache entries

Delete the tag. A task can do this with the workflow run token too, so a workflow can evict its own stale cache keys; it passes `--yes`, since a task has no terminal to confirm at:

```bash
swale tag delete my-project/node-modules:deps-0a1b2c3d4e5f6789 --yes
```

Content that only an old key still refers to keeps counting toward account storage until you delete the key, and until it is permanently deleted, within three weeks; see [Deleted content](https://docs.swale.io/reference/billing.md#deleted-content).

## Next steps

- [Push and pull files](https://docs.swale.io/how-to-guides/push-and-pull-files.md)
- [Manage tags](https://docs.swale.io/how-to-guides/manage-tags.md)
- [Repositories and storage](https://docs.swale.io/explanation/repositories-and-storage.md)
