mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
Cost optimization: Migrate from expensive Cloud Build ($100+/month) to free GitHub Actions with registry caching. Changes: - Add .dockerignore to reduce build context (~1GB → ~10MB) - Rewrite deploy-backend job to use docker/build-push-action - Use Artifact Registry as persistent layer cache (mode=max) - Keep cloudbuild*.yaml as fallback for manual builds - Add documentation for new build process Benefits: - $0 monthly cost (public repo, free GitHub Actions) - Faster builds with registry-cached layers - Simpler deployment pipeline (all in one workflow) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
888 lines
32 KiB
YAML
888 lines
32 KiB
YAML
name: CI
|
|
|
|
on:
|
|
push:
|
|
pull_request:
|
|
branches: [main]
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: write
|
|
pages: write
|
|
id-token: write
|
|
|
|
concurrency:
|
|
group: "ci-${{ github.ref }}"
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
# ============================================
|
|
# PATH FILTERING - Determine what changed
|
|
# ============================================
|
|
|
|
changes:
|
|
name: "Detect Changes"
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
frontend: ${{ steps.filter.outputs.frontend }}
|
|
backend: ${{ steps.filter.outputs.backend }}
|
|
package: ${{ steps.filter.outputs.package }}
|
|
infra: ${{ steps.filter.outputs.infra }}
|
|
deps: ${{ steps.filter.outputs.deps }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- uses: dorny/paths-filter@v3
|
|
id: filter
|
|
with:
|
|
filters: |
|
|
frontend:
|
|
- 'frontend/**'
|
|
backend:
|
|
- 'backend/**'
|
|
package:
|
|
- 'karaoke_gen/**'
|
|
- 'lyrics_transcriber_temp/**'
|
|
- 'tests/**'
|
|
infra:
|
|
- 'infrastructure/**'
|
|
deps:
|
|
- 'poetry.lock'
|
|
|
|
- name: Summary
|
|
run: |
|
|
echo "### Change Detection Results" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Area | Changed |" >> $GITHUB_STEP_SUMMARY
|
|
echo "|------|---------|" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Frontend | ${{ steps.filter.outputs.frontend }} |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Backend | ${{ steps.filter.outputs.backend }} |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Package | ${{ steps.filter.outputs.package }} |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Infrastructure | ${{ steps.filter.outputs.infra }} |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Dependencies | ${{ steps.filter.outputs.deps }} |" >> $GITHUB_STEP_SUMMARY
|
|
|
|
# ============================================
|
|
# FRONTEND TESTS
|
|
# ============================================
|
|
|
|
frontend-unit-tests:
|
|
name: "Frontend - Unit Tests"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.frontend == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js with caching
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '20'
|
|
cache: 'npm'
|
|
cache-dependency-path: frontend/package-lock.json
|
|
|
|
- name: Install dependencies
|
|
working-directory: frontend
|
|
run: npm ci --legacy-peer-deps
|
|
|
|
- name: Run unit tests with coverage
|
|
working-directory: frontend
|
|
run: npm run test:ci
|
|
|
|
- name: Upload coverage report
|
|
if: always()
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: frontend-coverage
|
|
path: frontend/coverage/
|
|
retention-days: 7
|
|
|
|
frontend-build:
|
|
name: "Frontend - Build"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.frontend == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js with caching
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '20'
|
|
cache: 'npm'
|
|
cache-dependency-path: frontend/package-lock.json
|
|
|
|
- name: Install dependencies
|
|
working-directory: frontend
|
|
run: npm ci --legacy-peer-deps
|
|
|
|
- name: Build frontend
|
|
working-directory: frontend
|
|
env:
|
|
NEXT_PUBLIC_API_URL: https://api.nomadkaraoke.com
|
|
run: npm run build
|
|
|
|
# ============================================
|
|
# PACKAGE TESTS (karaoke-gen Python CLI)
|
|
# ============================================
|
|
|
|
package-unit-tests:
|
|
name: "Package - Unit Tests (70% coverage)"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
submodules: true
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-package-${{ runner.os }}-py3.13-
|
|
|
|
- name: Install dependencies
|
|
run: poetry install --with dev
|
|
|
|
- name: Install system dependencies
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y ffmpeg
|
|
|
|
- name: Cache spaCy model
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: ~/.cache/spacy
|
|
key: spacy-en_core_web_sm-${{ runner.os }}
|
|
|
|
- name: Install spaCy language models
|
|
run: poetry run python -m spacy download en_core_web_sm
|
|
|
|
- name: Run unit tests
|
|
run: |
|
|
poetry run pytest tests/unit/ -v --cov=karaoke_gen --cov-report=xml --cov-report=term-missing --cov-fail-under=69
|
|
|
|
- name: Upload coverage reports
|
|
uses: codecov/codecov-action@v5
|
|
with:
|
|
token: ${{ secrets.CODECOV_TOKEN }}
|
|
files: ./coverage.xml
|
|
flags: unittests
|
|
name: codecov-umbrella
|
|
|
|
package-integration-tests:
|
|
name: "Package - Integration Tests"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
submodules: true
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-package-${{ runner.os }}-py3.13-
|
|
|
|
- name: Install dependencies
|
|
run: poetry install --with dev
|
|
|
|
- name: Install system dependencies
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y ffmpeg
|
|
|
|
- name: Cache spaCy model
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: ~/.cache/spacy
|
|
key: spacy-en_core_web_sm-${{ runner.os }}
|
|
|
|
- name: Install spaCy language models
|
|
run: poetry run python -m spacy download en_core_web_sm
|
|
|
|
- name: Run integration tests
|
|
run: poetry run pytest tests/integration/ -v
|
|
|
|
package-build:
|
|
name: "Package - Build & Install"
|
|
runs-on: ubuntu-latest
|
|
needs: [changes, package-unit-tests, package-integration-tests]
|
|
if: ${{ always() && (needs.package-unit-tests.result == 'success' || needs.package-unit-tests.result == 'skipped') && (needs.package-integration-tests.result == 'success' || needs.package-integration-tests.result == 'skipped') && (needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main') }}
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
submodules: true
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-package-${{ runner.os }}-py3.13-
|
|
|
|
- name: Build package
|
|
run: poetry build
|
|
|
|
- name: Free disk space
|
|
uses: jlumbroso/free-disk-space@main
|
|
with:
|
|
tool-cache: false
|
|
android: true
|
|
haskell: true
|
|
dotnet: false
|
|
large-packages: false
|
|
docker-images: false
|
|
swap-storage: false
|
|
|
|
- name: Test package installation
|
|
run: |
|
|
pip install dist/*.whl
|
|
python -m karaoke_gen.utils.gen_cli --help
|
|
|
|
- name: Upload build artifacts
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: package-dist
|
|
path: dist/
|
|
retention-days: 1
|
|
|
|
# ============================================
|
|
# BACKEND TESTS (Cloud Run API service)
|
|
# ============================================
|
|
|
|
backend-unit-tests:
|
|
name: "Backend - Unit Tests"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
submodules: false
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-backend-${{ runner.os }}-py3.13-
|
|
|
|
- name: Install dependencies
|
|
run: poetry install
|
|
|
|
- name: Run all backend unit tests
|
|
env:
|
|
GOOGLE_CLOUD_PROJECT: test-project
|
|
GCS_BUCKET_NAME: test-bucket
|
|
FIRESTORE_COLLECTION: jobs
|
|
ENVIRONMENT: test
|
|
run: |
|
|
poetry run pytest \
|
|
backend/tests/ \
|
|
--ignore=backend/tests/emulator \
|
|
-v --tb=short
|
|
|
|
backend-emulator-tests:
|
|
name: "Backend - Emulator Integration Tests"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true' || github.ref == 'refs/heads/main' }}
|
|
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.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-backend-${{ runner.os }}-py3.13-
|
|
|
|
- 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: poetry install
|
|
|
|
- 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: backend-test-results
|
|
path: backend/htmlcov/
|
|
retention-days: 7
|
|
|
|
backend-lint:
|
|
name: "Backend - Code Quality"
|
|
runs-on: ubuntu-latest
|
|
needs: changes
|
|
if: ${{ needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || github.ref == 'refs/heads/main' }}
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
submodules: false
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
with:
|
|
virtualenvs-create: true
|
|
virtualenvs-in-project: true
|
|
|
|
- name: Cache Poetry dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: .venv
|
|
key: venv-lint-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
|
|
restore-keys: |
|
|
venv-lint-${{ runner.os }}-py3.13-
|
|
|
|
- name: Install dependencies
|
|
run: poetry install
|
|
|
|
- name: Run validation (syntax check only)
|
|
run: |
|
|
poetry run python -m py_compile backend/main.py backend/config.py
|
|
echo "✅ Basic syntax validation passed"
|
|
|
|
# ============================================
|
|
# CI GATE - Required status check for PRs
|
|
# ============================================
|
|
|
|
ci-gate:
|
|
name: "CI Gate"
|
|
runs-on: ubuntu-latest
|
|
needs: [frontend-unit-tests, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, backend-lint]
|
|
if: always()
|
|
steps:
|
|
- name: Check results
|
|
run: |
|
|
results=(
|
|
"${{ needs.frontend-unit-tests.result }}"
|
|
"${{ needs.frontend-build.result }}"
|
|
"${{ needs.package-unit-tests.result }}"
|
|
"${{ needs.package-integration-tests.result }}"
|
|
"${{ needs.backend-unit-tests.result }}"
|
|
"${{ needs.backend-emulator-tests.result }}"
|
|
"${{ needs.backend-lint.result }}"
|
|
)
|
|
for result in "${results[@]}"; do
|
|
if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
|
|
echo "❌ CI failed"
|
|
exit 1
|
|
fi
|
|
done
|
|
echo "✅ CI passed"
|
|
|
|
# ============================================
|
|
# DEPLOYMENTS (main branch only)
|
|
# ============================================
|
|
|
|
package-publish:
|
|
name: "Deploy - Publish to PyPI"
|
|
runs-on: ubuntu-latest
|
|
needs: [package-build]
|
|
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
submodules: true
|
|
|
|
- name: Set up Python 3.13
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
|
|
- name: Download build artifacts
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: package-dist
|
|
path: dist/
|
|
|
|
- name: Extract version
|
|
id: extract_version
|
|
run: |
|
|
VERSION=$(poetry version --short)
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "📋 Extracted version: $VERSION"
|
|
|
|
- name: Check if tag exists
|
|
id: check_tag
|
|
run: |
|
|
VERSION="${{ steps.extract_version.outputs.version }}"
|
|
if git ls-remote --tags origin | grep -q "refs/tags/v$VERSION$"; then
|
|
echo "tag_exists=true" >> $GITHUB_OUTPUT
|
|
echo "🏷️ Tag v$VERSION already exists, skipping release"
|
|
else
|
|
echo "tag_exists=false" >> $GITHUB_OUTPUT
|
|
echo "✨ Tag v$VERSION does not exist, proceeding with release"
|
|
fi
|
|
|
|
- name: Publish to PyPI
|
|
if: steps.check_tag.outputs.tag_exists == 'false'
|
|
run: |
|
|
echo "📦 Publishing the following files to PyPI:"
|
|
ls -la dist/
|
|
echo "🚀 Publishing to PyPI..."
|
|
poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }}
|
|
poetry publish
|
|
|
|
- name: Create Git Tag
|
|
if: steps.check_tag.outputs.tag_exists == 'false'
|
|
run: |
|
|
git config user.name github-actions
|
|
git config user.email github-actions@github.com
|
|
git tag -a "v${{ steps.extract_version.outputs.version }}" -m "Release v${{ steps.extract_version.outputs.version }}"
|
|
git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} "v${{ steps.extract_version.outputs.version }}"
|
|
|
|
- name: Create GitHub Release
|
|
if: steps.check_tag.outputs.tag_exists == 'false'
|
|
uses: softprops/action-gh-release@v1
|
|
with:
|
|
tag_name: "v${{ steps.extract_version.outputs.version }}"
|
|
name: "v${{ steps.extract_version.outputs.version }}"
|
|
body: |
|
|
## Changes in v${{ steps.extract_version.outputs.version }}
|
|
|
|
This release was automatically created from the main branch.
|
|
|
|
### Installation
|
|
```bash
|
|
pip install karaoke-gen==${{ steps.extract_version.outputs.version }}
|
|
```
|
|
|
|
### Assets
|
|
- Python package published to PyPI
|
|
- Frontend deployed to GitHub Pages at gen.nomadkaraoke.com
|
|
|
|
For detailed changes, see the commit history.
|
|
draft: false
|
|
prerelease: false
|
|
files: |
|
|
dist/*.whl
|
|
dist/*.tar.gz
|
|
|
|
deploy-frontend:
|
|
name: "Deploy - Frontend (GitHub Pages)"
|
|
runs-on: ubuntu-latest
|
|
needs: [frontend-unit-tests, frontend-build]
|
|
if: ${{ always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && (needs.frontend-unit-tests.result == 'success' || needs.frontend-unit-tests.result == 'skipped') && (needs.frontend-build.result == 'success' || needs.frontend-build.result == 'skipped') }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js with caching
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '20'
|
|
cache: 'npm'
|
|
cache-dependency-path: frontend/package-lock.json
|
|
|
|
- name: Enable Yarn (for lyrics frontend)
|
|
run: corepack enable
|
|
|
|
# ===== Build Main Frontend (Next.js) =====
|
|
- name: Install main frontend dependencies
|
|
working-directory: frontend
|
|
run: npm ci --legacy-peer-deps
|
|
|
|
- name: Build main frontend (Next.js static export)
|
|
working-directory: frontend
|
|
env:
|
|
NEXT_PUBLIC_API_URL: https://api.nomadkaraoke.com
|
|
run: npm run build
|
|
|
|
# ===== Build Lyrics Review Frontend (Vite) =====
|
|
- name: Cache Yarn dependencies (lyrics frontend)
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: lyrics_transcriber_temp/lyrics_transcriber/frontend/node_modules
|
|
key: yarn-lyrics-${{ runner.os }}-${{ hashFiles('lyrics_transcriber_temp/lyrics_transcriber/frontend/yarn.lock') }}
|
|
restore-keys: |
|
|
yarn-lyrics-${{ runner.os }}-
|
|
|
|
- name: Install lyrics frontend dependencies
|
|
working-directory: lyrics_transcriber_temp/lyrics_transcriber/frontend
|
|
run: yarn install --frozen-lockfile
|
|
|
|
- name: Build lyrics frontend (Vite)
|
|
working-directory: lyrics_transcriber_temp/lyrics_transcriber/frontend
|
|
env:
|
|
# Set base path for subpath deployment
|
|
VITE_BASE_PATH: /lyrics/
|
|
run: yarn build --base=/lyrics/
|
|
|
|
- name: Copy lyrics frontend to main output
|
|
run: |
|
|
mkdir -p frontend/out/lyrics
|
|
cp -r lyrics_transcriber_temp/lyrics_transcriber/frontend/dist/* frontend/out/lyrics/
|
|
|
|
# ===== Copy Instrumental Review UI (Static HTML) =====
|
|
- name: Copy instrumental review UI to main output
|
|
run: |
|
|
mkdir -p frontend/out/instrumental
|
|
cp karaoke_gen/instrumental_review/static/index.html frontend/out/instrumental/
|
|
|
|
- name: Setup Pages
|
|
uses: actions/configure-pages@v4
|
|
|
|
- name: Upload artifact
|
|
uses: actions/upload-pages-artifact@v3
|
|
with:
|
|
path: "frontend/out"
|
|
|
|
- name: Deploy to GitHub Pages
|
|
id: deployment
|
|
uses: actions/deploy-pages@v4
|
|
|
|
deploy-backend:
|
|
name: "Deploy - Backend (Cloud Run)"
|
|
runs-on: ubuntu-latest
|
|
needs: [backend-unit-tests, backend-emulator-tests, backend-lint]
|
|
if: ${{ always() && github.ref == 'refs/heads/main' && github.event_name == 'push' && (needs.backend-unit-tests.result == 'success' || needs.backend-unit-tests.result == 'skipped') && (needs.backend-emulator-tests.result == 'success' || needs.backend-emulator-tests.result == 'skipped') && (needs.backend-lint.result == 'success' || needs.backend-lint.result == 'skipped') }}
|
|
permissions:
|
|
contents: read
|
|
id-token: write
|
|
env:
|
|
REGISTRY: us-central1-docker.pkg.dev
|
|
PROJECT_ID: nomadkaraoke
|
|
REPOSITORY: karaoke-repo
|
|
BASE_IMAGE_NAME: karaoke-backend-base
|
|
APP_IMAGE_NAME: karaoke-backend
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
submodules: false
|
|
fetch-depth: 0 # Need full history for tag check
|
|
|
|
- name: Set up Python (for version extraction)
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.13"
|
|
|
|
- name: Install Poetry
|
|
uses: snok/install-poetry@v1
|
|
|
|
- name: Extract version and check if deployment needed
|
|
id: version_check
|
|
run: |
|
|
VERSION=$(poetry version --short)
|
|
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
echo "📋 Current version: $VERSION"
|
|
|
|
if git ls-remote --tags origin | grep -q "refs/tags/v$VERSION$"; then
|
|
echo "needs_deploy=false" >> $GITHUB_OUTPUT
|
|
echo "⏭️ Tag v$VERSION already exists - skipping backend deployment"
|
|
echo " (Bump version in pyproject.toml to trigger a new deployment)"
|
|
else
|
|
echo "needs_deploy=true" >> $GITHUB_OUTPUT
|
|
echo "✨ Tag v$VERSION does not exist - proceeding with deployment"
|
|
fi
|
|
|
|
- name: Authenticate to Google Cloud (Workload Identity)
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
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
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
uses: google-github-actions/setup-gcloud@v2
|
|
with:
|
|
version: "latest"
|
|
|
|
- name: Configure Docker for Artifact Registry
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
run: gcloud auth configure-docker ${{ env.REGISTRY }} --quiet
|
|
|
|
# ============================================
|
|
# Docker Build with BuildKit + Registry Cache
|
|
# This replaces expensive Cloud Build with free GitHub Actions
|
|
# Registry cache persists between builds for fast rebuilds
|
|
# ============================================
|
|
|
|
- name: Set up Docker Buildx
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Calculate base image hash
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
id: base-hash
|
|
run: |
|
|
# Hash both poetry.lock AND Dockerfile.base - rebuild if either changes
|
|
CURRENT_HASH=$(cat poetry.lock backend/Dockerfile.base | sha256sum | cut -d' ' -f1)
|
|
echo "hash=$CURRENT_HASH" >> $GITHUB_OUTPUT
|
|
echo "📋 Base content hash: $CURRENT_HASH"
|
|
|
|
- name: Check if base image exists and is current
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
id: check-base
|
|
run: |
|
|
BASE_IMAGE="${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:latest"
|
|
|
|
# Try to get the existing base image's hash label
|
|
EXISTING_HASH=""
|
|
if docker pull $BASE_IMAGE 2>/dev/null; then
|
|
EXISTING_HASH=$(docker inspect $BASE_IMAGE --format='{{index .Config.Labels "base.content.hash"}}' 2>/dev/null || echo "")
|
|
# Fallback to old label name for backwards compatibility
|
|
if [ -z "$EXISTING_HASH" ]; then
|
|
EXISTING_HASH=$(docker inspect $BASE_IMAGE --format='{{index .Config.Labels "poetry.lock.hash"}}' 2>/dev/null || echo "")
|
|
fi
|
|
echo "Existing base image hash: $EXISTING_HASH"
|
|
else
|
|
echo "⚠️ No existing base image found - will build"
|
|
fi
|
|
|
|
if [ "${{ steps.base-hash.outputs.hash }}" != "$EXISTING_HASH" ]; then
|
|
echo "🔄 Base image needs rebuild (poetry.lock or Dockerfile.base changed)"
|
|
echo "needs_rebuild=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "✅ Base image is up to date"
|
|
echo "needs_rebuild=false" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Free disk space (for base image build)
|
|
if: steps.version_check.outputs.needs_deploy == 'true' && steps.check-base.outputs.needs_rebuild == 'true'
|
|
uses: jlumbroso/free-disk-space@main
|
|
with:
|
|
tool-cache: false
|
|
android: true
|
|
haskell: true
|
|
dotnet: true
|
|
large-packages: true
|
|
docker-images: false
|
|
swap-storage: false
|
|
|
|
- name: Build and push base image (if needed)
|
|
if: steps.version_check.outputs.needs_deploy == 'true' && steps.check-base.outputs.needs_rebuild == 'true'
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
file: backend/Dockerfile.base
|
|
push: true
|
|
tags: |
|
|
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:latest
|
|
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:${{ github.sha }}
|
|
# Registry cache for ALL layers (mode=max) - persists between builds
|
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:cache
|
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:cache,mode=max
|
|
build-args: |
|
|
BUILD_DATE=${{ github.run_id }}
|
|
BUILD_ID=${{ github.sha }}
|
|
BASE_CONTENT_HASH=${{ steps.base-hash.outputs.hash }}
|
|
# Provenance disabled for smaller image and faster push
|
|
provenance: false
|
|
|
|
- name: Build and push app image
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
file: backend/Dockerfile
|
|
push: true
|
|
tags: |
|
|
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:latest
|
|
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:${{ github.sha }}
|
|
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:v${{ steps.version_check.outputs.version }}
|
|
# Registry cache for app layers
|
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:cache
|
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:cache,mode=max
|
|
provenance: false
|
|
|
|
# ============================================
|
|
# Deploy to Cloud Run
|
|
# ============================================
|
|
|
|
- name: Deploy to Cloud Run
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
run: |
|
|
VERSION="${{ steps.version_check.outputs.version }}"
|
|
IMAGE="${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:${{ github.sha }}"
|
|
|
|
echo "🚀 Deploying to Cloud Run..."
|
|
echo "Image: $IMAGE"
|
|
echo "Version: $VERSION"
|
|
|
|
gcloud run deploy karaoke-backend \
|
|
--image $IMAGE \
|
|
--region us-central1 \
|
|
--platform managed \
|
|
--allow-unauthenticated \
|
|
--memory 16Gi \
|
|
--cpu 8 \
|
|
--cpu-boost \
|
|
--execution-environment gen2 \
|
|
--timeout 1800 \
|
|
--concurrency 1 \
|
|
--max-instances 50 \
|
|
--min-instances 0 \
|
|
--set-env-vars "GOOGLE_CLOUD_PROJECT=${{ env.PROJECT_ID }},GCS_BUCKET_NAME=karaoke-gen-storage-${{ env.PROJECT_ID }},FIRESTORE_COLLECTION=jobs,ENVIRONMENT=production,AUDIO_SEPARATOR_API_URL=https://nomadkaraoke--audio-separator-api.modal.run,KARAOKE_GEN_VERSION=$VERSION,DEFAULT_DROPBOX_PATH=/MediaUnsynced/Karaoke/Tracks-Organized,DEFAULT_GDRIVE_FOLDER_ID=1laRKAyxo0v817SstfM5XkpbWiNKNAMSX,ENABLE_CLOUD_TASKS=true,CLOUD_RUN_SERVICE_URL=https://api.nomadkaraoke.com,GCP_REGION=us-central1,DEFAULT_ENABLE_YOUTUBE_UPLOAD=true,DEFAULT_BRAND_PREFIX=NOMAD" \
|
|
--set-secrets "AUDIOSHAKE_API_TOKEN=audioshake-api-key:latest,GENIUS_API_TOKEN=genius-api-key:latest,SPOTIFY_COOKIE_SP_DC=spotify-cookie:latest,RAPIDAPI_KEY=rapidapi-key:latest,DEFAULT_DISCORD_WEBHOOK_URL=discord-releases-webhook:latest,ADMIN_TOKENS=admin-tokens:latest,RED_API_KEY=red-api-key:latest,RED_API_URL=red-api-url:latest,OPS_API_KEY=ops-api-key:latest,OPS_API_URL=ops-api-url:latest,FLACFETCH_API_URL=flacfetch-api-url:latest,FLACFETCH_API_KEY=flacfetch-api-key:latest"
|
|
|
|
- name: Verify deployment and version
|
|
if: steps.version_check.outputs.needs_deploy == 'true'
|
|
run: |
|
|
EXPECTED_VERSION="${{ steps.version_check.outputs.version }}"
|
|
SERVICE_URL=$(gcloud run services describe karaoke-backend \
|
|
--region us-central1 \
|
|
--format 'value(status.url)')
|
|
|
|
echo "Service URL: $SERVICE_URL"
|
|
echo "Expected version: $EXPECTED_VERSION"
|
|
|
|
# Wait for new revision to be ready
|
|
echo "Waiting for new revision to be ready..."
|
|
sleep 15
|
|
|
|
# Basic health check
|
|
curl -f "$SERVICE_URL/api/health" || exit 1
|
|
echo "✅ Service is healthy"
|
|
|
|
# Version verification with retries (Cloud Run may take time to route to new revision)
|
|
MAX_RETRIES=6
|
|
RETRY_DELAY=10
|
|
for i in $(seq 1 $MAX_RETRIES); do
|
|
echo "Checking deployed version (attempt $i/$MAX_RETRIES)..."
|
|
DEPLOYED_VERSION=$(curl -sf "$SERVICE_URL/api/health/detailed" | jq -r '.version // empty')
|
|
|
|
if [ "$DEPLOYED_VERSION" = "$EXPECTED_VERSION" ]; then
|
|
echo "✅ Version verified: $DEPLOYED_VERSION"
|
|
break
|
|
else
|
|
echo "⚠️ Version mismatch: deployed=$DEPLOYED_VERSION, expected=$EXPECTED_VERSION"
|
|
if [ $i -lt $MAX_RETRIES ]; then
|
|
echo " Retrying in ${RETRY_DELAY}s..."
|
|
sleep $RETRY_DELAY
|
|
else
|
|
echo "❌ Version verification failed after $MAX_RETRIES attempts"
|
|
exit 1
|
|
fi
|
|
fi
|
|
done
|
|
|
|
- name: Summary
|
|
if: steps.version_check.outputs.needs_deploy == 'true' && success()
|
|
run: |
|
|
echo "🎉 Deployment successful!"
|
|
echo "Version: ${{ steps.version_check.outputs.version }}"
|
|
echo "Commit: ${{ github.sha }}"
|
|
echo "Branch: ${{ github.ref_name }}"
|
|
echo "Run: ${{ github.run_number }}"
|
|
echo ""
|
|
echo "📊 Cost savings: This build used FREE GitHub Actions instead of Cloud Build"
|
|
echo " Previously: ~\$0.064/min with N1_HIGHCPU_32"
|
|
echo " Now: \$0 (public repo) with registry-cached builds"
|
|
|
|
- name: Skipped deployment summary
|
|
if: steps.version_check.outputs.needs_deploy == 'false'
|
|
run: |
|
|
echo "⏭️ Backend deployment skipped - version ${{ steps.version_check.outputs.version }} already deployed"
|
|
echo ""
|
|
echo "To deploy a new version:"
|
|
echo " 1. Update version in pyproject.toml"
|
|
echo " 2. Commit and push to main"
|