-
Notifications
You must be signed in to change notification settings - Fork 0
142 lines (131 loc) · 4.44 KB
/
Copy pathpython-ci.yml
File metadata and controls
142 lines (131 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Python CI (reusable)
# Generic lint + typecheck + test workflow for Python projects. Uses uv for
# fast installs when a `pyproject.toml` is present; falls back to pip with
# `requirements.txt` otherwise. Each step is opt-out via inputs.
#
# Lint: ruff (fast, single-binary, defaults to PEP 8 + pyflakes + isort).
# Typecheck: pyright (faster than mypy for most repos; consumer can swap).
# Test: pytest.
on:
workflow_call:
inputs:
python-versions:
description: 'JSON array of Python versions to test against, e.g. `["3.11","3.12"]`.'
type: string
default: '["3.12"]'
os-matrix:
type: string
default: '["ubuntu-latest"]'
working-directory:
type: string
default: "."
use-uv:
description: "Use uv for installs (requires pyproject.toml). Falls back to pip if false or no pyproject."
type: boolean
default: true
install-command:
description: 'Override install command. Empty = auto-detect (uv sync or pip install -e ".[dev]" / -r requirements.txt).'
type: string
default: ""
run-lint:
type: boolean
default: true
lint-command:
type: string
default: "ruff check ."
run-format-check:
type: boolean
default: true
format-check-command:
type: string
default: "ruff format --check ."
run-typecheck:
type: boolean
default: true
typecheck-command:
type: string
default: "pyright"
run-tests:
type: boolean
default: true
test-command:
type: string
default: "pytest"
jobs:
ci:
name: ${{ matrix.os }} / py ${{ matrix.python }}
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
strategy:
fail-fast: false
matrix:
python: ${{ fromJSON(inputs.python-versions) }}
os: ${{ fromJSON(inputs.os-matrix) }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python }}
- name: Install uv
if: inputs.use-uv
uses: astral-sh/setup-uv@v7
- name: Install dependencies
shell: bash
run: |
if [ -n "${{ inputs.install-command }}" ]; then
# The ':' no-op keeps this branch a valid bash body even when the
# interpolated command is empty — an empty install-command otherwise
# renders `then` + blank line + `elif`, a parse error that failed the
# step (and every consumer's python lane) before running anything (#108).
:
${{ inputs.install-command }}
elif [ "${{ inputs.use-uv }}" = "true" ] && [ -f pyproject.toml ]; then
uv sync --all-extras --dev
elif [ -f pyproject.toml ]; then
python -m pip install --upgrade pip
pip install -e ".[dev]" || pip install -e .
elif [ -f requirements.txt ]; then
python -m pip install --upgrade pip
pip install -r requirements.txt
[ -f requirements-dev.txt ] && pip install -r requirements-dev.txt || true
else
echo "No pyproject.toml or requirements.txt found — nothing to install."
fi
- name: Lint (ruff check)
if: inputs.run-lint
shell: bash
run: |
if [ "${{ inputs.use-uv }}" = "true" ] && [ -f pyproject.toml ]; then
uv run ${{ inputs.lint-command }}
else
${{ inputs.lint-command }}
fi
- name: Format check (ruff format)
if: inputs.run-format-check
shell: bash
run: |
if [ "${{ inputs.use-uv }}" = "true" ] && [ -f pyproject.toml ]; then
uv run ${{ inputs.format-check-command }}
else
${{ inputs.format-check-command }}
fi
- name: Typecheck
if: inputs.run-typecheck
shell: bash
run: |
if [ "${{ inputs.use-uv }}" = "true" ] && [ -f pyproject.toml ]; then
uv run ${{ inputs.typecheck-command }}
else
${{ inputs.typecheck-command }}
fi
- name: Tests
if: inputs.run-tests
shell: bash
run: |
if [ "${{ inputs.use-uv }}" = "true" ] && [ -f pyproject.toml ]; then
uv run ${{ inputs.test-command }}
else
${{ inputs.test-command }}
fi