-
Notifications
You must be signed in to change notification settings - Fork 0
312 lines (274 loc) · 12.2 KB
/
python-publish.yml
File metadata and controls
312 lines (274 loc) · 12.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
name: Python Publish (Reusable)
on:
workflow_call:
inputs:
environment:
type: string
required: false
default: production
python-version:
type: string
required: false
default: "3.10.14"
distribution-tier:
type: string
required: false
default: community
limited-mode:
type: boolean
required: false
default: false
run-tests:
type: boolean
required: false
default: true
build-docs:
type: boolean
required: false
default: true
allow-production:
type: boolean
required: false
default: false
allow-prerelease-production:
type: boolean
required: false
default: false
secrets:
TEST_PYPI_TOKEN:
required: false
PYPI_TOKEN:
required: false
permissions:
contents: read
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
env:
DISTRIBUTION_TIER: ${{ inputs.distribution-tier }}
LIMITED_MODE: ${{ inputs.limited-mode }}
TARGET_ENVIRONMENT: ${{ inputs.environment }}
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
TEST_PYPI_TOKEN: ${{ secrets.TEST_PYPI_TOKEN }}
RAPIDKIT_NPM_DIR: rapidkit-npm
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: "Guard: tag must match pyproject version"
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
shell: bash
run: |
set -euo pipefail
ref_name="${GITHUB_REF_NAME:-}"
if [[ -z "$ref_name" ]]; then
echo "GITHUB_REF_NAME is empty; refusing to continue." >&2
exit 1
fi
actual_version=$(awk -F '"' 'BEGIN{in_poetry=0} /^\[tool\.poetry\]$/{in_poetry=1;next} in_poetry && /^\[/{in_poetry=0} in_poetry && /^version =/{print $2; exit}' pyproject.toml)
if [[ -z "${actual_version:-}" ]]; then
echo "Unable to read [tool.poetry].version from pyproject.toml" >&2
exit 1
fi
expected_version=""
if [[ "$ref_name" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)-rc\.([0-9]+)$ ]]; then
expected_version="${BASH_REMATCH[1]}rc${BASH_REMATCH[2]}"
elif [[ "$ref_name" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
expected_version="${BASH_REMATCH[1]}"
else
echo "Tag format not enforced for: $ref_name (skipping version guard)" >&2
exit 0
fi
if [[ "$actual_version" != "$expected_version" ]]; then
echo "❌ Version mismatch" >&2
echo " tag: $ref_name" >&2
echo " expected: $expected_version" >&2
echo " pyproject: $actual_version" >&2
echo "" >&2
echo "Fix: ensure the RC tag workflow bumps+commits pyproject before tagging." >&2
exit 1
fi
echo "✅ Version guard passed: $actual_version matches $ref_name"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: "2.2.1"
virtualenvs-in-project: true
virtualenvs-create: true
- name: Cache Poetry downloads
uses: actions/cache@v4
with:
path: |
~/.cache/pypoetry
~/.cache/pip
.venv
key: poetry-publish-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
poetry-publish-${{ runner.os }}-
- name: Install dependencies (with dev)
run: poetry install --with dev --no-interaction
- name: Validate community documentation bundle
if: ${{ inputs.build-docs }}
shell: bash
run: |
set -euo pipefail
if [ -f Makefile ] && grep -Eq '^community-docs:' Makefile; then
make community-docs
exit 0
fi
if [ -f Makefile ] && make -n community-docs >/dev/null 2>&1; then
make community-docs
exit 0
fi
echo "Skipping community-docs: Makefile target not available in this repository." >&2
- name: Upload community README artifact
if: ${{ inputs.build-docs && always() }}
uses: actions/upload-artifact@v4
with:
name: community-readme
path: build/community/README.md
if-no-files-found: warn
- name: Run tests
if: ${{ inputs.run-tests }}
run: poetry run pytest -q --maxfail=1 --disable-warnings
- name: Detect npm workspace
id: npm_detect
run: |
if [ -d "${{ env.RAPIDKIT_NPM_DIR }}" ]; then
echo "found=true" >> "$GITHUB_OUTPUT"
else
echo "found=false" >> "$GITHUB_OUTPUT"
fi
- name: Setup Node.js
if: steps.npm_detect.outputs.found == 'true'
uses: actions/setup-node@v4
with:
node-version: "20"
cache: npm
cache-dependency-path: ${{ env.RAPIDKIT_NPM_DIR }}/package-lock.json
- name: Install npm dependencies
if: steps.npm_detect.outputs.found == 'true'
working-directory: ${{ env.RAPIDKIT_NPM_DIR }}
run: npm ci
- name: Check contracts sync (Core ↔ NPM)
if: steps.npm_detect.outputs.found == 'true'
working-directory: ${{ env.RAPIDKIT_NPM_DIR }}
env:
RAPIDKIT_CORE_SCHEMA_PATH: ${{ github.workspace }}/docs/contracts/rapidkit-cli-contracts.json
run: |
if [ -f "${RAPIDKIT_CORE_SCHEMA_PATH}" ]; then
npm run check:contracts
else
echo "Skipping contract sync check (schema missing)"
fi
- name: Run drift guard (Core ↔ NPM contracts)
if: steps.npm_detect.outputs.found == 'true'
working-directory: ${{ env.RAPIDKIT_NPM_DIR }}
env:
RAPIDKIT_DRIFT_GUARD: "1"
RAPIDKIT_DRIFT_GUARD_MODE: "monorepo"
run: npm run test:drift
- name: Clean previous build outputs
shell: bash
run: |
set -euo pipefail
rm -rf dist build *.egg-info
- name: Build and verify package
run: |
poetry build
poetry run python -m pip install --upgrade twine
poetry run twine check dist/*
- name: Generate checksums
shell: bash
run: |
set -euo pipefail
# NOTE: Do not write SHA256SUMS.txt into dist/.
# pypa/gh-action-pypi-publish treats unknown files in packages-dir as errors.
find dist -maxdepth 1 -type f \( -name '*.whl' -o -name '*.tar.gz' \) -print0 \
| sort -z \
| xargs -0 sha256sum > SHA256SUMS.txt
- name: Upload dist artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: dist
path: |
dist/*
SHA256SUMS.txt
if-no-files-found: error
- name: "Guard: prevent production publish of pre-release tags"
if: ${{ inputs.allow-production }}
shell: bash
run: |
set -euo pipefail
ref_name="${GITHUB_REF_NAME:-}"
if [ -z "$ref_name" ]; then
echo "GITHUB_REF_NAME is empty; refusing to publish." >&2
exit 1
fi
version="${ref_name#v}"
if [[ "$TARGET_ENVIRONMENT" == "production" ]] && echo "$version" | grep -Eq '(a|b|rc|dev)[0-9]+'; then
if [[ "${{ inputs.allow-prerelease-production }}" != "true" ]]; then
echo "❌ Refusing production publish for pre-release tag: $ref_name" >&2
echo " Set allow-prerelease-production=true for the RC pipeline." >&2
exit 1
fi
echo "✅ Guard passed: prerelease production publish explicitly allowed"
exit 0
fi
echo "✅ Guard passed: not a production pre-release"
- name: Publish to TestPyPI (token)
if: ${{ inputs.environment == 'test' && env.TEST_PYPI_TOKEN != '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
user: __token__
password: ${{ env.TEST_PYPI_TOKEN }}
verbose: true
skip-existing: true
- name: Publish to TestPyPI (trusted publishing)
if: ${{ inputs.environment == 'test' && env.TEST_PYPI_TOKEN == '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
verbose: true
skip-existing: true
- name: Publish to PyPI (token)
if: ${{ inputs.environment == 'production' && inputs.allow-production && env.PYPI_TOKEN != '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ env.PYPI_TOKEN }}
- name: Publish to PyPI (trusted publishing)
if: ${{ inputs.environment == 'production' && inputs.allow-production && env.PYPI_TOKEN == '' }}
uses: pypa/gh-action-pypi-publish@release/v1
- name: Fail for disallowed production publish
if: ${{ inputs.environment == 'production' && !inputs.allow-production }}
run: |
echo "❌ Production PyPI publish is disabled for this tier"
exit 1
- name: Verify install from built wheel (end-user)
shell: bash
run: |
set -euo pipefail
pkg=$(awk -F '"' 'BEGIN{in_poetry=0} /^\[tool\.poetry\]$/{in_poetry=1;next} in_poetry && /^\[/{in_poetry=0} in_poetry && /^name =/{print $2; exit}' pyproject.toml)
if [ -z "${pkg:-}" ]; then
echo "Unable to read [tool.poetry].name from pyproject.toml" >&2
exit 1
fi
python -m venv test_env
source test_env/bin/activate
python -m pip install --upgrade pip
constraints_args=()
if [ -f constraints.txt ]; then
constraints_args=(-c constraints.txt)
fi
# Install the just-built wheel from dist/, but resolve dependencies like a real user (PyPI).
python -m pip install --only-binary "$pkg" --find-links dist/ "${constraints_args[@]}" "$pkg"
rapidkit --help
rapidkit list
echo "✅ End-user installation smoke test passed"