Debian Codegraph @

docs/how-to/ci-cd.rst

raw
1 ###################
2 CI/CD Integration
3 ###################
4
5 This guide shows how to integrate build into your continuous integration and deployment workflows.
6
7 ****************
8 GitHub Actions
9 ****************
10
11 Using hynek's build-and-inspect-python-package
12 ==============================================
13
14 The recommended way to build and verify packages in `GitHub Actions <https://docs.github.com/en/actions>`_ is using the
15 `hynek/build-and-inspect-python-package <https://github.com/hynek/build-and-inspect-python-package>`_ action. This
16 action builds your package, inspects it for common issues, and provides the built artifacts.
17
18 .. code-block:: yaml
19
20 name: Build
21
22 on: [push, pull_request]
23
24 jobs:
25 build:
26 runs-on: ubuntu-latest
27 steps:
28 - uses: actions/checkout@v4
29 - uses: hynek/build-and-inspect-python-package@v2
30
31 This action automatically:
32
33 - Builds both sdist and wheel
34 - Verifies the sdist contains all necessary files
35 - Checks for common packaging issues
36 - Uploads artifacts for download
37 - Works with any PEP 517 build backend
38
39 For more details, see the `action documentation <https://github.com/hynek/build-and-inspect-python-package>`_.
40
41 Manual build with isolation
42 ===========================
43
44 If you need more control, use build directly with isolated environments. ``pipx`` is pre-installed on all GitHub Actions
45 runners:
46
47 .. code-block:: yaml
48
49 name: Build
50
51 on: [push, pull_request]
52
53 jobs:
54 build:
55 runs-on: ubuntu-latest
56 steps:
57 - uses: actions/checkout@v4
58
59 - uses: actions/setup-python@v5
60 with:
61 python-version: "3.12"
62
63 - name: Build package
64 run: pipx run build
65
66 - name: Upload artifacts
67 uses: actions/upload-artifact@v4
68 with:
69 name: dist
70 path: dist/
71
72 Fast builds with pre-installed dependencies
73 ===========================================
74
75 For faster CI builds, pre-install build dependencies and use ``--no-isolation``:
76
77 .. code-block:: yaml
78
79 name: Fast Build
80
81 on: [push, pull_request]
82
83 jobs:
84 build:
85 runs-on: ubuntu-latest
86 steps:
87 - uses: actions/checkout@v4
88
89 - uses: actions/setup-python@v5
90 with:
91 python-version: "3.12"
92
93 - name: Install dependencies
94 run: |
95 pip install build hatchling # Replace hatchling with your backend
96
97 - name: Build package
98 run: python -m build --no-isolation
99
100 Using uv for faster builds
101 ==========================
102
103 Use uv as the installer for faster dependency installation:
104
105 .. code-block:: yaml
106
107 name: Build with uv
108
109 on: [push, pull_request]
110
111 jobs:
112 build:
113 runs-on: ubuntu-latest
114 steps:
115 - uses: actions/checkout@v4
116
117 - uses: actions/setup-python@v5
118 with:
119 python-version: "3.12"
120
121 - name: Install uv and build
122 run: pip install uv build
123
124 - name: Build package
125 run: python -m build --installer=uv
126
127 Matrix builds across Python versions
128 ====================================
129
130 Test building with multiple Python versions:
131
132 .. code-block:: yaml
133
134 name: Build Matrix
135
136 on: [push, pull_request]
137
138 jobs:
139 build:
140 runs-on: ubuntu-latest
141 strategy:
142 matrix:
143 python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
144
145 steps:
146 - uses: actions/checkout@v4
147
148 - uses: actions/setup-python@v5
149 with:
150 python-version: ${{ matrix.python-version }}
151
152 - name: Install build
153 run: pip install build
154
155 - name: Build package
156 run: python -m build
157
158 Building wheels for multiple platforms
159 ======================================
160
161 For packages with C extensions or platform-specific code, use cibuildwheel:
162
163 .. code-block:: yaml
164
165 name: Build Wheels
166
167 on: [push, pull_request]
168
169 jobs:
170 build_wheels:
171 name: Build wheels on ${{ matrix.os }}
172 runs-on: ${{ matrix.os }}
173 strategy:
174 matrix:
175 os: [ubuntu-latest, windows-latest, macos-13, macos-14]
176
177 steps:
178 - uses: actions/checkout@v4
179
180 - uses: pypa/[email protected]
181
182 - uses: actions/upload-artifact@v4
183 with:
184 name: wheels-${{ matrix.os }}
185 path: ./wheelhouse/*.whl
186
187 build_sdist:
188 runs-on: ubuntu-latest
189 steps:
190 - uses: actions/checkout@v4
191 - uses: hynek/build-and-inspect-python-package@v2
192
193 See :doc:`choosing-tools` for when to use build vs cibuildwheel.
194
195 Publishing to PyPI
196 ==================
197
198 Combine building and publishing in a release workflow using the `PyPA publish action
199 <https://github.com/pypa/gh-action-pypi-publish>`_:
200
201 .. code-block:: yaml
202
203 name: Publish
204
205 on:
206 release:
207 types: [published]
208
209 jobs:
210 build:
211 runs-on: ubuntu-latest
212 steps:
213 - uses: actions/checkout@v4
214 - uses: hynek/build-and-inspect-python-package@v2
215
216 publish:
217 needs: build
218 runs-on: ubuntu-latest
219 permissions:
220 id-token: write
221 steps:
222 - uses: actions/download-artifact@v4
223 with:
224 name: Packages
225 path: dist
226
227 - uses: pypa/gh-action-pypi-publish@release/v1
228
229 ***********
230 GitLab CI
231 ***********
232
233 Basic build
234 ===========
235
236 .. code-block:: yaml
237
238 build:
239 image: python:3.12
240 script:
241 - pip install build
242 - python -m build
243 artifacts:
244 paths:
245 - dist/
246
247 With caching
248 ============
249
250 Cache pip packages to speed up builds:
251
252 .. code-block:: yaml
253
254 build:
255 image: python:3.12
256 cache:
257 paths:
258 - .cache/pip
259 variables:
260 PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
261 script:
262 - pip install build
263 - python -m build
264 artifacts:
265 paths:
266 - dist/
267
268 Fast builds with pre-installed dependencies
269 ===========================================
270
271 .. code-block:: yaml
272
273 build:
274 image: python:3.12
275 script:
276 - pip install build hatchling
277 - python -m build --no-isolation
278 artifacts:
279 paths:
280 - dist/
281
282 Matrix builds
283 =============
284
285 .. code-block:: yaml
286
287 .build_template:
288 script:
289 - pip install build
290 - python -m build
291 artifacts:
292 paths:
293 - dist/
294
295 build:py310:
296 extends: .build_template
297 image: python:3.10
298
299 build:py314:
300 extends: .build_template
301 image: python:3.14
302
303 **********
304 CircleCI
305 **********
306
307 Basic build
308 ===========
309
310 .. code-block:: yaml
311
312 version: 2.1
313
314 jobs:
315 build:
316 docker:
317 - image: cimg/python:3.12
318 steps:
319 - checkout
320 - run:
321 name: Install build
322 command: pip install build
323 - run:
324 name: Build package
325 command: python -m build
326 - store_artifacts:
327 path: dist/
328
329 workflows:
330 build_workflow:
331 jobs:
332 - build
333
334 ***********
335 Travis CI
336 ***********
337
338 Basic build
339 ===========
340
341 .. code-block:: yaml
342
343 language: python
344 python:
345 - "3.10"
346 - "3.11"
347 - "3.12"
348 - "3.13"
349 - "3.14"
350
351 install:
352 - pip install build
353
354 script:
355 - python -m build
356
357 *****************
358 Azure Pipelines
359 *****************
360
361 Basic build
362 ===========
363
364 .. code-block:: yaml
365
366 trigger:
367 - main
368
369 pool:
370 vmImage: 'ubuntu-latest'
371
372 strategy:
373 matrix:
374 Python310:
375 python.version: '3.10'
376 Python314:
377 python.version: '3.14'
378
379 steps:
380 - task: UsePythonVersion@0
381 inputs:
382 versionSpec: '$(python.version)'
383
384 - script: |
385 pip install build
386 python -m build
387 displayName: 'Build package'
388
389 - task: PublishBuildArtifacts@1
390 inputs:
391 pathToPublish: 'dist'
392 artifactName: 'dist'
393
394 *********
395 Jenkins
396 *********
397
398 Declarative pipeline
399 ====================
400
401 .. code-block:: groovy
402
403 pipeline {
404 agent any
405
406 stages {
407 stage('Build') {
408 steps {
409 sh '''
410 python3 -m pip install build
411 python3 -m build
412 '''
413 }
414 }
415 stage('Archive') {
416 steps {
417 archiveArtifacts artifacts: 'dist/*', fingerprint: true
418 }
419 }
420 }
421 }
422
423 ********
424 Docker
425 ********
426
427 Multi-stage build
428 =================
429
430 Build packages in a Docker container:
431
432 .. code-block:: dockerfile
433
434 FROM python:3.12-slim AS builder
435
436 WORKDIR /app
437
438 COPY pyproject.toml README.md ./
439 COPY src/ src/
440
441 RUN pip install build && python -m build
442
443 # Runtime image
444 FROM python:3.12-slim
445
446 COPY --from=builder /app/dist/*.whl /tmp/
447 RUN pip install /tmp/*.whl && rm /tmp/*.whl
448
449 CMD ["python"]
450
451 *****************
452 tox Integration
453 *****************
454
455 Using build within tox environments:
456
457 .. code-block:: toml
458
459 [env_run_base]
460 description = "run test suite"
461 deps = ["pytest"]
462 commands = [["pytest", "tests"]]
463
464 [env.build]
465 description = "build the package"
466 deps = ["build"]
467 commands = [["python", "-m", "build"]]
468
469 [env."build-check"]
470 description = "build and check the package"
471 deps = [
472 "build",
473 "twine",
474 ]
475 commands = [
476 ["python", "-m", "build"],
477 ["twine", "check", "dist/*"],
478 ]
479
480 Run the build environment:
481
482 .. code-block:: console
483
484 $ tox -e build
485
486 *****************************
487 Discovering Built Artifacts
488 *****************************
489
490 Artifact filenames are dynamic, so pipelines that need to act on the exact files build produced (upload, sign, compute
491 digests) should not guess or glob them. Pass ``--report PATH`` to capture a machine-readable list and read it back with
492 a JSON tool such as ``jq``. Pick a ``PATH`` outside the output directory so tools that upload ``dist/*`` wholesale do
493 not trip over it:
494
495 .. code-block:: yaml
496
497 - name: Build and upload
498 run: |
499 python -m build --report build-report.json
500 twine upload $(jq -r '.artifacts[].path' build-report.json)
501
502 The report also includes each artifact's size and SHA-256 hash, useful for emitting provenance or checksums:
503
504 .. code-block:: console
505
506 $ jq -r '.artifacts[] | "\(.hashes.sha256) \(.name)"' build-report.json
507
508 See :doc:`../reference/cli` for the full report schema.
509
510 ******************
511 Verifying Builds
512 ******************
513
514 Always verify your built packages before publishing:
515
516 .. code-block:: yaml
517
518 - name: Check package
519 run: |
520 pip install twine
521 twine check dist/*
522
523 Or use hynek's action which includes verification automatically.
524
525 ********************
526 Conditional Builds
527 ********************
528
529 Only build on specific events:
530
531 .. code-block:: yaml
532
533 name: Build
534
535 on:
536 push:
537 branches: [main]
538 tags: ['v*']
539 pull_request:
540 branches: [main]
541
542 jobs:
543 build:
544 runs-on: ubuntu-latest
545 steps:
546 - uses: actions/checkout@v4
547 - uses: hynek/build-and-inspect-python-package@v2
548
549 publish:
550 if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
551 needs: build
552 runs-on: ubuntu-latest
553 permissions:
554 id-token: write
555 steps:
556 - uses: actions/download-artifact@v4
557 - uses: pypa/gh-action-pypi-publish@release/v1
558
559 This builds on every push and PR, but only publishes when pushing a version tag.
560
561 ***************************
562 Troubleshooting CI Builds
563 ***************************
564
565 Enable verbose output
566 =====================
567
568 Add ``-vv`` flag for debugging:
569
570 .. code-block:: yaml
571
572 - name: Build package
573 run: python -m build -vv
574
575 Preserve build artifacts
576 ========================
577
578 Upload the dist directory even on failure:
579
580 .. code-block:: yaml
581
582 - name: Upload artifacts
583 if: always()
584 uses: actions/upload-artifact@v4
585 with:
586 name: dist
587 path: dist/
588
589 Cache build environments
590 ========================
591
592 For repeated builds, cache pip's download cache:
593
594 .. code-block:: yaml
595
596 - uses: actions/cache@v4
597 with:
598 path: ~/.cache/pip
599 key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }}
600
601 **********
602 See also
603 **********
604
605 - :doc:`troubleshooting` for build-specific issues
606 - :doc:`corporate-environments` for private indexes and proxies
607 - :doc:`choosing-tools` for when to use build vs other tools
608 - `hynek/build-and-inspect-python-package <https://github.com/hynek/build-and-inspect-python-package>`_ - Recommended
609 GitHub Action
610 - `cibuildwheel <https://cibuildwheel.readthedocs.io/>`_ - For multi-platform wheel builds