CircleCI 缓存键不匹配

CircleCI cache keys mismatch

当我运行构建我的构建时,它在处理捆绑程序缓存时显示此输出:

正在恢复缓存:

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=

保存缓存

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

但它用于保存的密钥与用于获取的密钥不匹配。因此构建的下一个 运行 将有另一个缓存未命中

圈子文件:

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/ruby@1.0
  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.

我该如何解决这个问题才能正确存储缓存

我想我找到了问题。

我在构建项目时从未推送过 Gemfile.lock 文件。我认为 gemfile.lock 文件被散列以生成密钥,因为存储库包含一个散列不匹配的旧 gemfile。在 运行 bundler 之后,锁文件发生变化,因此它获得了一个新的散列(已经存在)。因为我从来没有在本地 运行 捆绑器,它从来没有更新我的本地 gemfile.lock 所以它从来没有被推送到 repo。