GitHub Actions is great as a CI/CD platform. However, to be efficient, they need to leverage some optimization techniques, such as caching or running tasks in parallel. In this note, I am sharing some thoughts on how to use cache effectively, with respect to multiple paths and sudo-installed APT packages. The discussion will touch on a few non-trivial aspects that, in my opinion, are not well-explained in other web materials.
My use case was simple: speed-up building a university course in Sphinx (to be hosted on GitHub pages). Installing Sphinx dependencies required multiple Python and APT downloads, which took quite a long. The caching solution indeed fixed a problem, and here are the key takeaways:
- Use the general cache action actions/cache@v3 rather than Python-specific cache
- Use conditional logic and the cache-hit flag to not trigger installations if the cache can be retrieved. This is better than the commonly-used pattern of triggering installation and letting it recognize the cache itself.
- Best to link cache to user-controlled space, to avoid problems with privileges. In my case, this fixed issues with caching sudo-installed packages.
This repository demonstrates the working solution. And this is how the job looks like:
name: docs
on: [push, pull_request, workflow_dispatch]
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: prepare virtual environment
run: |
python -m venv .venv
- name: cache dependencies
id: cache_deps
uses: actions/cache@v3
env:
cache-name: cache-dependencies
with:
path: |
.venv
.cache/apt
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('.github/workflows/*') }}
- name: Install python dependencies
if: ${{ steps.cache_deps.outputs.cache-hit != 'true' }}
run: |
source .venv/bin/activate
pip install jupyter-book
pip install sphinxcontrib-plantuml
- name: Install sudo dependencies
run: |
sudo apt-get -o dir::cache::archives=".cache/apt" install plantuml
- name: Compile Docs
run: |
source .venv/bin/activate
jupyter-book build docs
- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: ./docs/_build/html