Yarn caching in 2.0

yarn

#1

I have separated my build into multiple jobs. One of those jobs is setting up the yarn dependencies and installing. The next job, which requires the yarn dependencies to be finished, is running tests.

Yarn dependencies:

  yarn_dependencies:
    docker:
      - image: circleci/ruby:2.4.2-node
    working_directory: ~/foo/bar
    steps:
      - restore_cache:
          keys:
            - repo-{{ .Environment.CIRCLE_SHA1 }}
      - run:
          name: Install node@6.11.4
          command: |
            set +e
            touch $BASH_ENV
            curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
            echo 'export NVM_DIR="$HOME/.nvm"' >> $BASH_ENV
            echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
            echo 'nvm install v6.11.4' >> $BASH_ENV
            echo 'nvm alias default v6.11.4' >> $BASH_ENV
      - restore_cache:
          keys:
            - yarn-{{ checksum "yarn.lock" }}
      - run: yarn install --cache-folder ~/.cache/yarn
      - save_cache:
          key: yarn-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

Test:

  test:
    docker:
      - image: circleci/ruby:2.4.2-node
    working_directory: ~/foo/bar
    steps:
      - restore_cache:
          keys:
            - repo-{{ .Environment.CIRCLE_SHA1 }}
      - run:
          name: Install node@6.11.4
          command: |
            set +e
            touch $BASH_ENV
            curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
            echo 'export NVM_DIR="$HOME/.nvm"' >> $BASH_ENV
            echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
            echo 'nvm install v6.11.4' >> $BASH_ENV
            echo 'nvm alias default v6.11.4' >> $BASH_ENV
      - restore_cache:
          keys:
            - yarn-{{ checksum "yarn.lock" }}
      - run: yarn install --cache-folder ~/.cache/yarn
     
     ...

When yarn install is run inside the test job, it is doing a fresh install of the dependencies. What do I need to change so that I am using the cached version?

I also have to use node 6.11.4. Is there a docker image for this? Or can this also be cached somehow?

Thanks!


#2

Restoring the cache after yarn install in your build job should remove the need to yarn install dependencies again in your test job.

There’s also a previous discuss post on caching yarn here which may be helpful.

I’m not sure if we have an image with node 6.11.4 installed, as we generally take updates from the upstream images (in this case the ruby team’s docker image), but I’ll check on that for you.


#3

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.