.tests-matrix workflow (python-poetry/poetry)
The .tests-matrix workflow from python-poetry/poetry, explained and optimized by Latchkey.
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 .tests-matrix workflow from the python-poetry/poetry repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs.
on:
workflow_call:
inputs:
runner:
required: true
type: string
python-version:
required: true
type: string
run-mypy:
required: true
type: boolean
run-pytest:
required: true
type: boolean
run-pytest-export:
required: true
type: boolean
defaults:
run:
shell: bash
env:
PYTHONWARNDEFAULTENCODING: 'true'
jobs:
mypy:
name: mypy
runs-on: ${{ inputs.runner }}
if: inputs.run-mypy
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- uses: ./.github/actions/bootstrap-poetry
id: bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}
- uses: ./.github/actions/poetry-install
- uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: .mypy_cache
key: mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-
mypy-${{ runner.os }}-
- run: poetry run mypy
pytest:
name: pytest
runs-on: ${{ inputs.runner }}
if: inputs.run-pytest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}
- uses: ./.github/actions/poetry-install
with:
args: --with github-actions
- run: poetry run pytest --integration -v
env:
POETRY_TEST_INTEGRATION_GIT_USERNAME: ${{ github.actor }}
POETRY_TEST_INTEGRATION_GIT_PASSWORD: ${{ github.token }}
- run: git diff --exit-code --stat HEAD
pytest-export:
name: pytest (poetry-plugin-export)
runs-on: ${{ inputs.runner }}
if: inputs.run-pytest-export
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
path: poetry
- uses: ./poetry/.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}
- name: Get poetry-plugin-export version
run: |
PLUGIN_VERSION=$(curl -s https://pypi.org/pypi/poetry-plugin-export/json | jq -r ".info.version")
echo "Found version ${PLUGIN_VERSION}"
echo version=${PLUGIN_VERSION} >> $GITHUB_OUTPUT
id: poetry-plugin-export-version
- name: Check out poetry-plugin-export
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
path: poetry-plugin-export
repository: python-poetry/poetry-plugin-export
# use main for now because of poetry-core#826
# ref: refs/tags/${{ steps.poetry-plugin-export-version.outputs.version }}
- name: Use local poetry
working-directory: poetry-plugin-export
# Replace the python version to avoid conflicts
# if the plugin still supports a wider range than Poetry itself.
run: |
perl -pi -e 's/^requires-python =.*$/requires-python = "~='"${PYTHON_VERSION}"'"/' pyproject.toml
poetry remove --lock poetry-core # use whatever poetry uses
poetry add --lock --group dev ../poetry
env:
PYTHON_VERSION: ${{ inputs.python-version }}
- name: Install
working-directory: poetry-plugin-export
run: poetry install
- name: Run tests
working-directory: poetry-plugin-export
run: poetry run pytest -v
- name: Check for clean working tree
working-directory: poetry-plugin-export
run: |
git checkout -- pyproject.toml poetry.lock
git diff --exit-code --stat HEAD
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs. on: workflow_call: inputs: runner: required: true type: string python-version: required: true type: string run-mypy: required: true type: boolean run-pytest: required: true type: boolean run-pytest-export: required: true type: boolean defaults: run: shell: bash env: PYTHONWARNDEFAULTENCODING: 'true' jobs: mypy: timeout-minutes: 30 name: mypy runs-on: ${{ inputs.runner }} if: inputs.run-mypy steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: persist-credentials: false - uses: ./.github/actions/bootstrap-poetry id: bootstrap-poetry with: python-version: ${{ inputs.python-version }} - uses: ./.github/actions/poetry-install - uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 with: path: .mypy_cache key: mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }} restore-keys: | mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}- mypy-${{ runner.os }}- - run: poetry run mypy pytest: timeout-minutes: 30 name: pytest runs-on: ${{ inputs.runner }} if: inputs.run-pytest steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: persist-credentials: false - uses: ./.github/actions/bootstrap-poetry with: python-version: ${{ inputs.python-version }} - uses: ./.github/actions/poetry-install with: args: --with github-actions - run: poetry run pytest --integration -v env: POETRY_TEST_INTEGRATION_GIT_USERNAME: ${{ github.actor }} POETRY_TEST_INTEGRATION_GIT_PASSWORD: ${{ github.token }} - run: git diff --exit-code --stat HEAD pytest-export: timeout-minutes: 30 name: pytest (poetry-plugin-export) runs-on: ${{ inputs.runner }} if: inputs.run-pytest-export steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: persist-credentials: false path: poetry - uses: ./poetry/.github/actions/bootstrap-poetry with: python-version: ${{ inputs.python-version }} - name: Get poetry-plugin-export version run: | PLUGIN_VERSION=$(curl -s https://pypi.org/pypi/poetry-plugin-export/json | jq -r ".info.version") echo "Found version ${PLUGIN_VERSION}" echo version=${PLUGIN_VERSION} >> $GITHUB_OUTPUT id: poetry-plugin-export-version - name: Check out poetry-plugin-export uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: persist-credentials: false path: poetry-plugin-export repository: python-poetry/poetry-plugin-export # use main for now because of poetry-core#826 # ref: refs/tags/${{ steps.poetry-plugin-export-version.outputs.version }} - name: Use local poetry working-directory: poetry-plugin-export # Replace the python version to avoid conflicts # if the plugin still supports a wider range than Poetry itself. run: | perl -pi -e 's/^requires-python =.*$/requires-python = "~='"${PYTHON_VERSION}"'"/' pyproject.toml poetry remove --lock poetry-core # use whatever poetry uses poetry add --lock --group dev ../poetry env: PYTHON_VERSION: ${{ inputs.python-version }} - name: Install working-directory: poetry-plugin-export run: poetry install - name: Run tests working-directory: poetry-plugin-export run: poetry run pytest -v - name: Check for clean working tree working-directory: poetry-plugin-export run: | git checkout -- pyproject.toml poetry.lock git diff --exit-code --stat HEAD
What changed
- Add a job timeout so a hung step cannot burn hours of runner time.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Dependency installs
This workflow runs 3 jobs per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.