このシリーズの最初の2つの部分では、私たちは: - GitHub Actions と PyPI を使用して Python アプリケーションの機能的な CI/CD パイプラインを構築しました。 - このパイプラインをマルチバージョンテスト、Test PyPIによる段階的リリース、品質およびセキュリティツールで工業化しました。

この第三部では、私たちは行きますPyPI への単なる公開を超えてを構築するために、完全で堅牢かつプロフェッショナルな CI/CD チェーンを作るために : - 詩モダンなパッケージングと依存関係の最適化された管理のために - ドッカー再現可能でマルチプラットフォームなビルドを作成するために。 - 条件付き出版リリース候補などのシナリオを管理するために - 自動化ツール(Renovate, Dependabot) パイプラインを最新の状態に保つために労力をかけずに。

Poetry との統合

Poetry は従来のパッケージングツール (setup.py, requirements.txt) を置き換え、依存関係とビルドの管理を一元化する中に`pyproject.toml`.

Poetryのインストール

# Installer Poetry
curl -sSL https://install.python-poetry.org | python3 -
# Vérifier la version
poetry --version

プロジェクトの初期化

# Initialiser un nouveau projet avec Poetry
poetry init
# Suivre l'assistant pour renseigner : nom, version, description, licence, dépendances.

これはファイルを生成します`pyproject.toml`:

[tool.poetry]
name = "playlist-downloader"
version = "0.1.0"
description = "CLI tool for managing YouTube playlists"
authors = ["Christophe Hérolivier <[email protected]>"]

[tool.poetry.dependencies]
python = ">=3.8"
typer = "^0.9.0"
yt-dlp = "^2023.7.6"
google-api-python-client = "^2.0.0"
google-auth-oauthlib = "^1.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0"
mypy = "^1.0"
bandit = "^1.7"
safety = "^2.3"
black = "^23.0"
ruff = "^0.1"

依存関係の追加とインストール

poetry add typer yt-dlp google-api-python-client google-auth-oauthlib
poetry add --group dev pytest mypy black bandit safety ruff

Poetryを使った公開

Poetry はネイティブに公開を管理します :

# Publication sur Test PyPI
poetry publish --build --repository test-pypi

# Publication sur PyPI
poetry publish --build

このコマンドは自動的に存在する情報を使用する`pyproject.toml`。

Dockerを使った再現可能なビルド

開発、CI/CD、本番で同じ実行を保証するために、DockerはPoetryと完全に統合されます。

Dockerfileの例

FROM python:3.11-slim

WORKDIR /app
COPY pyproject.toml poetry.lock ./
RUN pip install poetry
RUN poetry install --no-root --only main

COPY . .

CMD ["poetry", "run", "python", "cli.py"]

これは保証します: - 凍結されたPython環境。 経由でロックされた依存関係`poetry.lock`. - Dockerをサポートするあらゆるシステムで実行可能なイメージ。

GitHub Actionsへの統合

name: Docker Build

on:
  push:
    branches: [main]

jobs:
  build-docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build Docker image
        run: docker build -t ghcr.io/${{ github.repository }}:latest .
      - name: Push Docker image
        run: docker push ghcr.io/${{ github.repository }}:latest

条件付き公開

プロフェッショナルなパイプラインでは、特定の場合のみ公開できる必要があります : - テストPyPI向けリリース候補。 - 安定版をPyPIに - Builds Docker トリガーのみ`main`.

- name: Publish to Test PyPI
  if: contains(github.ref, '-rc')
  run: poetry publish --build --repository test-pypi

- name: Publish to PyPI
  if: startsWith(github.ref, 'refs/tags/v')
  run: poetry publish --build

このアプローチは意図しない公開を防ぎます。

Renovate と Dependabot を使った依存関係の自動化

依存関係が古くなるのを防ぐため、自動更新ツールを組み込んでください。

Dependabot

version: 2
updates:
  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "weekly"
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"

Dependabotは毎週、PythonおよびGitHub Actionsの依存関係を更新するためにPRを開きます。

リノベート

{
  "extends": ["config:base"],
  "packageRules": [
    {
      "matchManagers": ["pip"],
      "groupName": "python-dependencies",
      "schedule": ["before 6am on monday"]
    }
  ]
}

Renovateはより細かい制御を可能にします:依存関係のグループ化、スケジューリング、および高度なルール。

PlantUML図

ユースケース – 高度なCI/CD

@startuml
actor Developer
actor GitHub as "GitHub アクション"
actor PyPI
actor DockerRegistry as "Docker レジストリ"
actor Automation as "リノベート/Dependabot"

Developer --> (Push code)
(Trigger CI) --> GitHub
GitHub --> (Run Tests & Lint)
GitHub --> (Build Docker Image)
GitHub --> DockerRegistry
GitHub --> (Publish to Test PyPI)
GitHub --> (Promote to PyPI)
Automation --> (Update Dependencies)
@enduml

シーケンス – 条件付き公開

@startuml
!theme vibrant
left to right direction
Developer -> GitHub: Push tag v1.2.3-rc
GitHub -> CI: Run tests
CI -> CD: Check release type
CD -> Test PyPI: Publish RC
Developer -> GitHub: Push tag v1.2.3
GitHub -> CD: Publish to PyPI
CD -> Docker Registry: Push Docker image
@enduml

ステータス – CI/CD パイプライン

@startuml
[*] --> Idle
Idle --> CI_Running : push
CI_Running --> CI_Success : tests ok
CI_Running --> CI_Failed : tests fail
CI_Success --> CD_Running : tag detected
CD_Running --> CD_Success : publish ok
CD_Running --> CD_Failed : error
CD_Success --> [*]
@enduml

デプロイメント – CI/CDアーキテクチャ

@startuml
node "開発者用マシン" {
  component "Git クライアント"
}

node "GitHub Actions" {
  component "CI ワークフロー"
  component "CD ワークフロー"
}

node "パッケージリポジトリ" {
  artifact "テスト PyPI"
  artifact "PyPI"
  artifact "Dockerレジストリ"
}

"開発者用マシン" --> "GitHub Actions"
"GitHub Actions" --> "テスト PyPI"
"GitHub Actions" --> "PyPI"
"GitHub Actions" --> "Dockerレジストリ"
@enduml

結論

この第三部では、次のことを見ました: - Poetryを使用してPythonパッケージングをモダン化する。 - Dockerを使用して再現可能なビルドを保証する。 - 条件付き公開を設定する。 依存関係の更新を自動化する。

あなたは今ではCI/CDパイプラインを持っています完全、工業化され、安全, あなたのプロジェクトとともに進化する準備ができています。

関連記事