如何在 CircleCI 上缓存 Elixir / Phoenix?

How to cache Elixir / Phoenix on CircleCI?

目前,我的CircleCI 2.0 Elixir 个项目的缓存策略如下:

  - restore_cache:
      keys:
        - v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
        - v1-mix-cache-{{ .Branch }}
        - v1-mix-cache
        - v1-build-cache-{{ .Branch }}
        - v1-build-cache

  - save_cache:
      key: v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
      paths: deps
  - save_cache:
      key: v1-mix-cache-{{ .Branch }}
      paths: deps
  - save_cache:
      key: v1-mix-cache
      paths: deps
  - save_cache:
      key: v1-build-cache-{{ .Branch }}
      paths: _build
  - save_cache:
      key: v1-build-cache
      paths: _build

然而,有时,它会导致这样的错误:

===> Compiling certifi

=ERROR REPORT==== 12-Jul-2018::15:37:40 ===
Loading of /home/circleci/project/_build/test/lib/parse_trans/ebin/parse_trans.beam failed: badfile

=ERROR REPORT==== 12-Jul-2018::15:37:40 ===
beam/beam_load.c(1863): Error loading module parse_trans:
  This BEAM file was compiled for a later version of the run-time system than 20.
  To fix this, please recompile this module with an 20 compiler.
  (Use of opcode 162; this emulator supports only up to 159.)

===> Compiling src/certifi.erl failed

有时我们得到:

 ** (UndefinedFunctionError) function :hackney.request/5 is undefined (module :hackney is not available)

/home/circleci/project/_build/test/lib/hackney/ebin/hackney.beam failed: :badfile

 12:44:02.665 [error] beam/beam_load.c(1863): Error loading module hackney:
   This BEAM file was compiled for a later version of the run-time system than 20.
   To fix this, please recompile this module with an 20 compiler.
   (Use of opcode 162; this emulator supports only up to 159.)

当然,一切都是缓存问题,因为当我们重新[​​=28=]没有缓存的构建时,一切都按预期工作。

这不会每次都发生,但有时会出现不同的错误。

你们有Elixir项目可靠的缓存策略吗?

免责声明:我是 CircleCI 开发者倡导者

我在这里看到 1 个问题和 1 个潜在问题。

首先,您不想为部分键多次保存缓存。恢复缓存键通过部分匹配来工作。当你保存缓存时,使用一次完整的键名就可以了。恢复缓存将在需要时进行军事匹配。

第二,Elixir支持部分缓存吗?我自己不使用它,所以我还不太了解它。如果没有,您可能只想恢复完整的缓存键而不是任何部分键。

我的第一点:

  - restore_cache:
      keys:
        - v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
        - v1-mix-cache-{{ .Branch }}
        - v1-mix-cache
        - v1-build-cache-{{ .Branch }}
        - v1-build-cache

  - save_cache:
      key: v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
      paths: deps
  - save_cache:
      key: v1-build-cache-{{ .Branch }}
      paths: _build

第二点:

  - restore_cache:
      keys:
        - v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
        - v1-build-cache-{{ .Branch }}

  - save_cache:
      key: v1-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
      paths: deps
  - save_cache:
      key: v1-build-cache-{{ .Branch }}
      paths: _build

解决方案是:

  - restore_cache:
      keys:
        - v{{ .Environment.CACHE_VERSION }}-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
  - restore_cache:
      keys:
        - v{{ .Environment.CACHE_VERSION }}-build-cache-{{ .Branch }}

  - save_cache:
      key: v{{ .Environment.CACHE_VERSION }}-mix-cache-{{ .Branch }}-{{ checksum "mix.lock" }}
      paths: deps
  - save_cache:
      key: v{{ .Environment.CACHE_VERSION }}-build-cache-{{ .Branch }}
      paths: _build