Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ericbn/2e4ffdd3b0cc602850b01a8694292c8e to your computer and use it in GitHub Desktop.
Save ericbn/2e4ffdd3b0cc602850b01a8694292c8e to your computer and use it in GitHub Desktop.
GitHub Actions: Faster Python runs with cached virtual environments
# See https://adamj.eu/tech/2023/11/02/github-actions-faster-python-virtual-environments/
# ...
jobs:
example:
# ...
steps:
# ...
- uses: actions/setup-python@v5
id: setup-python
with:
python-version: '3.12'
- uses: actions/cache/restore@v4
id: cache-venv-restore
with:
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('requirements.txt') }}
path: .venv
- name: Set up virtualenv
run: |
[[ ! -d .venv ]] && python -m venv .venv
echo "VIRTUAL_ENV=${PWD}/.venv" >> ${GITHUB_ENV} && \
echo "${PWD}/.venv/bin" >> ${GITHUB_PATH}
- name: Install dependencies
if: steps.cache-venv-restore.outputs.cache-hit != 'true'
run: python -m pip install -r requirements.txt
- uses: actions/cache/save@v4
if: steps.cache-venv-restore.outputs.cache-hit != 'true'
with:
key: ${{ steps.cache-venv-restore.outputs.cache-primary-key }}
path: .venv
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment