-
Notifications
You must be signed in to change notification settings - Fork 0
341 lines (305 loc) · 13 KB
/
python-release.yml
File metadata and controls
341 lines (305 loc) · 13 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
name: Python Release (Reusable)
on:
workflow_call:
inputs:
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
publish-testpypi:
type: boolean
required: false
default: false
publish-testpypi-on-release:
type: boolean
required: false
default: false
allow-production:
type: boolean
required: false
default: false
tag-prefix:
type: string
required: false
default: v
secrets:
TEST_PYPI_TOKEN:
required: false
PYPI_TOKEN:
required: false
RAPIDKIT_SIGN_KEY:
required: false
permissions:
contents: write
id-token: write
jobs:
build-and-test:
runs-on: ubuntu-latest
env:
DISTRIBUTION_TIER: ${{ inputs.distribution-tier }}
LIMITED_MODE: ${{ inputs.limited-mode }}
RAPIDKIT_NPM_DIR: rapidkit-npm
strategy:
matrix:
python-version:
- ${{ inputs.python-version }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: "1.8.3"
virtualenvs-in-project: true
virtualenvs-create: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: |
~/.cache/pypoetry
~/.cache/pip
.venv
key: poetry-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
poetry-${{ matrix.python-version }}-
- name: Install dependencies
run: poetry install --with dev --no-interaction
- name: 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: Build package
run: poetry build
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: dist-${{ matrix.python-version }}
path: dist/
sign-and-release:
runs-on: ubuntu-latest
needs: build-and-test
env:
DISTRIBUTION_TIER: ${{ inputs.distribution-tier }}
LIMITED_MODE: ${{ inputs.limited-mode }}
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: Download build artifacts
uses: actions/download-artifact@v4
with:
name: dist-${{ inputs.python-version }}
path: dist
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: "1.8.3"
virtualenvs-in-project: true
virtualenvs-create: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: |
~/.cache/pypoetry
~/.cache/pip
.venv
key: poetry-sign-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
poetry-sign-
- name: Install dependencies
run: poetry install --with dev --no-interaction
- name: Determine release version
id: version
run: |
VERSION=$(poetry version --short)
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Sign artifacts
env:
SIGN_KEY: ${{ secrets.RAPIDKIT_SIGN_KEY || '' }}
run: |
if [ -z "$SIGN_KEY" ]; then
echo "⚠️ No signing key available for this tier"
echo "ℹ️ Skipping signing"
else
echo "🔐 Signing would be performed here"
fi
- name: Generate modules.lock
run: poetry run python scripts/generate_modules_lock.py
- name: Compute aggregate version
id: agg
run: poetry run python scripts/compute_agg_version.py
- name: Set TAG env for downstream steps
id: tag
run: |
VERSION="${{ steps.version.outputs.version }}"
if [ -z "$VERSION" ]; then
VERSION="${{ steps.agg.outputs.agg_version }}"
fi
PREFIX="${{ inputs.tag-prefix }}"
# If this workflow is triggered from an existing tag, reuse it.
if [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then
TAG_VALUE="${GITHUB_REF_NAME}"
if [[ -z "$TAG_VALUE" ]]; then
echo "GITHUB_REF_NAME is empty; refusing to continue." >&2
exit 1
fi
if [[ "$TAG_VALUE" != "${PREFIX}"* ]]; then
echo "❌ Tag '$TAG_VALUE' does not start with expected prefix '$PREFIX'." >&2
exit 1
fi
EXPECTED_TAG="${PREFIX}${VERSION}"
if [[ "$TAG_VALUE" != "$EXPECTED_TAG" ]]; then
echo "❌ Tag/version mismatch: ref='$TAG_VALUE' but poetry version implies '$EXPECTED_TAG'." >&2
exit 1
fi
else
TAG_VALUE="${PREFIX}${VERSION}"
fi
echo "TAG=${TAG_VALUE}" >> $GITHUB_ENV
echo "tag=${TAG_VALUE}" >> "$GITHUB_OUTPUT"
- name: Create git tag
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
env:
TAG: ${{ steps.tag.outputs.tag }}
run: |
git config user.name "github-actions"
git config user.email "github-actions@users.noreply.github.com"
git tag -a "$TAG" -m "Release $TAG" || echo "⚠️ Tag exists"
git push origin "$TAG" || echo "⚠️ Tag already pushed"
- name: Generate release notes
run: |
prev_tag=$(git tag -l "${{ inputs.tag-prefix }}*" | sort -V | grep -v "$TAG" | tail -n1 || true)
if [ -n "$prev_tag" ]; then
git show "$prev_tag:modules.lock" > prev_modules.lock || echo '{}' > prev_modules.lock
else
echo '{}' > prev_modules.lock
fi
poetry run python scripts/generate_release_notes.py --prev prev_modules.lock --curr modules.lock > release_notes.md
- name: Create release summary
run: |
echo "📦 Modules versions:" > release_summary.txt
cat modules.lock >> release_summary.txt
if [ -f release_notes.md ]; then
echo -e "\n---\n📝 Detailed Release Notes" >> release_summary.txt
cat release_notes.md >> release_summary.txt
fi
if [ -f CHANGELOG.md ]; then
echo -e "\n---\n📜 Changelog" >> release_summary.txt
awk 'BEGIN{printing=0} /^## / {if(printing) exit; printing=1} {if(printing) print}' CHANGELOG.md >> release_summary.txt
fi
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.tag.outputs.tag }}
name: "Release ${{ steps.tag.outputs.tag }}"
body_path: release_summary.txt
files: |
dist/*
- name: "Guard: prevent production publish of pre-release tags"
if: ${{ inputs.allow-production }}
shell: bash
run: |
set -euo pipefail
tag="${TAG:-}"
if [ -z "$tag" ]; then
echo "TAG is empty; refusing to publish." >&2
exit 1
fi
version="${tag#${{ inputs.tag-prefix }}}"
if echo "$version" | grep -Eq '(a|b|rc|dev)[0-9]+'; then
echo "❌ Refusing production publish for pre-release tag: $tag" >&2
exit 1
fi
- name: Publish to TestPyPI (token)
if: ${{ inputs['publish-testpypi'] && (!inputs['publish-testpypi-on-release'] || github.event_name == 'release') && env.TEST_PYPI_TOKEN != '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
repository-url: https://test.pypi.org/legacy/
user: __token__
password: ${{ env.TEST_PYPI_TOKEN }}
- name: Publish to TestPyPI (trusted publishing)
if: ${{ inputs['publish-testpypi'] && (!inputs['publish-testpypi-on-release'] || github.event_name == 'release') && env.TEST_PYPI_TOKEN == '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
repository-url: https://test.pypi.org/legacy/
- name: Smoke test install from TestPyPI (no deps)
if: ${{ inputs['publish-testpypi'] }}
shell: bash
run: |
set -euo pipefail
version="${TAG#${{ inputs.tag-prefix }}}"
python -m venv testpypi_env
source testpypi_env/bin/activate
python -m pip install --upgrade pip
python -m pip install --no-deps \
--index-url https://test.pypi.org/simple \
--extra-index-url https://pypi.org/simple \
"rapidkit-core==${version}"
rapidkit --help
- name: Publish to PyPI (token)
if: ${{ inputs.allow-production && env.PYPI_TOKEN != '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
user: __token__
password: ${{ env.PYPI_TOKEN }}
- name: Publish to PyPI (trusted publishing)
if: ${{ inputs.allow-production && env.PYPI_TOKEN == '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/