CI/CD Pipeline #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD Pipeline | ||
| on: | ||
| push: | ||
| branches: [ main, develop ] | ||
| pull_request: | ||
| branches: [ main, develop ] | ||
| schedule: | ||
| - cron: '0 0 * * 0' # 毎週日曜日の午前0時に実行 | ||
| defaults: | ||
| run: | ||
| shell: bash | ||
| jobs: | ||
| test: | ||
| name: Run Tests | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest, macos-latest] | ||
| python-version: ['3.8', '3.9', '3.10'] | ||
| exclude: | ||
| - os: windows-latest | ||
| python-version: '3.8' | ||
| - os: macos-latest | ||
| python-version: '3.8' | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| - name: Install system dependencies | ||
| if: runner.os == 'Linux' | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y python3-dev default-libmysqlclient-dev build-essential | ||
| - name: Set up virtual environment | ||
| run: | | ||
| python -m venv .venv | ||
| source .venv/bin/activate | ||
| python -m pip install --upgrade pip | ||
| pip install -r requirements.txt | ||
| pip install -r requirements-dev.txt | ||
| - name: Run tests | ||
| run: | | ||
| source .venv/bin/activate | ||
| pytest --cov=./ --cov-report=xml --cov-report=term | ||
| - name: Upload coverage to Codecov | ||
| uses: codecov/codecov-action@v3 | ||
| with: | ||
| token: ${{ secrets.CODECOV_TOKEN }} | ||
| file: ./coverage.xml | ||
| flags: unittests | ||
| name: codecov-umbrella | ||
| fail_ci_if_error: false | ||
| lint: | ||
| name: Lint | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' | ||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install -r requirements-dev.txt | ||
| - name: Run linters | ||
| run: | | ||
| flake8 . | ||
| black --check . | ||
| isort --check-only . | ||
| mypy . | ||
| build: | ||
| name: Build | ||
| runs-on: ubuntu-latest | ||
| needs: [test, lint] | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' | ||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install build | ||
| - name: Build package | ||
| run: | | ||
| python -m build | ||
| - name: Upload package | ||
| uses: actions/upload-artifact@v3 | ||
| with: | ||
| name: python-package | ||
| path: dist/* | ||
| performance-test: | ||
| name: Performance Test | ||
| runs-on: ubuntu-latest | ||
| needs: build | ||
| env: | ||
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db | ||
| TEST_ENV: ci | ||
| PYTHONPATH: ${{ github.workspace }} | ||
| PERF_TEST_USERS: 10 | ||
| PERF_TEST_SPAWN_RATE: 1 | ||
| PERF_TEST_DURATION: 30s | ||
| PERF_TEST_CONFIG: tests/performance/config/perf_config.json | ||
| services: | ||
| postgres: | ||
| image: postgres:14 | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| POSTGRES_DB: test_db | ||
| ports: | ||
| - 5432:5432 | ||
| options: >- | ||
| --health-cmd pg_isready | ||
| --health-interval 10s | ||
| --health-timeout 5s | ||
| --health-retries 5 | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' | ||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install -r requirements.txt | ||
| pip install -r requirements-dev.txt | ||
| pip install locust pyyaml | ||
| - name: Create required directories | ||
| run: | | ||
| mkdir -p reports/performance | ||
| mkdir -p results/performance | ||
| mkdir -p plots | ||
| - name: Start PostgreSQL | ||
| uses: harmon758/postgresql-action@v1 | ||
| with: | ||
| postgresql version: '14' | ||
| postgresql db: 'test_db' | ||
| postgresql user: 'postgres' | ||
| postgresql password: 'postgres' | ||
| - name: Run database migrations | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| alembic upgrade head | ||
| - name: Generate test data | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| python scripts/generate_test_data.py --size medium | ||
| - name: Run performance tests | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| locust -f tests/performance/locustfile.py \ | ||
| --headless \ | ||
| --users ${{ env.PERF_TEST_USERS }} \ | ||
| --spawn-rate ${{ env.PERF_TEST_SPAWN_RATE }} \ | ||
| --run-time ${{ env.PERF_TEST_DURATION }} \ | ||
| --csv=results/performance/current \ | ||
| --html=reports/performance/report.html | ||
| - name: Generate performance report | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| python scripts/analyze_performance.py \ | ||
| --baseline results/performance/baseline_metrics.json \ | ||
| --current results/performance/current_metrics.json \ | ||
| --output reports/performance/regression_report.html | ||
| - name: Upload performance report | ||
| uses: actions/upload-artifact@v3 | ||
| with: | ||
| name: performance-report | ||
| path: | | ||
| reports/performance/**/* | ||
| results/performance/*.html | ||
| results/performance/*.json | ||
| retention-days: 7 | ||
| - name: Check for performance regressions | ||
| id: check_perf | ||
| continue-on-error: true | ||
| run: | | ||
| . .venv/bin/activate | ||
| python scripts/check_performance.py \ | ||
| --baseline results/performance/baseline_metrics.json \ | ||
| --current results/performance/current_metrics.json \ | ||
| --threshold 0.1 # 10%以上のパフォーマンス低下を検知 | ||
| - name: Fail on performance regression | ||
| if: steps.check_perf.outcome == 'failure' | ||
| run: | | ||
| echo "::error::Performance regression detected!" | ||
| exit 1 | ||
| env: | ||
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db | ||
| - name: Create required directories | ||
| run: | | ||
| mkdir -p flet-multiplatform-app/results | ||
| mkdir -p flet-multiplatform-app/reports | ||
| - name: Start application in background | ||
| env: | ||
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db | ||
| APP_ENV: test | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| nohup uvicorn src.backend.app.main:app --host 0.0.0.0 --port 8000 > app.log 2>&1 & | ||
| echo $! > app.pid | ||
| echo "Waiting for application to start..." | ||
| sleep 5 | ||
| - name: Run performance analysis | ||
| if: always() | ||
| run: | | ||
| source .venv/bin/activate | ||
| cd flet-multiplatform-app | ||
| echo "Running performance analysis..." | ||
| python scripts/analyze_performance.py --test-type all | ||
| - name: Upload performance test results | ||
| if: always() | ||
| uses: actions/upload-artifact@v3 | ||
| with: | ||
| name: performance-test-results | ||
| path: | | ||
| flet-multiplatform-app/results/* | ||
| flet-multiplatform-app/reports/* | ||
| - name: Stop application | ||
| if: always() | ||
| run: | | ||
| if [ -f flet-multiplatform-app/app.pid ]; then | ||
| kill $(cat flet-multiplatform-app/app.pid) || true | ||
| rm -f flet-multiplatform-app/app.pid | ||
| fi | ||
| deploy: | ||
| name: Deploy | ||
| runs-on: ubuntu-latest | ||
| needs: build | ||
| if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| - name: Download package artifact | ||
| uses: actions/download-artifact@v3 | ||
| with: | ||
| name: python-package | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' | ||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install twine | ||
| - name: Publish to PyPI | ||
| env: | ||
| TWINE_USERNAME: __token__ | ||
| TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | ||
| run: | | ||
| twine upload --skip-existing dist/* | ||
| notify: | ||
| name: Notify | ||
| runs-on: ubuntu-latest | ||
| needs: [test, lint, security, build, performance-test] | ||
| if: always() | ||
| steps: | ||
| - name: Send notification | ||
| uses: rtCamp/action-slack-notify@v2 | ||
| env: | ||
| SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} | ||
| SLACK_TITLE: "CI/CD Pipeline ${{ job.status }}" | ||
| SLACK_MESSAGE: "${{ github.workflow }} workflow run #${{ github.run_number }} has ${{ job.status }} for ${{ github.ref }}." | ||
| SLACK_COLOR: ${{ job.status == 'success' && 'good' || 'danger' }} | ||