Skip to content

Commit

Permalink
CI: Cache virtual environments
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee authored and Ned Batchelder committed Jul 21, 2023
1 parent 7727ac5 commit 90064b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
uses: "actions/checkout@v3"

- name: "Set up Python"
id: "setup-python"
uses: "actions/setup-python@v4"
with:
# The last listed Python version is the default.
Expand All @@ -47,13 +48,26 @@ jobs:
3.9
3.10
3.11
cache: pip
cache-dependency-path: 'requirements/*.txt'
- name: "Restore cache"
id: "restore-cache"
uses: "actions/cache@v3"
with:
path: |
.tox/
.venv/
key: "cache-python-${{ steps.setup-python.outputs.python-version }}-os-${{ runner.os }}-hash-${{ hashFiles('tox.ini', 'requirements/*.txt') }}"

- name: "Identify venv path"
shell: "bash"
run: "echo 'venv-path=${{ runner.os == 'Windows' && '.venv/Scripts' || '.venv/bin' }}' >> $GITHUB_ENV"

- name: "Install dependencies"
if: "steps.restore-cache.outputs.cache-hit == false"
run: |
python -m pip install -U setuptools
python -m pip install -r requirements/tox.txt
python -m venv .venv
${{ env.venv-path }}/python -m pip install -U setuptools
${{ env.venv-path }}/python -m pip install -r requirements/tox.txt
- name: "Install pandoc on Linux"
# sudo apt-get pandoc: will install a version from 2018!
Expand All @@ -74,7 +88,7 @@ jobs:
- name: "Run tox"
run: |
python -m tox -m ci-tests
${{ env.venv-path }}/python -m tox -m ci-tests
- name: "Upload coverage data"
uses: actions/upload-artifact@v3
Expand Down
12 changes: 12 additions & 0 deletions changelog.d/20230206_072342_kurtmckee_reduce_ci_usage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Changed
.......

- Create and cache a virtual environment in CI, as well as the tox environments.

With a cache hit, this decreases CI total usage from ~15 minutes to ~10 minutes.
It decreases CI duration from ~7 minutes to ~5 minutes.

The cache keys hash ``requirements/*.txt`` and ``tox.ini``.
GitHub caches are branch-specific, but if no cache is found for the current branch,
the cache for the ``main`` branch will then be checked.
It is anticipated that most branches will therefore have a cache hit.

0 comments on commit 90064b8

Please sign in to comment.