Week07 code added. #11
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
| # week07/.github/workflows/backend_ci.yml | |
| name: Backend CI - Code Quality & Tests | |
| # Trigger the workflow on pushes to the 'main' branch | |
| # You can also add 'pull_request:' to run on PRs | |
| on: | |
| # Manual trigger | |
| workflow_dispatch: | |
| # Automatically on pushes to main branch | |
| push: | |
| branches: | |
| - main | |
| paths: # Only trigger if changes are in backend directories | |
| - 'backend/**' | |
| - '.github/workflows/backend_ci.yml' # Trigger if this workflow file changes | |
| jobs: | |
| # Define a matrix job to run checks for each backend service concurrently | |
| test_and_lint_backends: | |
| runs-on: ubuntu-latest # Use a GitHub-hosted runner | |
| services: | |
| # Product DB container | |
| product_db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: products | |
| # Make pg_isready available so the service is healthy before tests run | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: | |
| - 5432:5432 | |
| # Order DB | |
| order_db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: orders | |
| ports: | |
| - 5433:5432 | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| # Customer DB | |
| customer_db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: customers | |
| ports: | |
| - 5434:5432 | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| steps: | |
| # 1. Checkout the repository code to the runner | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 # Action to check out your repository code | |
| # 2. Set up Python environment | |
| - name: Set up Python 3.10 | |
| uses: actions/setup-python@v5 # Action to set up Python environment | |
| with: | |
| python-version: '3.10' | |
| # 3. Install dependencies and run code quality checks | |
| - name: Install dependencies | |
| run: | # Use a multi-line script to install pip dependencies | |
| pip install --upgrade pip | |
| # Loop through each backend service folder | |
| for req in backend/*/requirements.txt; do | |
| echo "Installing $req" | |
| pip install -r "$req" | |
| done | |
| # Install CI tools | |
| pip install isort flake8 pytest | |
| # 4. Verify import ordering with isort | |
| - name: Run isort (Import Sort) | |
| # Check and enforce consistent import ordering | |
| # The '--check-only' flag ensures it doesn't modify files, but fails if changes are needed. | |
| run: isort --check-only backend | |
| # 5. Run tests for product service | |
| - name: Run product_service tests | |
| working-directory: backend/product_service | |
| env: | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5432 | |
| POSTGRES_DB: products | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| run: | | |
| pytest tests --maxfail=1 --disable-warnings -q | |
| # 6. Run tests for order service | |
| - name: Run order_service tests | |
| working-directory: backend/order_service | |
| env: | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5433 | |
| POSTGRES_DB: orders | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| run: | | |
| pytest tests --maxfail=1 --disable-warnings -q | |
| # 7. Run tests for customer service | |
| - name: Run customer_service tests | |
| working-directory: backend/customer_service | |
| env: | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5434 | |
| POSTGRES_DB: customers | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| run: | | |
| pytest tests --maxfail=1 --disable-warnings -q |