Skip to content

Commit

Permalink
ci/tests: DRY matrix with reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Mar 25, 2024
1 parent 1c6c1d9 commit eb78cdd
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 112 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/.tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs.
name: .tests

on:
workflow_call:
inputs:
os-name:
required: true
type: string
os-image:
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

jobs:
mypy:
name: mypy
runs-on: ${{ inputs.os-image }}
if: inputs.run-mypy
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync

- run: poetry env info

- run: poetry show

- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ inputs.os-image }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
mypy-${{ inputs.os-image }}-${{ env.pythonLocation }}-
mypy-${{ inputs.os-image }}-
- run: poetry run mypy

pytest:
name: pytest
runs-on: ${{ inputs.os-image }}
if: inputs.run-pytest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- 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.os-image }}
if: inputs.run-pytest-export
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ inputs.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- id: poetry-plugin-export
run: echo version=$(poetry run pip list --format json | jq -r '.[] | select(.name == "poetry-plugin-export").version') >> $GITHUB_OUTPUT

- uses: actions/checkout@v4
with:
path: poetry-plugin-export
repository: python-poetry/poetry-plugin-export
ref: refs/tags/${{ steps.poetry-plugin-export.outputs.version }}

- run: poetry run -C .. pytest -v
working-directory: ./poetry-plugin-export

- run: git -C poetry-plugin-export diff --exit-code --stat HEAD
126 changes: 14 additions & 112 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
filters: |
workflow: &workflow
- '.github/workflows/tests.yaml'
- '.github/workflows/.tests.yaml'
project: &project
- *workflow
- 'poetry.lock'
Expand Down Expand Up @@ -77,54 +78,11 @@ jobs:

- run: git diff --exit-code --stat HEAD tests/repositories/fixtures/pypi.org

mypy:
name: mypy [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.mypy == 'true'
tests-matrix:
name: ${{ matrix.os.name }} (Python ${{ matrix.python-version }})
needs:
- changes
- lockfile
strategy:
matrix:
os:
- name: Ubuntu
image: ubuntu-22.04
- name: macOS
image: macos-12
- name: Windows
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync

- run: poetry env info

- run: poetry show

- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ matrix.os.image }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
restore-keys: |
mypy-${{ inputs.os-image }}-${{ env.pythonLocation }}-
mypy-${{ inputs.os-image }}-
- run: poetry run mypy

pytest:
name: pytest [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.pytest == 'true'
needs:
- changes
- lockfile
strategy:
matrix:
os:
Expand All @@ -136,70 +94,16 @@ jobs:
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- 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) [${{ matrix.os.name }} / Python ${{ matrix.python-version }}]
runs-on: ${{ matrix.os.image }}
if: needs.changes.outputs.pytest-export == 'true'
needs:
- changes
- lockfile
strategy:
matrix:
os:
- name: Ubuntu
image: ubuntu-22.04
- name: macOS
image: macos-12
- name: Windows
image: windows-2022
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/bootstrap-poetry
with:
python-version: ${{ matrix.python-version }}

- run: poetry install --sync --with github-actions

- run: poetry env info

- run: poetry show

- id: poetry-plugin-export
run: echo version=$(poetry run pip list --format json | jq -r '.[] | select(.name == "poetry-plugin-export").version') >> $GITHUB_OUTPUT

- uses: actions/checkout@v4
with:
path: poetry-plugin-export
repository: python-poetry/poetry-plugin-export
ref: refs/tags/${{ steps.poetry-plugin-export.outputs.version }}

- run: poetry run -C .. pytest -v
working-directory: ./poetry-plugin-export

- run: git -C poetry-plugin-export diff --exit-code --stat HEAD
# Use this matrix with multiple jobs defined in a reusable workflow:
uses: ./.github/workflows/.tests.yaml
with:
os-name: ${{ matrix.os.name }}
os-image: ${{ matrix.os.image }}
python-version: ${{ matrix.python-version }}
run-mypy: ${{ needs.changes.outputs.mypy == 'true' }}
run-pytest: ${{ needs.changes.outputs.pytest == 'true' }}
run-pytest-export: ${{ needs.changes.outputs.pytest-export == 'true' }}
secrets: inherit

status:
name: Status
Expand All @@ -208,8 +112,6 @@ jobs:
needs:
- lockfile
- fixtures-pypi
- mypy
- pytest
- pytest-export
- tests-matrix
steps:
- run: ${{ (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) && 'false' || 'true' }}

0 comments on commit eb78cdd

Please sign in to comment.