Skip to content
Latchkey

How to Cache Dependencies in Bitbucket Pipelines

The caches key restores a directory at the start of a step and saves it at the end, so unchanged dependencies are not re-downloaded.

Add caches: to a step. Use the predefined node cache, or declare a custom cache under definitions.caches pointing at any path.

Steps

  • List caches: on the step (for example - node).
  • For a custom path, add it under definitions.caches.
  • Reference that custom cache name in the step caches: list.

bitbucket-pipelines.yml

bitbucket-pipelines.yml
definitions:
  caches:
    pip: ~/.cache/pip

pipelines:
  default:
    - step:
        image: python:3.12
        caches:
          - pip
        script:
          - pip install -r requirements.txt
          - pytest

Gotchas

  • A cache is keyed by its name and the step image; it is shared across branches, so do not cache build output you need fresh.
  • Caches over 1 GB are not saved; trim the path or split it.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →