-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathpython-build-and-release-package.yml
More file actions
438 lines (408 loc) · 17.2 KB
/
python-build-and-release-package.yml
File metadata and controls
438 lines (408 loc) · 17.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
name: Python - build and release package
on:
workflow_dispatch:
push:
branches:
- "main"
tags:
# Note: These need to match what specified in env.[TEST_]RELEASE_TAG_PREFIX below.
- "python-v*"
- "python-test-v*"
pull_request:
paths:
- "python/**"
- "rust/**"
- "tests_data/**"
- ".github/workflows/python-build-and-release-package.yml"
schedule:
- cron: "12 3 * * 4" # Run everything once per week.
- cron: "12 3 * * 1" # Refresh the cache an additional time.
release:
types: [created]
permissions:
contents: read
env:
# Trigger for publishing to pypi and testpypi (and for pre-release checks
# enforcement).
RELEASE_TAG_PREFIX: "python-v"
TEST_RELEASE_TAG_PREFIX: "python-test-v"
UV_VERSION: "0.9.5"
jobs:
# This job acts as a gatekeeper for releases, which are triggered by a tag
# push. It performs critical pre-release checks. These checks are skipped for
# non-release pushes.
pre-release-checks:
name: Pre-release checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Setup python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # pin@v5
with:
python-version: "3.12"
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
- name: Check package for release
env:
IS_RELEASE_TAG: ${{ github.event_name == 'push' && github.ref_type == 'tag' && (startsWith(github.ref_name, env.RELEASE_TAG_PREFIX) || startsWith(github.ref_name, env.TEST_RELEASE_TAG_PREFIX)) }}
run: |
if [[ "${{ env.IS_RELEASE_TAG }}" == 'true' ]]; then
FULL_TAG_REF="${{ github.ref }}"
TAG_NAME="${{ github.ref_name }}"
if [[ "${TAG_NAME}" == "${{ env.RELEASE_TAG_PREFIX }}"* ]]; then
PREFIX_TO_REMOVE="refs/tags/${{ env.RELEASE_TAG_PREFIX }}"
else
PREFIX_TO_REMOVE="refs/tags/${{ env.TEST_RELEASE_TAG_PREFIX }}"
fi
TAG_VERSION="${FULL_TAG_REF#${PREFIX_TO_REMOVE}}"
CHECKER_OPTIONS="--expected-version ${TAG_VERSION}"
else
CHECKER_OPTIONS="--report-only"
fi
# Note: this uses the magika python package installed via uv. Also,
# pip is not available here, so we skip pip show check.
uv run ./scripts/pre_release_check.py $CHECKER_OPTIONS --no-check-pip-show-package-version
working-directory: python
build-wheels:
needs: [pre-release-checks]
runs-on: ${{ matrix.platform.runner }}
strategy:
matrix:
platform:
- runner: ubuntu-latest
target: x86_64
- runner: windows-latest
target: x64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v5
- name: Setup Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # pin@v5
with:
python-version: "3.12"
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
- if: matrix.platform.runner == 'ubuntu-latest'
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
with:
path: rust/onnx/runtime/build/Linux
key: maturin-${{ matrix.platform.target }}-${{ hashFiles('rust/onnx/build.sh') }}
- run: python3 ./python/scripts/fix_package_version.py
- if: matrix.platform.runner == 'ubuntu-latest'
name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out=../dist
before-script-linux: "${{ github.workspace }}/rust/onnx/maturin.sh"
manylinux: 2_28
working-directory: python
- if: matrix.platform.runner != 'ubuntu-latest'
name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.platform.target }}
args: --release --out=../dist
working-directory: python
- name: Upload wheels
uses: actions/upload-artifact@v5
with:
name: wheel-${{ matrix.platform.runner }}-${{ matrix.platform.target }}
path: dist
# Download, install, and test the wheels with different versions of python
test-wheels:
needs: [build-wheels]
runs-on: ${{ matrix.platform.runner }}
if: github.event.schedule != '12 3 * * 1'
strategy:
# We want to know in which exact situation the tests fail
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
platform:
- runner: ubuntu-latest
target: x86_64
- runner: windows-latest
target: x64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v5
- uses: actions/download-artifact@v4
with:
name: wheel-${{ matrix.platform.runner }}-${{ matrix.platform.target }}
path: dist
- name: Setup Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # pin@v5
with:
python-version: "${{ matrix.python-version }}"
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
# Attempt "uv add magika.whl", in a temporary directory
- name: Check that `uv add magika.whl` works
shell: bash
run: |
mkdir /tmp/test-uv
cp -vR dist/*.whl /tmp/test-uv
cd /tmp/test-uv
uv init
uv add ./$(\ls -1 *.whl | head -n 1)
# From now on, magika will be available in the global environment
- name: Install the wheel via pip
run: python3 -m pip install $(python -c "import glob; print(glob.glob('dist/*.whl')[0])")
- run: magika --version
- run: "python3 -c 'import magika; m = magika.Magika(); print(m)'"
- run: magika -r tests_data/basic
- run: python3 ./python/scripts/run_quick_test_magika_cli.py
- run: python3 ./python/scripts/run_quick_test_magika_module.py
- name: Check package for release readiness
env:
IS_RELEASE_TAG: ${{ github.event_name == 'push' && github.ref_type == 'tag' && (startsWith(github.ref_name, env.RELEASE_TAG_PREFIX) || startsWith(github.ref_name, env.TEST_RELEASE_TAG_PREFIX)) }}
shell: bash
run: |
if [[ "${{ env.IS_RELEASE_TAG }}" == 'true' ]]; then
FULL_TAG_REF="${{ github.ref }}"
TAG_NAME="${{ github.ref_name }}"
if [[ "${TAG_NAME}" == "${{ env.RELEASE_TAG_PREFIX }}"* ]]; then
PREFIX_TO_REMOVE="refs/tags/${{ env.RELEASE_TAG_PREFIX }}"
else
PREFIX_TO_REMOVE="refs/tags/${{ env.TEST_RELEASE_TAG_PREFIX }}"
fi
TAG_VERSION="${FULL_TAG_REF#${PREFIX_TO_REMOVE}}"
CHECKER_OPTIONS="--expected-version ${TAG_VERSION}"
else
CHECKER_OPTIONS="--report-only"
fi
# Note: this uses the magika python package that was just built.
python3 ./scripts/pre_release_check.py $CHECKER_OPTIONS
working-directory: python
build-pure-python-wheel-and-sdist:
needs: [pre-release-checks]
runs-on: ubuntu-latest
if: github.event.schedule != '12 3 * * 1'
steps:
- uses: actions/checkout@v5
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
- run: uv run ./scripts/prepare_pyproject_for_pure_python_wheel.py
working-directory: python
- name: Build pure python wheel and source distribution
run: uv build --out-dir ../dist
working-directory: python
- name: Upload pure python wheel
uses: actions/upload-artifact@v5
with:
name: wheel-pure-python
path: dist/*.whl
- name: Upload sdist
uses: actions/upload-artifact@v5
with:
name: sdist
path: dist/*.tar.gz
# Download, install, and test the pure python wheel on multiple platforms
test-pure-python-wheel:
needs: [build-pure-python-wheel-and-sdist]
runs-on: ${{ matrix.platform.runner }}
if: github.event.schedule != '12 3 * * 1'
strategy:
# We want to know in which exact situation the tests fail
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
platform:
- runner: ubuntu-latest
target: x86_64
- runner: windows-latest
target: x64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v5
- uses: actions/download-artifact@v4
with:
name: wheel-pure-python
path: dist
- name: Setup Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # pin@v5
with:
python-version: "${{ matrix.python-version }}"
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
# Attempt "uv add magika.whl", in a temporary directory
- name: Check that `uv add magika.whl` works
shell: bash
run: |
mkdir /tmp/test-uv
cp -vR dist/*.whl /tmp/test-uv
cd /tmp/test-uv
uv init
uv add ./$(\ls -1 *.whl | head -n 1)
# From now on, magika will be available in the global environment
- name: Install the wheel
run: python3 -m pip install $(python -c "import glob; print(glob.glob('dist/*.whl')[0])")
# Check that the magika script points to the placeholder raising a warning
- run: magika --version | grep -C10 WARNING | grep -C10 magika-python-client
# Check that the fallback magika's python client can be run
- run: magika-python-client -r tests_data/basic
# Check that the results of the python's client are correct
- run: python3 ./python/scripts/run_quick_test_magika_cli.py --client-path magika-python-client
# Test the python module
- run: "python3 -c 'import magika; m = magika.Magika(); print(m)'"
- run: python3 ./python/scripts/run_quick_test_magika_module.py
- name: Check package for release readiness
env:
IS_RELEASE_TAG: ${{ github.event_name == 'push' && github.ref_type == 'tag' && (startsWith(github.ref_name, env.RELEASE_TAG_PREFIX) || startsWith(github.ref_name, env.TEST_RELEASE_TAG_PREFIX)) }}
shell: bash
run: |
if [[ "${{ env.IS_RELEASE_TAG }}" == 'true' ]]; then
FULL_TAG_REF="${{ github.ref }}"
TAG_NAME="${{ github.ref_name }}"
if [[ "${TAG_NAME}" == "${{ env.RELEASE_TAG_PREFIX }}"* ]]; then
PREFIX_TO_REMOVE="refs/tags/${{ env.RELEASE_TAG_PREFIX }}"
else
PREFIX_TO_REMOVE="refs/tags/${{ env.TEST_RELEASE_TAG_PREFIX }}"
fi
TAG_VERSION="${FULL_TAG_REF#${PREFIX_TO_REMOVE}}"
CHECKER_OPTIONS="--expected-version ${TAG_VERSION}"
else
CHECKER_OPTIONS="--report-only"
fi
# Note: this uses the magika python package that was just built.
python3 ./scripts/pre_release_check.py $CHECKER_OPTIONS --use-python-client
working-directory: python
test-sdist:
needs: [build-pure-python-wheel-and-sdist]
runs-on: ${{ matrix.platform.runner }}
if: github.event.schedule != '12 3 * * 1'
strategy:
# We want to know in which exact situation the tests fail
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
platform:
- runner: ubuntu-latest
target: x86_64
- runner: windows-latest
target: x64
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v5
- uses: actions/download-artifact@v4
with:
name: sdist
path: dist
- name: Setup Python
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # pin@v5
with:
python-version: "${{ matrix.python-version }}"
- name: Install uv
run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
# Attempt "uv add magika.whl", in a temporary directory
- name: Check that `uv add magika.tar.gz` works
shell: bash
run: |
mkdir /tmp/test-uv
cp -vR dist/*.tar.gz /tmp/test-uv
cd /tmp/test-uv
uv init
uv add ./$(\ls -1 *.tar.gz | head -n 1)
# From now on, magika will be available in the global environment
- name: Install the sdist
run: python3 -m pip install $(python -c "import glob; print(glob.glob('dist/*.tar.gz')[0])")
# Check that the magika script points to the placeholder raising a warning
- run: magika --version | grep -C10 WARNING | grep -C10 magika-python-client
# Check that the fallback magika's python client can be run
- run: magika-python-client -r tests_data/basic
# Check that the results of the python's client are correct
- run: python3 ./python/scripts/run_quick_test_magika_cli.py --client-path magika-python-client
# Test the python module
- run: "python3 -c 'import magika; m = magika.Magika(); print(m)'"
- run: python3 ./python/scripts/run_quick_test_magika_module.py
- name: Check package for release readiness
env:
IS_RELEASE_TAG: ${{ github.event_name == 'push' && github.ref_type == 'tag' && (startsWith(github.ref_name, env.RELEASE_TAG_PREFIX) || startsWith(github.ref_name, env.TEST_RELEASE_TAG_PREFIX)) }}
shell: bash
run: |
if [[ "${{ env.IS_RELEASE_TAG }}" == 'true' ]]; then
FULL_TAG_REF="${{ github.ref }}"
TAG_NAME="${{ github.ref_name }}"
if [[ "${TAG_NAME}" == "${{ env.RELEASE_TAG_PREFIX }}"* ]]; then
PREFIX_TO_REMOVE="refs/tags/${{ env.RELEASE_TAG_PREFIX }}"
else
PREFIX_TO_REMOVE="refs/tags/${{ env.TEST_RELEASE_TAG_PREFIX }}"
fi
TAG_VERSION="${FULL_TAG_REF#${PREFIX_TO_REMOVE}}"
CHECKER_OPTIONS="--expected-version ${TAG_VERSION}"
else
CHECKER_OPTIONS="--report-only"
fi
# Note: this uses the magika python package that was just built.
python3 ./scripts/pre_release_check.py $CHECKER_OPTIONS --use-python-client
working-directory: python
# Adapted from https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
# Note: The publishing is only done with pushes of release tags.
publish-to-pypi:
name: Publish to PyPI
if: github.event_name == 'push' && github.ref_type == 'tag' && contains(github.ref_name, 'python')
needs: [test-wheels, test-pure-python-wheel, test-sdist]
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/magika
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the artifacts (binary wheels, pure python wheel, sdist)
uses: actions/download-artifact@v4
with:
path: artifacts/
- name: Flatten artifacts structure
run: |
# List all files for debugging
ls -alR artifacts/
# Find all files inside the subdirectories and move them up
find artifacts/ -mindepth 2 -type f -exec mv -t artifacts/ {} +
# Remove the now-empty subdirectories
find artifacts/ -mindepth 1 -type d -empty -delete
# Check structure after flattening
ls -alR artifacts/
- name: Publish distribution to PyPI
if: github.event_name == 'push' && github.ref_type == 'tag' && startsWith(github.ref_name, env.RELEASE_TAG_PREFIX)
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: artifacts/
# Note: The publishing is only done with pushes of test release tags.
publish-to-testpypi:
name: Publish to TestPyPI
if: github.event_name == 'push' && github.ref_type == 'tag' && contains(github.ref_name, 'python')
needs: [test-wheels, test-pure-python-wheel, test-sdist]
runs-on: ubuntu-latest
environment:
name: testpypi
url: https://test.pypi.org/p/magika
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the artifacts (binary wheels, pure python wheel, sdist)
uses: actions/download-artifact@v4
with:
path: artifacts/
- name: Flatten artifacts structure
run: |
# List all files for debugging
ls -alR artifacts/
# Find all files inside the subdirectories and move them up
find artifacts/ -mindepth 2 -type f -exec mv -t artifacts/ {} +
# Remove the now-empty subdirectories
find artifacts/ -mindepth 1 -type d -empty -delete
# Check structure after flattening
ls -alR artifacts/
- name: Publish distribution to TestPyPI
if: github.event_name == 'push' && github.ref_type == 'tag' && startsWith(github.ref_name, env.TEST_RELEASE_TAG_PREFIX)
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: artifacts/
repository-url: https://test.pypi.org/legacy/