Sammlung von GitHub Actions Workflows für Python-Projekte aller Art.
- Workflow kopieren - Wählen Sie den passenden Workflow für Ihr Projekt
- Konfiguration anpassen - Nutzen Sie die entsprechende Config-Datei aus
.github/config/python-build/ - Secrets einrichten - Konfigurieren Sie die benötigten Repository-Secrets
- Workflow aktivieren - Committen Sie die Datei nach
.github/workflows/
| Workflow | Beschreibung | Verwendung |
|---|---|---|
python-app.yml |
Einfache Python-Anwendung | Web-Apps, Scripts, CLI-Tools |
python-package.yml |
Python-Paket für PyPI | Libraries, Frameworks |
python-publish.yml |
PyPI Publishing | Automatische Releases |
python-docker.yml |
Docker Build & Deploy | Containerisierte Apps |
| Workflow | Framework | Features |
|---|---|---|
python-fastapi-example.yml |
FastAPI | PostgreSQL, Redis, API-Tests |
python-django-example.yml |
Django | Migrations, Static Files, Frontend |
python-ml-example.yml |
Scikit-learn/ML | Model Training, Validation, Deployment |
Alle Konfigurationsdateien befinden sich in .github/config/python-build/:
default.yml- Standard-Konfiguration für einfache Projekteweb-app.yml- Web-Anwendungen (Flask, Django, FastAPI)package.yml- PyPI-Pakete mit Multi-Platform Testingdata-science.yml- Data Science und ML-Projektemicroservice.yml- Microservices mit Kubernetes
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [ main ]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: 'pip'
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest pytest-cov
- name: Run tests
run: pytest --cov=. --cov-report=xml# Kopiere python-fastapi-example.yml
# Anpassungen:
services:
postgres:
image: postgres:15
env:
POSTGRES_DB: your_db_name
POSTGRES_USER: your_user
POSTGRES_PASSWORD: your_password# Kopiere python-publish.yml
# Erforderliche Secrets:
# - PYPI_API_TOKEN (für PyPI)
# - TEST_PYPI_API_TOKEN (für TestPyPI)your-project/
├── .github/
│ └── workflows/
│ └── ci.yml
├── src/
│ └── your_package/
├── tests/
├── requirements.txt
├── requirements-dev.txt
├── setup.py oder pyproject.toml
└── README.md
# Produktions-Dependencies
requests>=2.28.0
click>=8.0.0# Entwicklungs-Dependencies
pytest>=7.0.0
pytest-cov>=4.0.0
black>=22.0.0
flake8>=5.0.0
mypy>=0.991[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "your-package"
version = "0.1.0"
description = "Your package description"
authors = [{name = "Your Name", email = "your.email@example.com"}]
license = {text = "MIT"}Je nach Workflow benötigen Sie verschiedene Secrets:
GITHUB_TOKEN # Automatisch verfügbar
PYPI_API_TOKEN # PyPI API Token
TEST_PYPI_API_TOKEN # TestPyPI API Token
DOCKER_USERNAME # Docker Hub Username
DOCKER_PASSWORD # Docker Hub Token
AWS_ACCESS_KEY_ID # AWS Credentials
AWS_SECRET_ACCESS_KEY
AZURE_CLIENT_ID # Azure Credentials
AZURE_CLIENT_SECRET
- ✅ pytest mit Coverage
- ✅ flake8 Linting
- ✅ Black Code Formatting
- ✅ isort Import Sorting
- ✅ mypy Type Checking
- ✅ bandit Security Scanning
- ✅ safety Dependency Scanning
- ✅ Multi-Platform Testing (Linux, Windows, macOS)
- ✅ Multiple Python Versions (3.9-3.12)
- ✅ Docker Multi-Platform Builds
- ✅ PyPI/TestPyPI Publishing
- ✅ GitHub Container Registry
- ✅ Kubernetes Deployment
- ✅ Codecov Coverage Reports
- ✅ GitHub Security Advisories
- ✅ Dependabot Integration
- ✅ Artifact Uploads
- ✅ Test Result Publishing
- name: Setup DVC
run: |
pip install dvc[s3]
dvc pull
- name: Train Model
run: |
python train.py
dvc add models/model.pkldeploy-staging:
if: github.ref == 'refs/heads/develop'
environment:
name: staging
url: https://staging.example.com
deploy-production:
if: github.ref == 'refs/heads/main'
environment:
name: production
url: https://example.comstrategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
database: ["postgresql", "mysql"]
services:
db:
image: ${{ matrix.database == 'postgresql' && 'postgres:15' || 'mysql:8' }}Import Error bei Tests
# Lösung: Package installieren
pip install -e .Coverage zu niedrig
# Lösung: Coverage-Konfiguration anpassen
pytest --cov=src --cov-fail-under=80Docker Build langsam
# Lösung: Multi-stage Build verwenden
FROM python:3.12-slim as builder
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.12-slim
COPY --from=builder /root/.local /root/.localFlake8 Fehler
# setup.cfg
[flake8]
max-line-length = 88
extend-ignore = E203, W503
exclude = venv/, build/, dist/- GitHub Actions Python Guide
- Python Packaging User Guide
- pytest Documentation
- Docker Python Best Practices
Beiträge sind willkommen! Bitte:
- Fork das Repository
- Erstelle einen Feature Branch
- Teste deine Änderungen
- Erstelle eine Pull Request
MIT License - siehe LICENSE für Details.