name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
release:
types: [ published ]
env:
PYTHON_VERSION: '3.11'
NODE_VERSION: '18'
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: anydocs_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7
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 ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Create test configuration
run: |
cat > config_test.yaml << EOF
server:
host: "localhost"
port: 8000
debug: true
database:
url: "postgresql://postgres:postgres@localhost:5432/anydocs_test"
echo: false
redis:
url: "redis://localhost:6379/1"
logging:
level: "INFO"
EOF
- name: Run linting
run: |
flake8 src/ tests/ --max-line-length=120 --exclude=__pycache__
black --check src/ tests/
isort --check-only src/ tests/
- name: Run type checking
run: |
mypy src/anydocs_mcp
- name: Run security scan
run: |
bandit -r src/ -f json -o bandit-report.json || true
safety check --json --output safety-report.json || true
- name: Run unit tests
run: |
pytest tests/unit/ -v --cov=src/anydocs_mcp --cov-report=xml --cov-report=html
env:
ANYDOCS_CONFIG: config_test.yaml
ANYDOCS_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/anydocs_test
ANYDOCS_REDIS_URL: redis://localhost:6379/1
- name: Run integration tests
run: |
pytest tests/integration/ -v
env:
ANYDOCS_CONFIG: config_test.yaml
ANYDOCS_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/anydocs_test
ANYDOCS_REDIS_URL: redis://localhost:6379/1
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
- name: Upload test artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: test-results-${{ matrix.python-version }}
path: |
htmlcov/
bandit-report.json
safety-report.json
build:
name: Build Docker Image
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
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
security:
name: Security Scan
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ghcr.io/${{ github.repository }}:${{ github.sha }}
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: [test, build]
if: github.ref == 'refs/heads/develop'
environment: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to staging
run: |
echo "Deploying to staging environment..."
# Add staging deployment commands here
# e.g., kubectl apply, docker-compose, etc.
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [test, build, security]
if: github.event_name == 'release'
environment: production
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: |
echo "Deploying to production environment..."
# Add production deployment commands here
# e.g., kubectl apply, docker-compose, etc.
- name: Create deployment notification
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
text: |
AnyDocs MCP Server deployed to production
Version: ${{ github.event.release.tag_name }}
Commit: ${{ github.sha }}
cleanup:
name: Cleanup
runs-on: ubuntu-latest
needs: [deploy-staging, deploy-production]
if: always()
steps:
- name: Clean up old images
run: |
echo "Cleaning up old container images..."
# Add cleanup commands here