如何在CircleCI上缓存Elixir / Phoenix?

时间:2018-07-13 10:01:20

标签: continuous-integration elixir phoenix-framework circleci circleci-2.0

目前,我对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.)

一切当然都是缓存问题,因为当我们重新运行不带缓存的构建时,一切都会按预期进行。

并非每次都发生这种情况,而是会不时出现不同的错误。

您对Elixir个项目有可靠的缓存策略吗?

2 个答案:

答案 0 :(得分:1)

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

我在这里看到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

答案 1 :(得分:0)

解决方法是:

  - 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