Enermis
Enermis

Reputation: 697

CircleCI cache keys mismatch

When i'm running my build it shows this output when dealing with the bundler cache:

Restoring Cache:

No cache is found for key: gems-E2sY+gLiTCg0YwHjev8ZrCqAW_U9vUHKPA_FZnRiAPc=-circleci-project-setup
No cache is found for key: gems-E2sY+gLiTCg0YwHjev8ZrCqAW_U9vUHKPA_FZnRiAPc=

Saving Cache

Creating cache archive...
Uploading cache archive...
Stored Cache to gems-wV_lhNyGV+evee5LBWbzjy7uuCEgZRgX8PlgbJreAv4=-circleci-project-setup
  * /home/circleci/project/vendor/bundle

but the key it used to save doesn't match the key its using to fetch. So the next run of the build will have another cache-miss

Circle File:

version: 2.1 # Use 2.1 to enable using orbs and other features.

# Declare the orbs that we'll use in our config.
# read more about orbs: https://circleci.com/docs/2.0/using-orbs/
orbs:
  ruby: circleci/[email protected]
  node: circleci/node@2

jobs:
  build:
    docker:
      - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
    steps:
      - checkout
      - ruby/install-deps: # use the ruby orb to install dependencies
          key: "gems"
      # use the node orb to install our packages
      # specifying that we use `yarn` and to cache dependencies with `yarn.lock`
      # learn more: https://circleci.com/docs/2.0/caching/
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"

  test:
    # we run "parallel job containers" to enable speeding up our tests;
    # this splits our tests across multiple containers.
    # !! No we don't, we only have a single node in the free plan
    parallelism: 1
    # here we set TWO docker images.
    docker:
      - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
      - image: circleci/postgres:9.5-alpine
        environment: # add POSTGRES environment variables.
          POSTGRES_USER: xxx
          POSTGRES_DB: xxx_test
          POSTGRES_PASSWORD: xxx
    # environment variables specific to Ruby/Rails, applied to the primary container.
    environment:
      BUNDLE_JOBS: "3"
      BUNDLE_RETRY: "3"
      DB_HOST: 127.0.0.1
      db_password: xxx
      RAILS_ENV: test
    # A series of steps to run, some are similar to those in "build".
    steps:
      - checkout
      - ruby/install-deps:
          key: "gems"
      - node/install-packages:
          pkg-manager: yarn
          cache-key: "yarn.lock"
      # Here we make sure that the secondary container boots
      # up before we run operations on the database.
      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          name: Database setup
          command: bundle exec rails db:schema:load --trace
      # Run tests
      - run:
          name: Tests
          command: bundle exec rails test

# We use workflows to orchestrate the jobs that we declared above.
workflows:
  version: 2
  build_and_test:     # The name of our workflow is "build_and_test"
    jobs:             # The list of jobs we run as part of this workflow.
      - build         # Run build first.
      - test:         # Then run test,
          requires:   # Test requires that build passes for it to run.
            - build   # Finally, run the build job.

How would i fix this so that the cache is stored properly

Upvotes: 1

Views: 604

Answers (1)

Enermis
Enermis

Reputation: 697

I think i found the issue.

I never pushed my Gemfile.lock file while building the project. I think the gemfile.lock file is hashed to generate the key, since the repository contained an old gemfile the hash didn't match. After running bundler the lockfile changes so it gets a new hash (which was already present). Since i never ran bundler locally it never updated my local gemfile.lock so it was never pushed to the repo.

Upvotes: 1

Related Questions