Files
karaoke-gen/.github/workflows/test.yml
T
Andrew Beveridge 58ccc4324e Optimize CI/CD: faster builds with base image and Cloud Build
- Add .gcloudignore to reduce upload from 43K files to 317 files (99% less)
- Create Dockerfile.base with pre-built dependencies (~15 min, only when deps change)
- Update Dockerfile to use base image (app builds now ~1-2 min)
- Upgrade Cloud Build to N1_HIGHCPU_32 for faster compilation
- Move Docker build from GitHub Actions to Cloud Build (faster GCP network)
- Fix signed URL generation for Cloud Run (use IAM signBlob API)
- Update submit-job.sh to poll for review completion instead of waiting for Enter
- Sync Cloud Run deploy settings (16Gi RAM, 8 CPU, gen2, proper secrets)
2025-12-08 03:00:22 -05:00

216 lines
6.2 KiB
YAML

name: Backend Tests
on:
push:
branches: [ main, replace-modal-with-google-cloud ]
pull_request:
branches: [ main ]
jobs:
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- name: Install dependencies
run: |
pip install -r backend/requirements.txt
pip install -e .
- name: Run unit tests
env:
GOOGLE_CLOUD_PROJECT: test-project
GCS_BUCKET_NAME: test-bucket
FIRESTORE_COLLECTION: jobs
ENVIRONMENT: test
run: |
PYTHONPATH=$GITHUB_WORKSPACE:$PYTHONPATH pytest \
backend/tests/test_models.py \
backend/tests/test_job_manager.py \
backend/tests/test_file_upload.py \
backend/tests/test_services.py \
-v --tb=short
emulator-tests:
name: Emulator Integration Tests
runs-on: ubuntu-latest
steps:
- name: Free disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf /usr/local/share/boost
df -h
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- name: Set up Java 21 (required by Firestore emulator)
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
version: 'latest'
- name: Install Firestore emulator
run: |
gcloud components install beta cloud-firestore-emulator --quiet
- name: Install dependencies
run: |
pip install -r backend/requirements.txt
pip install -e .
- name: Start Docker for GCS emulator
run: |
docker pull fsouza/fake-gcs-server:latest
- name: Run emulator integration tests
run: |
chmod +x scripts/*.sh
./scripts/run-emulator-tests.sh
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: backend/htmlcov/
retention-days: 7
lint:
name: Code Quality
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
pip install -r backend/requirements.txt
pip install -e .
pip install flake8 black isort
- name: Run validation (syntax check only)
run: |
# Skip full validation as it requires GCP credentials
# Just check Python syntax and imports
python -m py_compile backend/main.py backend/config.py
echo "✅ Basic syntax validation passed"
deploy:
name: Deploy via Cloud Build
runs-on: ubuntu-latest
needs: [unit-tests, emulator-tests, lint]
# Only deploy on push to the branch (not on PRs)
if: github.event_name == 'push' && github.ref == 'refs/heads/replace-modal-with-google-cloud'
permissions:
contents: read
id-token: write # Required for Workload Identity Federation
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Authenticate to Google Cloud (Workload Identity)
id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
version: 'latest'
- name: Trigger Cloud Build
run: |
echo "🚀 Triggering Cloud Build for deployment..."
echo "Commit: ${{ github.sha }}"
# Submit build and wait for completion
BUILD_ID=$(gcloud builds submit \
--config=cloudbuild.yaml \
--project=nomadkaraoke \
--substitutions=_COMMIT_SHA=${{ github.sha }} \
--format='value(id)' \
--async \
.)
echo "Build ID: $BUILD_ID"
echo "Build URL: https://console.cloud.google.com/cloud-build/builds/$BUILD_ID?project=nomadkaraoke"
# Wait for build to complete
echo "Waiting for build to complete..."
gcloud builds log $BUILD_ID --project=nomadkaraoke --stream
# Check build status
BUILD_STATUS=$(gcloud builds describe $BUILD_ID --project=nomadkaraoke --format='value(status)')
if [ "$BUILD_STATUS" != "SUCCESS" ]; then
echo "❌ Build failed with status: $BUILD_STATUS"
exit 1
fi
echo "✅ Build completed successfully"
- name: Verify deployment
run: |
SERVICE_URL=$(gcloud run services describe karaoke-backend \
--region us-central1 \
--format 'value(status.url)')
echo "Service URL: $SERVICE_URL"
# Wait a few seconds for the service to be ready
sleep 10
# Test health endpoint
curl -f "$SERVICE_URL/api/health" || exit 1
echo "✅ Deployment verified - service is healthy"
- name: Summary
if: success()
run: |
echo "🎉 Deployment successful!"
echo "Commit: ${{ github.sha }}"
echo "Branch: ${{ github.ref_name }}"
echo "Run: ${{ github.run_number }}"