> ## Documentation Index
> Fetch the complete documentation index at: https://instabox.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Faster dependency installs in CI

> Keep npm, pip, cargo, and Go packages between GitHub Actions jobs on InstaBox, with actions/cache or a snapshot.

Each job with an InstaBox label runs on a fresh machine, so a job that installs its packages
downloads them again every time. You can keep the packages between jobs in two ways. The
`actions/cache` action restores a folder at the start of a job and saves it at the end, and it
works on InstaBox as it does on GitHub's runners. A snapshot has the packages on the machine's
disk when the job starts, so its install step has little left to do. Its machine is not started
ahead of the job, though, so the job waits for it to start.

| | `actions/cache` | A snapshot |
| - | - | - |
| Setup | A step in the workflow | Prepare a machine once and save it |
| Updates itself | Yes, when the lockfile changes | No, you save it again |
| Works in | Every repository | Private repositories that do not run fork workflows (setting one up needs a paid plan) |
| Cost at the start of a job | A download and an unpack | The machine starts when the job arrives, and a larger snapshot takes longer |
| Best for | Dependencies that change often | A large toolchain or a slow first build |

Start with `actions/cache`. Add a snapshot when the restore itself is slow, or when the job needs
tools that take minutes to install.

## Cache packages with actions/cache

Add the `actions/cache` step before the step that installs your packages. The `key` names one
version of the cache, so make it change when your lockfile changes. A job that finds its key gets
the folder back, and a job that does not find it installs as usual and saves the folder when the
job ends.

On an InstaBox machine, `~` is `/data/home`. A `~/` path that you cache on `ubuntu-latest`
restores to the same place on InstaBox, so a workflow needs no change when you move it.

### npm

Cache npm's download folder, and let `npm ci` build `node_modules` from it.

```yaml theme={null}
steps:
  - uses: actions/checkout@v4
  - uses: actions/cache@v4
    with:
      path: ~/.npm
      key: npm-${{ hashFiles('package-lock.json') }}
      restore-keys: npm-
  - run: npm ci
```

With `restore-keys`, a job whose lockfile changed starts from the newest older cache, and
downloads only the packages that are new.

### pip

Cache pip's download folder.

```yaml theme={null}
steps:
  - uses: actions/checkout@v4
  - uses: actions/cache@v4
    with:
      path: ~/.cache/pip
      key: pip-${{ hashFiles('requirements.txt') }}
      restore-keys: pip-
  - run: pip install -r requirements.txt
```

### Cargo

Cache the registry and the build output together, because compiling the dependencies usually
takes longer than downloading them.

```yaml theme={null}
steps:
  - uses: actions/checkout@v4
  - uses: actions/cache@v4
    with:
      path: |
        ~/.cargo/registry
        ~/.cargo/git
        target
      key: cargo-${{ hashFiles('Cargo.lock') }}
      restore-keys: cargo-
  - run: cargo test
```

### Go

Cache the module folder.

```yaml theme={null}
steps:
  - uses: actions/checkout@v4
  - uses: actions/cache@v4
    with:
      path: ~/go/pkg/mod
      key: go-${{ hashFiles('go.sum') }}
      restore-keys: go-
  - run: go test ./...
```

> [!NOTE]
>
> GitHub stores these caches, so GitHub's limits on cache size and age apply. On a paid plan, a
> job's machine uploads faster than other machines do, which shortens the save of a large cache.

## Start jobs with the packages already on disk

A job that starts from a snapshot has your repository, toolchain, and package folders on its disk
when it begins. Its machine starts when the job arrives instead of ahead of it, so the job waits
for that start ([What to know](https://instabox.sh/docs/use-cases/ci/custom-setup.md#what-to-know)). Prepare a `ci` machine,
install and build once, save it, and name the snapshot in `runs-on`. [Advanced: custom setup](https://instabox.sh/docs/use-cases/ci/custom-setup.md) has the steps.

```yaml theme={null}
jobs:
  test:
    runs-on: instabox-medium-snapshot-acme-warm
```

Put the package folders in the snapshot: `~/.npm`, `~/.cache/pip`, `~/.cargo/registry`, or
`~/go/pkg/mod`. For a compiled language, also build the project once before you save, so the
build output is there too.

A snapshot holds the whole machine, not one folder. Each job gets its own copy, and a job cannot
change the snapshot. The packages that a job downloads are gone when the job ends.

### Use both together

You can keep `actions/cache` in a job that starts from a snapshot. The snapshot holds the
packages from the day you saved it. Use a `key` from your lockfile as above: after the lockfile
changes, the first job saves a cache with the newer packages, and later jobs restore it, so their
install has little left to download. The cache step downloads its whole folder whenever it finds the key, so this helps
most when the snapshot is old.

### Save the snapshot again

Save the snapshot again when your dependencies have moved far from it, and at least once a month.
[Refresh the snapshot](https://instabox.sh/docs/use-cases/ci/custom-setup.md#refresh-the-snapshot) has the steps.

## Limits

> [accordion] Where snapshots work
>
> Jobs can start from a snapshot only in private repositories with **Run workflows from fork
> pull requests** turned off. Setting one up needs a paid plan ([Advanced: custom
> setup](https://instabox.sh/docs/use-cases/ci/custom-setup.md)). [When a job that names a snapshot is
> refused](https://instabox.sh/docs/use-cases/ci#when-a-job-that-names-a-snapshot-is-refused) lists the other reasons.

> [accordion] Secrets
>
> Keep tokens, SSH keys, and `.env` files out of a snapshot, because any workflow on a branch of
> a linked repository can name it. A cache that `actions/cache` saves follows GitHub's rules for
> which branches can read it.

> [accordion] No cache shared between machines
>
> A machine's disk is its own. Two jobs share packages only through `actions/cache` or by
> starting from the same snapshot.

> [card] [CI](https://instabox.sh/docs/use-cases/ci)
>
> Run GitHub Actions jobs on InstaBox, and start them from a snapshot.

> [card] [Snapshots](https://instabox.sh/docs/machines/snapshots.md)
>
> Save a machine and start new machines from it.
