# Preloop Open Source - CI Pipeline
# Runs unit tests on pull requests and pushes to main
# Builds and pushes Docker images on releases and main branch
name: CI
on:
push:
branches: [main]
tags: ['v*']
pull_request:
branches: [main]
env:
PYTHON_VERSION: "3.11"
NODE_VERSION: "22"
REGISTRY_GHCR: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ==========================================================================
# Backend Tests
# ==========================================================================
lint:
name: Lint & Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install Python dependencies
run: |
pip install -U pip pre-commit ruff
pip install -e ".[dev]"
- name: Install frontend dependencies
run: cd frontend && npm ci
- name: Run pre-commit
run: pre-commit run --all-files
- name: Run ruff
run: ruff check .
test-backend:
name: Backend Tests
runs-on: ubuntu-latest
services:
postgres:
image: pgvector/pgvector:pg16
env:
POSTGRES_DB: test_db
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_password
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgresql://test_user:test_password@localhost:5432/test_db
OPENAI_API_KEY: mock_key
ANTHROPIC_API_KEY: mock_key
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install -U pip pytest pytest-cov pytest-asyncio loguru
pip install -e ".[dev]"
- name: Initialize database
run: python scripts/init_db.py --force
- name: Run tests
run: |
pytest -v \
--ignore=backend/tests/integration \
--cov=backend/preloop \
--cov-report=xml \
--junitxml=test-results.xml
- name: Upload coverage
uses: codecov/codecov-action@v4
if: github.event_name != 'pull_request'
with:
files: ./coverage.xml
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: backend-test-results
path: test-results.xml
# ==========================================================================
# Frontend Tests
# ==========================================================================
test-frontend:
name: Frontend Tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
run: npm ci
- name: Check formatting
run: npm run format:check
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
- name: Run tests
run: npm run test
# ==========================================================================
# Build and Push Docker Images
# ==========================================================================
build-and-push:
name: Build & Push Docker Images
runs-on: ubuntu-latest
needs: [lint, test-backend, test-frontend]
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY_GHCR }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
if: github.event_name != 'pull_request' && vars.PUSH_TO_DOCKERHUB == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata for backend image
id: meta-backend
uses: docker/metadata-action@v5
with:
images: |
${{ env.REGISTRY_GHCR }}/preloop/preloop
${{ vars.PUSH_TO_DOCKERHUB == 'true' && format('{0}/preloop', vars.DOCKERHUB_ORG) || '' }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push backend image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Extract metadata for frontend image
id: meta-frontend
uses: docker/metadata-action@v5
with:
images: |
${{ env.REGISTRY_GHCR }}/preloop/console
${{ vars.PUSH_TO_DOCKERHUB == 'true' && format('{0}/console', vars.DOCKERHUB_ORG) || '' }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push frontend image
uses: docker/build-push-action@v6
with:
context: ./frontend
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-frontend.outputs.tags }}
labels: ${{ steps.meta-frontend.outputs.labels }}
build-args: |
BRAND=preloop
cache-from: type=gha
cache-to: type=gha,mode=max