unit-tests workflow (dot-agent/nextpy)
The unit-tests workflow from dot-agent/nextpy, explained and optimized by Latchkey.
A
CI health: A - excellent
Run this on Latchkey for self-healing, caching, and up to 58% lower cost.
Grade your own workflow free or run it on Latchkey →What it does
This is the unit-tests workflow from the dot-agent/nextpy repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
workflow (.yml)
name: unit-tests
on:
push:
branches: [ "main" ]
paths-ignore:
- '**/*.md'
pull_request:
branches: [ "main" ]
paths-ignore:
- '**/*.md'
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
unit-tests:
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.8.18", "3.9.18", "3.10.13", "3.11.5"]
# Windows is a bit behind on Python version availability in Github
exclude:
- os: windows-latest
python-version: "3.10.13"
- os: windows-latest
python-version: "3.9.18"
- os: windows-latest
python-version: "3.8.18"
include:
- os: windows-latest
python-version: "3.10.11"
- os: windows-latest
python-version: "3.9.13"
- os: windows-latest
python-version: "3.8.10"
runs-on: ${{ matrix.os }}
# Service containers to run with `runner-job`
services:
# Label used to access the service container
redis:
image: ${{ matrix.os == 'ubuntu-latest' && 'redis' || '' }}
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6379:6379
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup_build_env
with:
python-version: ${{ matrix.python-version }}
run-poetry-install: true
create-venv-at-path: .venv
- name: Run unit tests
run: |
export PYTHONUNBUFFERED=1
poetry run pytest tests --cov --no-cov-on-fail --cov-report=
- name: Run unit tests w/ redis
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
export PYTHONUNBUFFERED=1
export REDIS_URL=redis://localhost:6379
poetry run pytest tests --cov --no-cov-on-fail --cov-report=
- run: poetry run coverage html
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
name: unit-tests on: push: branches: [ "main" ] paths-ignore: - '**/*.md' pull_request: branches: [ "main" ] paths-ignore: - '**/*.md' permissions: contents: read defaults: run: shell: bash concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: unit-tests: timeout-minutes: 30 strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] python-version: ["3.8.18", "3.9.18", "3.10.13", "3.11.5"] # Windows is a bit behind on Python version availability in Github exclude: - os: windows-latest python-version: "3.10.13" - os: windows-latest python-version: "3.9.18" - os: windows-latest python-version: "3.8.18" include: - os: windows-latest python-version: "3.10.11" - os: windows-latest python-version: "3.9.13" - os: windows-latest python-version: "3.8.10" runs-on: ${{ matrix.os }} # Service containers to run with `runner-job` services: # Label used to access the service container redis: image: ${{ matrix.os == 'ubuntu-latest' && 'redis' || '' }} # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps port 6379 on service container to the host - 6379:6379 steps: - uses: actions/checkout@v4 - uses: ./.github/actions/setup_build_env with: python-version: ${{ matrix.python-version }} run-poetry-install: true create-venv-at-path: .venv - name: Run unit tests run: | export PYTHONUNBUFFERED=1 poetry run pytest tests --cov --no-cov-on-fail --cov-report= - name: Run unit tests w/ redis if: ${{ matrix.os == 'ubuntu-latest' }} run: | export PYTHONUNBUFFERED=1 export REDIS_URL=redis://localhost:6379 poetry run pytest tests --cov --no-cov-on-fail --cov-report= - run: poetry run coverage html
What changed
- Cancel superseded runs when a branch or PR gets a newer push.
This workflow runs 1 job (12 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.