Refactor book management to use numeric IDs instead of ISBNs #27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD — Build, Deploy Backend & Frontend (Cloud Run) | |
| permissions: | |
| contents: read | |
| id-token: write | |
| on: | |
| push: | |
| branches: [ master ] | |
| workflow_dispatch: | |
| env: | |
| GCP_PROJECT: ${{ vars.GCP_PROJECT }} | |
| GCP_REGION: ${{ vars.GCP_REGION }} | |
| GAR_HOST: ${{ vars.GAR_HOST }} | |
| GAR_REPO: ${{ vars.GAR_REPO }} | |
| BACKEND_SERVICE: ${{ vars.BACKEND_SERVICE }} | |
| FRONTEND_SERVICE: ${{ vars.FRONTEND_SERVICE }} | |
| RUNTIME_SA: ${{ vars.RUNTIME_SA }} | |
| INSTANCE_CONNECTION_NAME: ${{ vars.INSTANCE_CONNECTION_NAME }} | |
| DB_NAME: ${{ vars.DB_NAME }} | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Java 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '21' | |
| - name: Set up Node.js 20 | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| cache-dependency-path: frontend/package-lock.json | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Google Cloud Auth | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| credentials_json: ${{ secrets.GCP_SA_KEY }} | |
| project_id: ${{ env.GCP_PROJECT }} | |
| - name: Set up Cloud SDK | |
| uses: google-github-actions/setup-gcloud@v2 | |
| - name: Configure Docker for Artifact Registry | |
| run: gcloud auth configure-docker $GAR_HOST | |
| - name: Build & Push Backend Image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./backend | |
| file: ./backend/DockerFile | |
| push: true | |
| tags: | | |
| ${{ env.GAR_HOST }}/${{ env.GCP_PROJECT }}/${{ env.GAR_REPO }}/backend:${{ github.sha }} | |
| - name: Deploy Backend to Cloud Run | |
| run: | | |
| IMAGE="${GAR_HOST}/${GCP_PROJECT}/${GAR_REPO}/backend:${GITHUB_SHA}" | |
| SPRING_DS_URL="jdbc:mysql:///${DB_NAME}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false" | |
| gcloud run deploy "$BACKEND_SERVICE" \ | |
| --image "$IMAGE" \ | |
| --region "$GCP_REGION" --platform managed --allow-unauthenticated \ | |
| --service-account "$RUNTIME_SA" \ | |
| --add-cloudsql-instances "$INSTANCE_CONNECTION_NAME" \ | |
| --set-env-vars "SPRING_PROFILES_ACTIVE=prod,SPRING_DATASOURCE_URL=${SPRING_DS_URL},JWT_EXPIRATION=${{ vars.JWT_EXPIRATION || '3600000' }},CHAT_MODEL=${{ vars.CHAT_MODEL || 'nvidia/nemotron-nano-9b-v2:free'}}" \ | |
| --update-secrets "SPRING_DATASOURCE_USERNAME=db_user:latest,SPRING_DATASOURCE_PASSWORD=db_password:latest" \ | |
| --update-secrets "JWT_SECRET=JWT_SECRET:latest,CHAT_API_KEY=CHAT_API_KEY:latest" | |
| # Capture backend URL for the frontend build | |
| BACKEND_URL=$(gcloud run services describe "$BACKEND_SERVICE" --region "$GCP_REGION" --format 'value(status.url)') | |
| echo "BACKEND_URL=${BACKEND_URL}" >> "$GITHUB_ENV" | |
| - name: Build & Push Frontend Image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./frontend | |
| file: ./frontend/DockerFile | |
| push: true | |
| tags: | | |
| ${{ env.GAR_HOST }}/${{ env.GCP_PROJECT }}/${{ env.GAR_REPO }}/frontend:${{ github.sha }} | |
| build-args: | | |
| NEXT_PUBLIC_API_URL=${{ env.BACKEND_URL }} | |
| - name: Deploy Frontend to Cloud Run | |
| run: | | |
| IMAGE="${GAR_HOST}/${GCP_PROJECT}/${GAR_REPO}/frontend:${GITHUB_SHA}" | |
| # Deploy; also set the env for runtime | |
| gcloud run deploy "$FRONTEND_SERVICE" \ | |
| --image "$IMAGE" \ | |
| --region "$GCP_REGION" --platform managed --allow-unauthenticated \ | |
| --service-account "$RUNTIME_SA" \ | |
| --set-env-vars "NEXT_PUBLIC_API_URL=${BACKEND_URL}" | |
| # Capture frontend URL for CORS on backend | |
| FRONTEND_URL=$(gcloud run services describe "$FRONTEND_SERVICE" --region "$GCP_REGION" --format 'value(status.url)') | |
| echo "FRONTEND_URL=${FRONTEND_URL}" >> "$GITHUB_ENV" | |
| # Patch backend with FRONTEND_URL (for CORS) | |
| - name: Update Backend FRONTEND_URL | |
| run: | | |
| gcloud run services update "$BACKEND_SERVICE" \ | |
| --region "$GCP_REGION" \ | |
| --update-env-vars "FRONTEND_URL=${FRONTEND_URL}" |