Skip to content

Instantly share code, notes, and snippets.

@pftg
Created January 13, 2018 21:02
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pftg/92cb374182ffec19c37352c435fb3684 to your computer and use it in GitHub Desktop.
Save pftg/92cb374182ffec19c37352c435fb3684 to your computer and use it in GitHub Desktop.
Pre-compile Assets and Cache for CircleCI
- restore_cache:
keys:
- v1-asset-cache-{{ arch }}-{{ .Branch }}
- v1-asset-cache-
- run: bundle exec rake assets:precompile
- save_cache:
key: v1-asset-cache-{{ arch }}-{{ .Branch }}-{{ epoch }}
paths:
- public/assets
- tmp/cache/assets/sprockets
@akostadinov
Copy link

akostadinov commented Sep 28, 2021

why save_cache has added epoch? Wouldn't this break restore?

@pftg
Copy link
Author

pftg commented Sep 29, 2021

@akostadinov that's hack is required in order to update cache only with the latest version. On restore will be used v1-asset-cache-{{ arch }}-{{ .Branch }} key, so it will not break.

@akostadinov
Copy link

akostadinov commented Sep 29, 2021

I changed my whole point from using BuildNum to using Revision so I heavily edit my post below.

First of all, thanks a lot for the example.

How about v1-asset-cache-{{ .Branch }}-{{ .Revision }}?

In this way cache will have exact match between workflows of same revision. You get a new cache for every change in repo instead of every build which seems exhaustive. Then v1-asset-cache- will match newest cache anyway regardless of it's key suffix.

Also for assets my understanding is that they are arch independent, so arch appears to be a needless constraint. Let me know if I'm missing something!

Another tuning I did is make the command bundle exec rake assets:precompile assets:clean by example of https://gist.github.com/ismell/a05035f441465e57cf52 to avoid cache bloat over time.

btw I had to add also webpacker stuff so my paths looks like this (note that -test suffix is because of the used environment test):

    paths:
      - public/assets
      - public/packs-test
      - tmp/cache/assets
      - tmp/cache/webpacker

@pftg
Copy link
Author

pftg commented Sep 29, 2021

yep, BuildNum also works, any incremental counter. arch is from the CircleCI default example.

this is an old snippet, some updates you can find here https://github.com/jetthoughts/jt_tools/blob/master/lib/install/.circleci/config.yml

I extracted all such tips into a rails app generator

you can cache even bootsnap and other stuff

@pftg
Copy link
Author

pftg commented Sep 29, 2021

Oh, I remember, why I moved to SHA1, in order to not run assets precompilation on re-run

@akostadinov
Copy link

akostadinov commented Sep 29, 2021

Good to know, thanks. My example above with branch-revision should do mostly the same. At least I observe that on re-run it catch the latest branch cache.

update: I ended up hashing all files which may cause assets change and do exact match. Better not to reuse partial cache to avoid bloat and unexpected issues for little if any perf gain. Hashing with the following command git ls-tree HEAD $(<.circleci/asset_paths) > tmp/assets_related_checksums, then using key v1-asset-cache-{{ checksum "tmp/assets_related_checksums" }}.

For full PR you can see 3scale/porta#2630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment