Podstawowy workflow CI
Podstawowy workflow do testów i lintowania:
YAML1 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 58name: CI on: push: branches: [main, develop] pull_request: branches: [main] jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11, 3.12] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt pip install pytest pytest-cov - name: Run tests run: | pytest tests/ --cov=src --cov-report=xml - name: Upload coverage uses: codecov/codecov-action@v3 with: file: ./coverage.xml lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 with: python-version: '3.11' - name: Install linting tools run: | pip install flake8 black isort - name: Run linters run: | flake8 src tests black --check src tests isort --check src tests
Workflow z packaging
Workflow z buildowaniem i publikowaniem pakietu:
YAML1 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 32name: Release on: release: types: [published] jobs: build-and-publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.11' - name: Install build tools run: | pip install build twine - name: Build package run: python -m build - name: Publish to PyPI env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} run: | twine upload dist/*
Kompletny pipeline CI/CD
Pełny pipeline z testami, lintem i deploymentem:
YAML1 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 48name: Full CI/CD Pipeline on: push: branches: [main] pull_request: branches: [main] jobs: quality-gate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.11' - name: Install dependencies run: | pip install -r requirements.txt pip install pytest pytest-cov flake8 black isort mypy - name: Run tests run: pytest tests/ --cov=src --cov-report=xml - name: Type checking run: mypy src/ - name: Linting run: | flake8 src tests black --check src tests isort --check src tests deploy: needs: quality-gate runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v3 - name: Deploy run: | echo "Deployment steps here" # Twój kod deploymentu
Podsumowanie
Automatyzacja CI/CD w GitHub Actions obejmuje:
- Workflow'y CI do testów i lintowania
- Publishing pakietów do PyPI
- Quality gates przed deploymentem
- Automatyczne deploymenty po testach
To kompletne rozwiązanie do automatyzacji procesów CI/CD w Pythonie.
➡️ Następny artykuł
Gratulacje! Opanowałeś kluczowe techniki automatyzacji w Pythonie. Jeśli chcesz pogłębić wiedzę, sprawdź pozostałe artykuły w sekcji automatyzacji:
Automatyzacja zadań w Pythonie — Praktyczne przykłady — dodatkowe przykłady i zaawansowane techniki automatyzacji.



