name: AcuPedia API CI on: push: branches: - main - master - develop - 'release/**' pull_request: workflow_dispatch: permissions: contents: read jobs: tests: name: Tests runs-on: ubuntu-latest steps: - name: Check out source uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.12" cache: pip - name: Install project and test dependencies run: python -m pip install --upgrade pip && python -m pip install -e '.[dev]' - name: Run test suite run: python -m pytest env: ENVIRONMENT: test JWT_SECRET: ci-only-test-secret-key-at-least-32-chars REDIS_CACHE_ENABLED: "false" coverage: name: Test coverage runs-on: ubuntu-latest steps: - name: Check out source uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.12" cache: pip - name: Install project and test dependencies run: python -m pip install --upgrade pip && python -m pip install -e '.[dev]' - name: Run tests with coverage run: >- python -m pytest --cov=app --cov-report=term-missing --cov-report=xml:coverage.xml --cov-report=html:htmlcov env: ENVIRONMENT: test JWT_SECRET: ci-only-test-secret-key-at-least-32-chars REDIS_CACHE_ENABLED: "false" - name: Add coverage summary if: always() run: | if [ -f coverage.xml ]; then python -c 'import xml.etree.ElementTree as ET; r=ET.parse("coverage.xml").getroot(); rate=float(r.attrib.get("line-rate", 0))*100; print("## Coverage\n\nLine coverage: **{:.2f}%**".format(rate))' >> "$GITHUB_STEP_SUMMARY" fi deployment-test: name: Build, deploy, and smoke test runs-on: ci-docker needs: - tests - coverage steps: - name: Check out source uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.12" cache: pip - name: Install project and runtime dependencies run: python -m pip install --upgrade pip && python -m pip install -e . - name: Start isolated PostgreSQL for container deployment run: | docker network create acupedia-ci || true docker rm -f acupedia-postgres-ci >/dev/null 2>&1 || true docker run -d --name acupedia-postgres-ci --network acupedia-ci \ -p 127.0.0.1:15432:5432 \ -e POSTGRES_DB=acupedia_ci \ -e POSTGRES_USER=acupedia \ -e POSTGRES_PASSWORD=ci-password \ postgres:16 for attempt in $(seq 1 30); do if docker exec acupedia-postgres-ci pg_isready -U acupedia -d acupedia_ci; then exit 0 fi sleep 2 done docker logs acupedia-postgres-ci exit 1 - name: Apply database migrations run: python -m alembic upgrade head env: DATABASE_URL: postgresql+asyncpg://acupedia:ci-password@127.0.0.1:15432/acupedia_ci - name: Seed auth and commerce run: | acupedia-bootstrap auth acupedia-bootstrap commerce env: DATABASE_URL: postgresql+asyncpg://acupedia:ci-password@127.0.0.1:15432/acupedia_ci - name: Validate seeded database run: acupedia-bootstrap validate env: DATABASE_URL: postgresql+asyncpg://acupedia:ci-password@127.0.0.1:15432/acupedia_ci - name: Build API image run: docker build --pull -t "$CI_IMAGE" . env: CI_IMAGE: acupedia-api:${{ github.sha }} - name: Deploy API container for acceptance checks run: | docker rm -f acupedia-api-ci >/dev/null 2>&1 || true docker run -d --name acupedia-api-ci --network acupedia-ci -p 127.0.0.1:18000:8000 \ -e DATABASE_URL=postgresql+asyncpg://acupedia:ci-password@acupedia-postgres-ci:5432/acupedia_ci \ -e CACHE_ENABLED=false \ -e ENVIRONMENT=test \ -e JWT_SECRET=ci-only-test-secret-key-at-least-32-chars \ "$CI_IMAGE" env: CI_IMAGE: acupedia-api:${{ github.sha }} - name: Run HTTP smoke tests against deployed container run: | if ! python scripts/smoke_deployed_api.py; then docker logs acupedia-api-ci exit 1 fi env: ACUPEDIA_BASE_URL: http://127.0.0.1:18000 - name: Install isolated AWS CLI if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/')) run: | python -m venv .artifact-venv .artifact-venv/bin/pip install --disable-pip-version-check awscli - name: Package and publish immutable S3 artifact if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/')) run: bash scripts/publish_s3_artifact.sh env: AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: ${{ secrets.S3_REGION || 'us-east-1' }} AWS_EC2_METADATA_DISABLED: "true" S3_ENDPOINT_URL: ${{ secrets.S3_ENDPOINT_URL }} S3_BUCKET: ${{ secrets.S3_BUCKET }} CI_IMAGE: acupedia-api:${{ github.sha }} GITHUB_SHA: ${{ github.sha }} GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_REF_NAME: ${{ github.ref_name }} GITHUB_RUN_ID: ${{ github.run_id }} - name: Stop CI containers and network if: always() run: | docker rm -f acupedia-api-ci acupedia-postgres-ci >/dev/null 2>&1 || true docker network rm acupedia-ci >/dev/null 2>&1 || true