896329ccb7
Agent-Logs-Url: https://github.com/christianlouis/InboxConverge/sessions/3fdf6219-3e1f-4867-b74d-efb6e4fe7e72 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
283 lines
8.9 KiB
YAML
283 lines
8.9 KiB
YAML
name: CI
|
||
|
||
on:
|
||
push:
|
||
branches: [main, develop]
|
||
tags:
|
||
- 'v*'
|
||
pull_request:
|
||
branches: [main, develop]
|
||
schedule:
|
||
- cron: '0 0 * * 0' # Weekly on Sunday (security scans)
|
||
workflow_dispatch:
|
||
|
||
env:
|
||
GHCR_REGISTRY: ghcr.io
|
||
PRIVATE_REGISTRY: registry.cklnet.com
|
||
|
||
jobs:
|
||
# ── Phase 1: Lint ──────────────────────────────────────────────────────
|
||
lint:
|
||
name: Lint
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
contents: read
|
||
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- name: Set up Python
|
||
uses: actions/setup-python@v6
|
||
with:
|
||
python-version: '3.12'
|
||
|
||
- name: Install Python linting tools
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install black ruff mypy
|
||
pip install -r backend/requirements.txt
|
||
|
||
- name: Check code formatting with Black
|
||
run: black --check backend/
|
||
|
||
- name: Lint with Ruff
|
||
run: ruff check backend/
|
||
|
||
- name: Type check with mypy
|
||
run: mypy backend/app --ignore-missing-imports
|
||
continue-on-error: true
|
||
|
||
- name: Set up Node.js
|
||
uses: actions/setup-node@v6
|
||
with:
|
||
node-version: '20.19.0'
|
||
cache: 'npm'
|
||
cache-dependency-path: frontend/package-lock.json
|
||
|
||
- name: Install frontend dependencies
|
||
run: npm ci
|
||
working-directory: frontend
|
||
|
||
- name: Lint frontend with ESLint
|
||
run: npm run lint
|
||
working-directory: frontend
|
||
|
||
# ── Phase 2: Test ──────────────────────────────────────────────────────
|
||
test:
|
||
name: Test
|
||
needs: lint
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
contents: read
|
||
pull-requests: write
|
||
|
||
services:
|
||
postgres:
|
||
image: postgres:15
|
||
env:
|
||
POSTGRES_USER: postgres
|
||
POSTGRES_PASSWORD: postgres
|
||
POSTGRES_DB: inbox_converge_test
|
||
options: >-
|
||
--health-cmd pg_isready
|
||
--health-interval 10s
|
||
--health-timeout 5s
|
||
--health-retries 5
|
||
ports:
|
||
- 5432:5432
|
||
|
||
redis:
|
||
image: redis:7-alpine
|
||
options: >-
|
||
--health-cmd "redis-cli ping"
|
||
--health-interval 10s
|
||
--health-timeout 5s
|
||
--health-retries 5
|
||
ports:
|
||
- 6379:6379
|
||
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- name: Set up Python
|
||
uses: actions/setup-python@v6
|
||
with:
|
||
python-version: '3.12'
|
||
cache: 'pip'
|
||
|
||
- name: Install dependencies
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install -r backend/requirements.txt
|
||
pip install pytest pytest-asyncio pytest-cov httpx
|
||
|
||
- name: Run tests with coverage
|
||
env:
|
||
DATABASE_URL: postgresql+asyncpg://postgres:postgres@localhost:5432/inbox_converge_test
|
||
REDIS_URL: redis://localhost:6379/0
|
||
SECRET_KEY: test-secret-key-for-ci-cd-at-least-32-chars
|
||
ENCRYPTION_KEY: test-encryption-key-for-ci-cd-at-least-32-chars
|
||
run: |
|
||
cd backend
|
||
pytest tests/ -v --cov=app --cov-report=xml --cov-report=term
|
||
|
||
- name: Upload coverage to Codecov
|
||
uses: codecov/codecov-action@v5
|
||
with:
|
||
file: ./backend/coverage.xml
|
||
flags: unittests
|
||
name: codecov-umbrella
|
||
fail_ci_if_error: false
|
||
|
||
# ── Phase 3: Security ──────────────────────────────────────────────────
|
||
security:
|
||
name: Security
|
||
needs: test
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
contents: read
|
||
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- name: Set up Python
|
||
uses: actions/setup-python@v6
|
||
with:
|
||
python-version: '3.12'
|
||
|
||
- name: Install dependencies
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install bandit safety
|
||
pip install -r backend/requirements.txt
|
||
|
||
- name: Run Bandit security scan
|
||
run: bandit -r backend/app -ll
|
||
continue-on-error: true
|
||
|
||
- name: Check dependencies for known vulnerabilities
|
||
run: safety check --json
|
||
continue-on-error: true
|
||
|
||
# ── Phase 4: Build ─────────────────────────────────────────────────────
|
||
build:
|
||
name: Build & Push Docker Image
|
||
needs: security
|
||
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
|
||
runs-on: ubuntu-latest
|
||
permissions:
|
||
contents: read
|
||
packages: write
|
||
|
||
strategy:
|
||
matrix:
|
||
include:
|
||
- context: ./backend
|
||
image_name: inboxconverge/backend
|
||
- context: ./frontend
|
||
image_name: inboxconverge/frontend
|
||
|
||
steps:
|
||
- name: Checkout repository
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Set up Docker Buildx
|
||
uses: docker/setup-buildx-action@v4
|
||
|
||
- name: Log in to GitHub Container Registry
|
||
uses: docker/login-action@v3
|
||
with:
|
||
registry: ${{ env.GHCR_REGISTRY }}
|
||
username: ${{ github.actor }}
|
||
password: ${{ secrets.GITHUB_TOKEN }}
|
||
|
||
# Requires PRIVATE_REGISTRY_USERNAME and PRIVATE_REGISTRY_PASSWORD secrets
|
||
- name: Log in to private registry
|
||
uses: docker/login-action@v3
|
||
with:
|
||
registry: ${{ env.PRIVATE_REGISTRY }}
|
||
username: ${{ secrets.PRIVATE_REGISTRY_USERNAME }}
|
||
password: ${{ secrets.PRIVATE_REGISTRY_PASSWORD }}
|
||
|
||
- name: Extract metadata (tags, labels)
|
||
id: meta
|
||
uses: docker/metadata-action@v5
|
||
with:
|
||
images: |
|
||
${{ env.GHCR_REGISTRY }}/${{ github.repository_owner }}/${{ matrix.image_name }}
|
||
${{ env.PRIVATE_REGISTRY }}/${{ matrix.image_name }}
|
||
tags: |
|
||
type=semver,pattern={{version}}
|
||
type=semver,pattern={{major}}.{{minor}}
|
||
type=semver,pattern={{major}}
|
||
type=sha
|
||
type=raw,value=latest,enable={{is_default_branch}}
|
||
|
||
- name: Build and push Docker image
|
||
id: build-push
|
||
uses: docker/build-push-action@v5
|
||
with:
|
||
context: ${{ matrix.context }}
|
||
push: true
|
||
tags: ${{ steps.meta.outputs.tags }}
|
||
labels: ${{ steps.meta.outputs.labels }}
|
||
cache-from: type=gha
|
||
cache-to: type=gha,mode=max
|
||
platforms: linux/amd64,linux/arm64
|
||
|
||
# ── Phase 5: GitOps – update preprod k8s manifest ─────────────────────────
|
||
update-k8s-manifest:
|
||
name: Update Preprod K8s Manifest
|
||
runs-on: ubuntu-latest
|
||
needs: [build]
|
||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||
permissions:
|
||
contents: read
|
||
|
||
steps:
|
||
- name: Compute image tags
|
||
id: tag
|
||
run: |
|
||
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||
echo "backend_image=ghcr.io/${{ github.repository_owner }}/inboxconverge/backend:main-${SHORT_SHA}" >> "$GITHUB_OUTPUT"
|
||
echo "frontend_image=ghcr.io/${{ github.repository_owner }}/inboxconverge/frontend:main-${SHORT_SHA}" >> "$GITHUB_OUTPUT"
|
||
echo "short_sha=${SHORT_SHA}" >> "$GITHUB_OUTPUT"
|
||
|
||
- name: Checkout k8s-cluster-state
|
||
uses: actions/checkout@v4
|
||
with:
|
||
repository: christianlouis/k8s-cluster-state
|
||
token: ${{ secrets.GH_PAT }}
|
||
path: k8s-cluster-state
|
||
|
||
- name: Update backend image tag in preprod manifest
|
||
uses: mikefarah/yq@v4.44.6
|
||
env:
|
||
IMAGE: ${{ steps.tag.outputs.backend_image }}
|
||
with:
|
||
cmd: |
|
||
yq -i '(.. | select(tag == "!!str") | select(test("^ghcr\\.io/christianlouis/inboxconverge/backend:"))) = strenv(IMAGE)' \
|
||
k8s-cluster-state/apps/gmail-puller/preprod/gmail-puller-stack.yaml
|
||
|
||
- name: Update frontend image tag in preprod manifest
|
||
uses: mikefarah/yq@v4.44.6
|
||
env:
|
||
IMAGE: ${{ steps.tag.outputs.frontend_image }}
|
||
with:
|
||
cmd: |
|
||
yq -i '(.. | select(tag == "!!str") | select(test("^ghcr\\.io/christianlouis/inboxconverge/frontend:"))) = strenv(IMAGE)' \
|
||
k8s-cluster-state/apps/gmail-puller/preprod/gmail-puller-stack.yaml
|
||
|
||
- name: Commit and push manifest update
|
||
run: |
|
||
cd k8s-cluster-state
|
||
git config user.name "github-actions[bot]"
|
||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||
git add apps/gmail-puller/preprod/gmail-puller-stack.yaml
|
||
if git diff --staged --quiet; then
|
||
echo "No changes to commit"
|
||
else
|
||
git commit -m "chore(preprod): update inboxconverge images to ${{ steps.tag.outputs.short_sha }}"
|
||
git push
|
||
fi
|