Skip to content

Commit

Permalink
Optimize CI for efficiency and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Dec 11, 2023
1 parent 7534b5e commit 49e1f79
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 35 deletions.
117 changes: 82 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "CI"
name: "🔬 Test"

on:
pull_request:
Expand All @@ -11,22 +11,64 @@ on:

jobs:
test:
name: "Test on ${{ matrix.config.os-name }}"
runs-on: "${{ matrix.config.runner }}"

strategy:
fail-fast: false
matrix:
config:
- os-name: "Linux"
runner: "ubuntu-latest"
test-label: "ci-test-linux"
- os-name: "macOS"
# macOS and Windows tests target only the upper and lower CPython versions.
interpreter:
- "CPython"
os:
- name: "macOS"
runner: "macos-latest"
test-label: "ci-test-macos"
- os-name: "Windows"
- name: "Windows"
interpreters: "CPython"
runner: "windows-latest"
test-label: "ci-test-windows"
fail-fast: false
cpythons:
# The goal is to ensure that each individual runner
# is paired with all Python versions simultaneously.
# This nested-list syntax accomplishes that goal.
- - "3.8"
- "3.12"
# These last two keys are placeholders to ensure consistency in matrix values.
cpython-beta:
- ""
pypys:
- []

# The Linux runner tests all CPython and PyPy versions.
# To reduce clock time, CPython and PyPy are tested separately.
include:
# CPython
- interpreter: "CPython"
os:
name: "Linux (CPython)"
runner: "ubuntu-latest"
cpythons:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
cpython-beta: "3.13"
pypys: []
# PyPy
- interpreter: "PyPy"
os:
name: "Linux (PyPy)"
runner: "ubuntu-latest"
cpythons: []
cpython-beta: ""
pypys:
- "3.8"
- "3.9"
- "3.10"
# NOTE: Tox only offers "best effort" support for PyPy.
# To prevent installing tox on a PyPy interpreter,
# a CPython version must be added to the end of the PyPy list.
tox-python: "3.12"

name: "${{ matrix.os.name }}"
runs-on: "${{ matrix.os.runner }}"

steps:
- name: "Checkout the repo"
Expand All @@ -36,36 +78,36 @@ jobs:
id: "setup-python"
uses: "actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236" # v4.7.1
with:
python-version: |
3.13
pypy-3.8
pypy-3.9
pypy-3.10
3.8
3.9
3.10
3.11
3.12
python-version: "${{
matrix.interpreter == 'PyPy'
&& format('pypy-{0}\n{1}', join(matrix.pypys, '\npypy-'), matrix.tox-python)
|| format('{0}\n{1}, matrix.cpython-beta, join(matrix.cpythons, '\n')
}}"
allow-prereleases: true

- name: "Determine month number"
shell: "bash"
run: |
date +'month-number=%m' >> $GITHUB_ENV
date +'%m' > month-number.txt
- name: "Determine venv path"
shell: "bash"
run: "echo 'venv-path=${{ matrix.config.os-name == 'Windows' && '.venv/Scripts' || '.venv/bin' }}' >> $GITHUB_ENV"
- name: "Detect Pythons"
uses: "kurtmckee/detect-pythons@cd2193638306e04e41ac36f0f9290f18680138ac" # v1.0.0

- name: "Restore cache"
id: "restore-cache"
uses: "actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84" # v3.3.2
with:
# The cache key includes the following to ensure it busts correctly:
#
# * All Python versions (detected by kurtmckee/detect-pythons, above)
# This ensures that .venv/ symlinks to Python interpreters work.
# * The tox configuration (tox.ini)
# * The pytest configurations (pyproject.toml)
# * Exact dependency versions (requirements/*.txt)
#
key: "test-os=${{ runner.os }}-interpreter-${{ matrix.interpreter }}-hash=${{ hashFiles('.python-identifiers', 'pyproject.toml', 'tox.ini', 'requirements/*.txt') }}"
path: |
.tox/
.venv/
key: "test-${{ matrix.config.runner }}-${{ hashFiles('pyproject.toml', 'tox.ini', 'month-number.txt') }}"
- name: "Determine venv path"
shell: "bash"
run: "echo 'venv-path=.venv/${{ runner.os == 'Windows' && 'Scripts' || 'bin' }}' >> $GITHUB_ENV"

- name: "Create virtual environment"
if: "steps.restore-cache.outputs.cache-hit == false"
Expand All @@ -74,8 +116,13 @@ jobs:
${{ env.venv-path }}/python -m pip install --upgrade pip setuptools wheel
${{ env.venv-path }}/python -m pip install tox
- name: "Run tests"
run: "${{ env.venv-path }}/tox run -m ${{ matrix.config.test-label }}"
- name: "Run tests (PyPy interpreters)"
if: "matrix.interpreter == 'PyPy'"
run: "${{ env.venv-path }}/tox run -e pypy${{ join(matrix.pypys, '-chardet,pypy') }}-chardet"

- name: "Run tests (CPython interpreters)"
if: "matrix.interpreter == 'CPython'"
run: "${{ env.venv-path }}/tox run -e py${{ join(matrix.cpythons, '-chardet,py') }}-chardet,py${{ matrix.cpython-beta }}-chardet"

lint:
name: "Lint"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Project development
-------------------

* Optimize CI for efficiency and speed.

0 comments on commit 49e1f79

Please sign in to comment.