To cache the actual compiled dependencies as opposed to caching the source tarballs or ccaching object files, adding the respective packages' Cellar
and opt
directories to the cache and using an appropriate before_install check seems to work fine.
You could also add all of /usr/local/Cellar/
and /usr/local/opt/
, but that would add all installed homebrew packages instead of only the ones you need.
Example from a project that depends on openssl, libevent and check:
cache:
directories:
- /usr/local/Cellar/openssl
- /usr/local/opt/openssl
- /usr/local/Cellar/libevent
- /usr/local/opt/libevent
- /usr/local/Cellar/check
- /usr/local/opt/check
before_install:
- test -d /usr/local/opt/openssl/lib || { rmdir /usr/local/opt/openssl; brew install openssl; }
- test -d /usr/local/opt/libevent/lib || { rmdir /usr/local/opt/libevent; brew install libevent; }
- test -d /usr/local/opt/check/lib || { rmdir /usr/local/opt/check; brew install check; }
The rmdir
is needed because TravisCI creates the cached directories if they don't exist, and brew install
fails if /usr/local/opt/$package
is a directory (as opposed to a symlink to a specific installed version in Cellar). For the same reason, test
tests for a subdirectory, not the main package directory.
Note that this approach requires your own project to be able to pick up the dependencies installed in /usr/local/opt
.