Caching dependencies

Bitbucket Pipelines is able to cache external build dependencies and directories, such as 3rd-party libraries, between builds.

As well as providing faster builds and developer feedback, caching can also reduce the number of build minutes consumed.

 

What is dependency caching?

Most builds start by downloading dependencies, which can account for a significant period of each build. As most dependencies do not change, it can be faster to not download them every time from the internet. Pipelines provides a way cache dependencies and directories between builds. 

Set up caching

To enable caching, simply add a cache section to your step .

Here's an example of how to cache your node_modules directory for a Node.js project using a pre-defined cache.

bitbucket-pipelines.yml
pipelines:
  default:
    - step:
        caches:
          - node
        script:
          - npm install
          - npm test

Pre-defined caches

Pipelines provides pre-defined caches for commonly used language tools:

Cache name In your step Directories
composer
caches:
  - composer
~/.composer/cache
dotnetcore
caches:
  - dotnetcore
~/.nuget/packages
gradle
caches:
  - gradle
~/.gradle/caches
ivy2
caches:
  - ivy2
~/.ivy2/cache
maven
caches:
  - maven
~/.m2/repository
node
caches:
  - node
node_modules
pip
caches:
  - pip
~/.cache/pip
sbt
caches:
  - sbt 
  - ivy2

~/.sbt

~/.ivy2/cache

Note that when you use the sbt tool you need to enable both the sbt and ivy2 caches.


Custom caches for other build tools and directories

If your build tool isn't listed above, you can still define a custom cache for your repository in your bitbucket-pipelines.yml file. First, define the cache and the directory to be cached in the definitions section, then refer to that cache in a pipeline's step:

bitbucket-pipelines.yml
image: ruby:2.4.0
pipelines:
  default:
    - step:
        caches:
          - bundler
        script:
          - bundle install

definitions:
  caches:
    bundler: vendor/bundle

The defined cache will be saved after the first successful build.

Cache directory paths can be absolute, or relative to the clone directory, for example:

  • $HOME/.npm
  • ~/.gradle/wrapper
  • /usr/local/lib/node_modules
  • vendor/bundle

Note that, for Ruby, you should install gems in a separate location from the system's gem repo, like this:
$ bundle install --path vendor/bundle

Caching multiple directories

Some builds might benefit from caching multiple directories. Simply reference multiple caches in your step like this:

bitbucket-pipelines.yml
image: java:8
pipelines:
  default:
    - step:
        caches:
          - gradle               # pre-defined cache
          - gradlewrapper        # custom cache
        script:
          - ./gradlew build
definitions:
  caches:
    gradlewrapper: ~/.gradle/wrapper

How does caching work?

When does a cache get saved?

Caches are saved on successful builds when the cache is empty. Only caches under 1GB compressed are saved.

When does a cache get restored?

At the beginning of each build, saved caches are downloaded if available and placed in their directories.

When does a cache get cleared?

A cache is cleared automatically 1 week after being saved. Additionally, caches can be cleared manually in the Bitbucket UI. Just choose Caches at the top right of the Pipelines page, then trash a cache from the list:

 

What should I cache and not cache?

Dependency caching is all about reducing build times by avoiding downloading and building the same files repeatedly.

That means you should cache things like:

  • Language-specific dependencies.
  • Binaries of dependencies that take a while to compile.

You shouldn't cache:

  • Sensitive data such as passwords and credentials

Remember that caches are ephemeral (that is, caches can be blown away at any time), so your builds should be configured to work whether or not the cache is present.

最終更新日 2017 年 9 月 27 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.