How to cache erlang builds on CI?

Hey everyone!

I’m working on optimising our build pipeline. I have a flow that looks like:

– compile
– dialyzer, test etc

These are executed in separate containers. The compiled files are moved from the first container to the second by saving the _build/dev directory to a workspace, which is attached to the subsequent containers.

This works well in that application files are never recompiled, however every time it always recompiles all the Erlang modules. What have I missed in order to cache these.

Thanks!
Sam

2 Likes

What do you mean by “compiles all the erlang modules”?

Are you compiling erlang or elixir from scratch in your build pipeline?

Also strategies for caching between subsequent builds or stages do depend on the system you use.

Jenkins with or without docker, gitlab-ci, travis or something completely different?

Are you targetting a docker container, LXS or even something completely different?

All those details affect the strategies you have available, which one you would use or how you use them.

We are using CircleCI (which runs all builds inside docker containers). The caching options available can save/restore any files, but I suspect it resets the timestamps on restore which might be causing this issue.

When I say “compiles all the Erlang modules”, I mean the output looks like this:

Summary
mix dialyzer --halt-exit-status
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Package unicode_util_compat-0.3.1 not found. Fetching registry updates and trying again...
===> Updating package registry...
===> Writing registry to /root/.cache/rebar3/hex/default/registry
===> Generating package index...
===> [appsignal:1.6.2], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.7.0-alpha.4], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0-beta.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.3], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.7.0-alpha.3], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.7.0-alpha.2], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.7.0-alpha.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.5], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.4], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0-alpha.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> Writing index to /root/.cache/rebar3/hex/default/packages.idx
===> Compiling idna
===> Compiling ranch
==> poolboy (compile)
Compiled src/poolboy_worker.erl
Compiled src/poolboy_sup.erl
Compiled src/poolboy.erl
===> Compiling hpack
==> ssl_verify_fun (compile)
Compiled src/ssl_verify_util.erl
Compiled src/ssl_verify_fingerprint.erl
Compiled src/ssl_verify_pk.erl
Compiled src/ssl_verify_hostname.erl
===> Compiling certifi
===> Compiling hackney
===> Compiling cowlib
src/cow_multipart.erl:392: Warning: call to crypto:rand_bytes/1 will fail, since it was removed in 20.0; use crypto:strong_rand_bytes/1

===> Compiling cowboy
===> Fetching rebar3_hex ({pkg,<<"rebar3_hex">>,<<"4.1.0">>})
===> Downloaded package, caching at /root/.cache/rebar3/hex/default/packages/rebar3_hex-4.1.0.tar
===> Compiling rebar3_hex
===> Compiling redbug
...dialyzer output

Maybe you can have a container which only runs mix deps.compile? Which would only get rebuilt if mix.lock changes.

This is exactly what we do :slight_smile:

The container runs mix deps.compile then copies _build/dev into a cache which is restored into subsequent containers.

The problem is for some reason, the Erlang builds are always marked as stale, so it always gets rebuilt regardless.

Not Erlang, id guess that a mix managed Erlang app wouldn’t recompile.

It’s rebar managed applications.

I think if you can manage to set up a pure Erlang project relying on rebar only which recompiles cached stuff on subsequent stages of the pipe, I’m sure the rebar people will be eager to help.

Are you caching both the deps and build folders? On circle ci we’re caching them both separately

      # restore deps cache
      - restore_cache:
          keys:
            # CI_CACHE_VERSION is used to manually bust the cache
            - messages-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}-{{ checksum "mix.lock" }}
            - messages-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}
            - messages-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-

      # restore build cache
      - restore_cache:
          keys:
            - messages-build-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}
            - messages-build-v9-{{ .Environment.CI_CACHE_VERSION }}-

Then steps related to compiling and running migrations. After that we can save the cache:

      # save deps cache
      - save_cache:
          key: my-app-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}-{{ checksum "mix.lock" }}
          paths: "deps"
      - save_cache:
          key: my-app-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}
          paths: "deps"
      - save_cache:
          key: my-app-deps-v9-{{ .Environment.CI_CACHE_VERSION }}-
          paths: "deps"

      # save build cache
      - save_cache:
          key: my-app-build-v9-{{ .Environment.CI_CACHE_VERSION }}-{{ .Branch }}
          paths: "_build"
      - save_cache:
          key: my-app-build-v9-{{ .Environment.CI_CACHE_VERSION }}-
          paths: "_build"

There’s multiple caches so that if for example the mix.lock changes then we can fallback to the next most recently used cache. Also the v9 is there so that when we make large changes we can cache bust everything and we can do something similar by changing the CI_CACHE_VERSION env variable (but we can change this without pushing a code update). A reason you’d need to bust the cache is when a library reads from application config during compilation (a great example is the Elixir mime type plug: https://hexdocs.pm/mime/MIME.html).

Here’s the official CircleCi elixir guide (although I don’t find it super helpful): https://circleci.com/docs/2.0/language-elixir/

I hope that helps!

1 Like

Thanks for the response axelson!

The config you posted is very close to EXACTLY what we are using :). As I stated above, this works for caching ELIXIR build files.

It does NOT work for caching the built ERLANG libs. I suspect this is because restoring a cache does not preserve the original file modification times - see mix compile docs and this circleci question.

If you watch your build logs, I think you will find that your tasks are rebuilding the erlang libs every time too.

I am trying to find a way to tell mix that it doesn’t need to recompile the erlang libs.

As I already said, you need to tell rebar not to recompile, as mix simply asks the dependencies manager if it needs to be recompiled.

Nobbz: Thanks for your reply! Any clues on how I can do that?

AFAIK you currently can’t, therefore I suggested to get in touch with the rebar team in How to cache erlang builds on CI?

Thanks NobbZ

I opened this issue on the rebar3 repo: https://github.com/erlang/rebar3/issues/1824

1 Like

@samphilipd were you eventually able to resolve this?

I ‘think’ if you just touch every compiled/beam file then it won’t recompile them due to how rebar works? I ‘think’? >.>

I tried:

touch _build/test/lib/*/ebin/*
touch _build/test/lib/*/.mix/*
touch _build/test/lib/*/include/*
touch _build/test/lib/*/consolidated/*
touch _build/test/lib/*/mix.rebar.config

Still recompiles everything, not sure if those are the files and if someone were able to solve. @axelson, did you found out?

1 Like

We don’t currently have any direct erlang dependencies so unfortunately I’m not that much help at the moment.

I see, but I guess the problem happens for indirect erlang deps too. Just mentioning…

Related: Are there people that have experience in doing the same thing (caching Elixir and/or Erlang dependencies so that only changed things are re-compiled) for the Travis CI?

Currently the Travis builds take almost four minutes, of which running the actual test suite takes only about 15 seconds.

This Travis CI runs each of the test suites in just over a minute. The linting and dialyzer tasks are ran as jobs after the tests pass, which really speeds things up.

1 Like

:wave:

Just ran into a somewhat similar issue (erlang deps didn’t seem to be cached) and in my case the problem was in not caching deps folder after running mix compile. This is important since rebar3 actually stores ebins in deps, and in _build mix only creates symlinks.

So, my approach before (which had the same problem as in OP):

# deps stage
- cache pull deps
- mix deps.get
- cache push deps

# compile stage
- cache pull deps, _build
- mix compile
- cache push _build

and my approach now:

# deps stage
- cache pull deps
- mix deps.get
- cache push deps

# compile stage
- cache pull deps, _build
- mix compile
- cache push deps, _build # <-- !!!
4 Likes