Build Cache

Top-level setting that contains file paths from your build that you would like to cache. The cache is stored in a build’s teardown bookend and restored in a build’s setup bookend. You can limit access to the cache using the pipeline, event or job scope.

Scope Access
pipeline All builds in the same pipeline pipeline-scope
event All builds in the same event event-scope
job All builds for the same job job-scope

Example

cache:
   pipeline: [~/versions]
   event: [$SD_SOURCE_DIR/node_modules]
   job:
       usejobcache: [/tmp/test]

jobs:
    setnpmcache:
        image: node:lts
        steps:
            - install: npm install
        requires: [~commit, ~pr]
    usenpmcache:
        image: node:lts
        steps:
            - ls: ls
            - install: npm install
        requires: [setnpmcache]
    usepipelinecache:
        image: java:7
        steps:
            - cat: cat ~/versions || echo "versions file doesn't exist"
            - install: echo "v1.0.0" > ~/versions
        requires: [~commit, ~pr]
    usejobcache:
        image: node:lts
        steps:
            - ls-tmp: ls /tmp
            - echo: echo hi > /tmp/test
            - cat: cat ~/versions || echo "versions file doesn't exist"
        requires: [~commit, ~pr]

In the above example, the pipeline-scoped versions cache can be accessed under all builds in the pipeline to share same version. For event-scoped cache, we cache the node_modules folder under the event scope in the setnpmcache build so that the downstream usenpmcache build can save time on npm install. For job-scoped cache, we cache /tmp/test file so that it is available for any subsequent builds of the same job.

Example repo: https://github.com/screwdriver-cd-test/cache-example

Notes

Disable Cache for a Specific Job

If you do not want to use cache in a specific job, you can set cache configuration under the specific job configuration. When the value of cache is false, a specific job will not store and restore the cache even if top-level cache settings are set.

Example

cache:
   event: [$SD_SOURCE_DIR/node_modules]

jobs:
    setnpmcache:
        image: node:lts
        steps:
            - install: npm install
        requires: [~commit, ~pr]
    usenpmcache:
        image: node:lts
        steps:
            - ls: ls
            - install: npm install
        requires: [setnpmcache]
    no-usenpmcache:
        image: node:lts
        steps:
            - ls: ls
            - run-command: echo 'run command which will not use npmcache.'
        requires: [usenpmcache]
        cache: false

Update Cache from Parallel jobs

Please be cautious when specifying same cache path at an event or pipeline level in screwdriver.yaml having parallel jobs. When jobs run in parallel there is no guarantee which job will succeed first in writing cache, therefore the same cache path might get overwritten or lost.

How to handle the above scenario?

  1. Use the store-cli set command explicitly in a job where you need to cache.
  2. Specify different cache folders from each job that you want to cache and use them accordingly.
  3. Do write cache from single job and then run jobs in parallel.

Clearing the Cache

In order to clear the cache, you can go to the Options tab for your pipeline in the Screwdriver UI and click on the Trash icon under the Cache section.

Clear cache