|
| 1 | + |
| 2 | +name: CI — API Service |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }}/api |
| 13 | + |
| 14 | +jobs: |
| 15 | + lint: |
| 16 | + name: Lint & Import Check (API) |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + ref: ${{ github.head_ref || github.ref_name }} |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Set up Python |
| 28 | + uses: actions/setup-python@v5 |
| 29 | + with: |
| 30 | + python-version: "3.11" |
| 31 | + cache: "pip" |
| 32 | + |
| 33 | + - name: Install lint dependencies |
| 34 | + run: | |
| 35 | + pip install flake8 black isort |
| 36 | + [ -f api/requirements.txt ] && pip install -r api/requirements.txt || true |
| 37 | +
|
| 38 | + - name: Black auto-format |
| 39 | + run: black api/ |
| 40 | + |
| 41 | + - name: isort auto-fix import order |
| 42 | + run: isort api/ |
| 43 | + |
| 44 | + - name: Commit formatting fixes (if any) |
| 45 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 46 | + with: |
| 47 | + commit_message: "style: auto-format api/ with black & isort [skip ci]" |
| 48 | + file_pattern: "api/**/*.py" |
| 49 | + |
| 50 | + - name: Flake8 lint |
| 51 | + run: flake8 api/ --max-line-length=100 --ignore=E203,W503 |
| 52 | + |
| 53 | + - name: Python import check (catch broken imports) |
| 54 | + run: | |
| 55 | + cd api |
| 56 | + python -c "import ast, sys, pathlib |
| 57 | + errors = [] |
| 58 | + for f in pathlib.Path('.').rglob('*.py'): |
| 59 | + try: |
| 60 | + ast.parse(f.read_text()) |
| 61 | + except SyntaxError as e: |
| 62 | + errors.append(f'{f}: {e}') |
| 63 | + if errors: |
| 64 | + print('\n'.join(errors)); sys.exit(1) |
| 65 | + print('All Python files parsed OK')" |
| 66 | +
|
| 67 | + build-and-push: |
| 68 | + name: Build & Push API Image |
| 69 | + runs-on: ubuntu-latest |
| 70 | + needs: lint |
| 71 | + permissions: |
| 72 | + contents: read |
| 73 | + packages: write |
| 74 | + |
| 75 | + steps: |
| 76 | + - name: Checkout code |
| 77 | + uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Log in to GitHub Container Registry |
| 80 | + uses: docker/login-action@v3 |
| 81 | + with: |
| 82 | + registry: ${{ env.REGISTRY }} |
| 83 | + username: ${{ github.actor }} |
| 84 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + |
| 86 | + - name: Extract Docker metadata |
| 87 | + id: meta |
| 88 | + uses: docker/metadata-action@v5 |
| 89 | + with: |
| 90 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 91 | + tags: | |
| 92 | + type=sha,prefix=sha- |
| 93 | + type=raw,value=latest,enable={{is_default_branch}} |
| 94 | +
|
| 95 | + - name: Build and push API Docker image |
| 96 | + uses: docker/build-push-action@v5 |
| 97 | + with: |
| 98 | + context: . |
| 99 | + file: Dockerfile.api |
| 100 | + push: ${{ github.ref == 'refs/heads/main' }} |
| 101 | + tags: ${{ steps.meta.outputs.tags }} |
| 102 | + labels: ${{ steps.meta.outputs.labels }} |
| 103 | + cache-from: type=gha |
| 104 | + cache-to: type=gha,mode=max |
0 commit comments