Diese Dokumentation beschreibt die verfügbaren Python Build Workflows und Konfigurationen für verschiedene Projekttypen.
Das Python Build System bietet mehrere vorgefertigte Workflows für unterschiedliche Python-Anwendungstypen:
- Python Application - Einfache Python-Anwendungen
- Python Package - Pakete für PyPI
- Python Docker - Containerisierte Anwendungen
- Python Publish - Publishing zu PyPI/TestPyPI
Grundlegender CI-Workflow für Python-Anwendungen.
Features:
- Python 3.12 Setup
- Virtual Environment
- Dependency Installation
- Security Checks (Safety, Bandit)
- Linting (flake8)
- Testing (pytest mit Coverage)
- Codecov Integration
- Test Results Upload
Verwendung:
name: My Python App CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build:
uses: ./.github/workflows/python-app.ymlMulti-Platform CI für Python-Pakete mit verschiedenen Python-Versionen.
Features:
- Matrix Build (Python 3.9-3.12, Ubuntu/Windows/macOS)
- Code Formatting (Black, isort)
- Type Checking (mypy)
- Package Building
- Package Validation
- Cross-Platform Testing
Verwendung:
name: Python Package CI
on:
push:
branches: [ main ]
pull_request:
jobs:
build:
uses: ./.github/workflows/python-package.ymlAutomatisches Publishing zu PyPI/TestPyPI.
Features:
- Trusted Publishing (OIDC)
- TestPyPI Support
- Production PyPI Publishing
- Pre-Publishing Tests
- GitHub Release Creation
Verwendung:
name: Publish Python Package
on:
release:
types: [published]
jobs:
publish:
uses: ./.github/workflows/python-publish.ymlDocker Build und Deployment Pipeline.
Features:
- Multi-Platform Docker Builds
- GitHub Container Registry
- Security Scanning (Trivy)
- Staging/Production Deployment
- Image Caching
Verwendung:
name: Docker Build & Deploy
on:
push:
branches: [ main ]
tags: [ 'v*' ]
jobs:
docker:
uses: ./.github/workflows/python-docker.ymlAlle Konfigurationen befinden sich in .github/config/python-build/:
Grundkonfiguration für einfache Python-Projekte.
python:
version: "3.12"
cache: "pip"
testing:
framework: "pytest"
coverage: true
coverage_threshold: 80
linting:
flake8:
enabled: true
max_line_length: 127Konfiguration für Web-Anwendungen (Flask, Django, FastAPI).
python:
version: "3.12"
testing:
coverage_threshold: 85
integration_tests: true
linting:
black:
enabled: true
isort:
enabled: true
security:
bandit:
enabled: true
fail_on_error: true
docker:
build: true
security_scan: trueKonfiguration für PyPI-Pakete.
python:
versions: ["3.9", "3.10", "3.11", "3.12"]
matrix:
os: ["ubuntu-latest", "windows-latest", "macos-latest"]
publishing:
testpypi:
enabled: true
pypi:
enabled: true
trusted_publishing: trueSpezielle Konfiguration für Data Science Projekte.
python:
version: "3.12"
conda_support: true
notebooks:
execution_test: true
output_cleanup: true
ml_specific:
model_validation: true
reproducibility_check: trueKonfiguration für Microservices.
docker:
build: true
multi_platform: true
security_scan: true
api_testing:
openapi_validation: true
contract_testing: true
deployment:
kubernetes: true
helm_charts: trueVollständige CI/CD Pipeline für FastAPI mit PostgreSQL und Redis.
Features:
- Service Container (PostgreSQL, Redis)
- Database Migrations (Alembic)
- API Integration Tests
- Security Scanning
- Container Deployment
Django-spezifische Pipeline mit Frontend-Integration.
Features:
- Node.js für Frontend Assets
- Django Management Commands
- Static Files Collection
- Database Migrations
- Performance Tests
ML-Pipeline mit Model Training und Deployment.
Features:
- Data Validation
- Model Training
- Model Testing
- Performance Validation
- ML Service Deployment
- Model Registry Integration
Für verschiedene Workflows werden folgende Secrets benötigt:
# GitHub Token (automatisch verfügbar)
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Für PyPI Publishing
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
TEST_PYPI_API_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
# Für Docker Registry
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
# Für Cloud Deployment
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}env:
PYTHON_version: "3.12"
DATABASE_URL: "postgresql://user:pass@localhost/db"
REDIS_URL: "redis://localhost:6379"
SECRET_KEY: ${{ secrets.SECRET_KEY }}# requirements.txt - Produktions-Dependencies
requests>=2.28.0
fastapi>=0.68.0
uvicorn[standard]>=0.15.0
# requirements-dev.txt - Entwicklungs-Dependencies
pytest>=7.0.0
pytest-cov>=4.0.0
black>=22.0.0
isort>=5.10.0
mypy>=0.991# pytest.ini
[tool:pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
--strict-markers
--disable-warnings
--cov=src
--cov-report=term-missing
--cov-report=html# pyproject.toml
[tool.black]
line-length = 88
target-version = ['py312']
[tool.isort]
profile = "black"
multi_line_output = 3
[tool.mypy]
python_version = "3.12"
strict = true# .bandit
[bandit]
exclude_dirs = ["tests", "venv"]
skips = ["B101", "B601"]-
Import Fehler
# Lösung: Package im Development Modus installieren pip install -e .
-
Test Failures
# Debug mit verbose output pytest -v --tb=long -
Coverage zu niedrig
# Coverage Report anzeigen pytest --cov-report=html open htmlcov/index.html -
Docker Build Fehler
# Multi-stage Build verwenden FROM python:3.12-slim as builder # ... build steps FROM python:3.12-slim as runtime
-
Caching
- uses: actions/setup-python@v5 with: python-version: "3.12" cache: 'pip'
-
Parallel Testing
- name: Test with pytest run: pytest -n auto
-
Matrix Optimization
strategy: fail-fast: false matrix: python-version: ["3.11", "3.12"] exclude: - python-version: "3.9" os: windows-latest
# Vorher (Travis CI)
language: python
python: "3.12"
script: pytest
# Nachher (GitHub Actions)
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pytest# Vorher (GitLab CI)
test:
image: python:3.12
script:
- pip install -r requirements.txt
- pytest
# Nachher (GitHub Actions)
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
- run: |
pip install -r requirements.txt
pytest| Input | Description | Default | Options |
|---|---|---|---|
runs-on |
Runner to use | ubuntu-latest |
String or JSON array (see below) |
timeout-minutes |
Job timeout | 30 |
Minutes |
The runs-on parameter supports both GitHub-hosted and self-hosted runners:
# GitHub-hosted (string)
runs-on: 'ubuntu-latest'
# Self-hosted (JSON array)
runs-on: '["self-hosted", "linux"]'
runs-on: '["self-hosted", "linux", "docker"]'See Self-Hosted Runner Documentation for details.