Cloud Run and Firebase production deployment¶
This guide describes the current LeaveMaster production deployment: Spring Boot on Google Cloud Run, React/Vite on Firebase Hosting, Supabase PostgreSQL, Terraform-managed GCP/Firebase infrastructure, Secret Manager runtime credentials and GitHub Actions authentication through Workload Identity Federation (WIF).
For architecture and local development, start with README.md, docs/architecture.md and docs/development-and-ci.md. For first-time staff activation and transactional email, see docs/account-activation-and-email.md.
Production topology¶
flowchart LR
Browser[Browser]
Firebase[Firebase Hosting]
CloudRun[Cloud Run leavemaster-api]
Supabase[(Supabase PostgreSQL)]
GCS[(GCS attachments)]
Secrets[Secret Manager]
Resend[Resend transactional email]
Browser --> Firebase
Firebase -->|backend rewrites| CloudRun
CloudRun --> Supabase
CloudRun --> GCS
Secrets --> CloudRun
CloudRun -. account activation email .-> Resend
Browser traffic stays on the Firebase/custom frontend origin. Do not configure the production Vite bundle to call the run.app hostname directly.
Prerequisites¶
- Google Cloud project with billing enabled.
- Supabase PostgreSQL project.
- GitHub repository/environment access.
- Google Cloud CLI for one-time bootstrap/secret versions.
- Terraform >= 1.8 if performing bootstrap/import work locally.
- Firebase terms accepted for the Google account/project when first enabling Firebase.
Current production defaults use region asia-southeast1.
Supabase¶
Create a Supabase PostgreSQL project in a nearby region and use a direct/session-mode compatible connection on port 5432. Do not use transaction-pooler port 6543 for Flyway migrations.
Record the non-secret connection values for GitHub environment variables:
Database password is a secret and belongs in Google Secret Manager.
Terraform state bucket¶
Create the remote state bucket once, for example:
PROJECT_ID=YOUR_PROJECT_ID
REGION=asia-southeast1
gcloud storage buckets create "gs://${PROJECT_ID}-tfstate" \
--location="${REGION}" \
--uniform-bucket-level-access
The production workflow initializes this bucket with prefix:
Always use the same backend when importing or troubleshooting production Terraform state.
GitHub Actions service account and WIF¶
Create a deployment service account and configure GitHub OIDC/WIF so Actions can impersonate it without a long-lived JSON key.
The WIF provider should map repository identity and restrict the provider to the intended repository. Grant the repository principal roles/iam.workloadIdentityUser on the deployment service account.
The deployment account needs the storage, Secret Manager, Firebase, Cloud Build, Cloud Run and other permissions required by routine Terraform/workflow resources. Firebase administration belongs on the deployment identity, not the Cloud Run runtime identity.
Terraform also defines roles/monitoring.editor and roles/logging.configWriter for the deployment service account so the workflow can manage alert policies, notification channels, and logs-based metrics. These project-level IAM bindings are bootstrap-only: routine Cloud Run deployments set manage_deployment_project_iam=false, so they never try to grant privileges to their own identity.
Create a separate bootstrap service account that is allowed to update project IAM policy and grant the repository WIF principal roles/iam.workloadIdentityUser on that bootstrap account. Configure its email as the GitHub production environment variable BOOTSTRAP_WIF_SERVICE_ACCOUNT. The manual Bootstrap deployment project IAM workflow impersonates that distinct identity, sets manage_deployment_project_iam=true, and reconciles only the deployment IAM bindings.
The GitHub workflow requires:
Do not commit a service-account key as a fallback.
Database password secret¶
Terraform manages the leavemaster-db-password secret resource and Cloud Run access binding, but not the plaintext value.
Add the first secret version before a Cloud Run revision references it:
printf '%s' 'YOUR_SUPABASE_DB_PASSWORD' | \
gcloud secrets versions add leavemaster-db-password \
--data-file=- \
--project=YOUR_PROJECT_ID
If the secret resource does not exist yet, run the prerequisite Terraform deployment first or create/import it according to the production state plan.
PlatformAdmin password secret¶
Production can source PLATFORM_ADMIN_PASSWORD from:
The password value is plaintext inside Secret Manager; Spring BCrypt-hashes it before storing a password hash in PostgreSQL.
Use the controlled recovery/rotation process in docs/platform-admin-password.md. Normally:
The reset flag should be set to true only for the deliberate recovery deployment and returned to false immediately afterward.
Resend activation-email secret¶
First-time staff account activation can send verification PINs through Resend. The Java application is provider-neutral, but the current provider adapter is enabled with:
The Resend API key belongs in Secret Manager. Terraform expects an existing secret ID through resend_api_key_secret_id; the current default is:
Create the secret and first version if needed:
printf '%s' 'YOUR_RESEND_API_KEY' | \
gcloud secrets create leavemaster-resend-api-key \
--data-file=- \
--replication-policy=automatic \
--project=YOUR_PROJECT_ID
Rotate an existing key by adding a version:
printf '%s' 'YOUR_NEW_RESEND_API_KEY' | \
gcloud secrets versions add leavemaster-resend-api-key \
--data-file=- \
--project=YOUR_PROJECT_ID
Terraform grants the Cloud Run runtime service account secret access and injects the value as secret-backed RESEND_API_KEY. Never put the plaintext key in GitHub variables, Terraform variable files, application configuration, logs or Vite/frontend settings.
A custom sending domain is not required for current development/testing. onboarding@resend.dev is supported as the development sender. Treat resend.dev as test/development usage rather than unrestricted production delivery.
No GoDaddy or other registrar mailbox/email-hosting subscription is required merely to send outbound transactional email through Resend. When a production sending identity is needed later, verify a domain/subdomain in Resend and change EMAIL_FROM_ADDRESS; no application code change is required.
See docs/account-activation-and-email.md for activation API behaviour, PIN security, smoke testing and custom-domain rollout.
OpenAI assistant secret¶
The assistant is disabled by default. Production expects an existing Secret Manager secret, default:
Create the secret and first version if needed:
printf '%s' 'YOUR_OPENAI_API_KEY' | \
gcloud secrets create leavemaster-openai-api-key \
--data-file=- \
--replication-policy=automatic \
--project=YOUR_PROJECT_ID
Rotate an existing key by adding a version:
printf '%s' 'YOUR_NEW_OPENAI_API_KEY' | \
gcloud secrets versions add leavemaster-openai-api-key \
--data-file=- \
--project=YOUR_PROJECT_ID
The current Terraform configuration manages the Cloud Run secret-accessor binding and secret-backed OPENAI_API_KEY environment reference when the assistant is enabled. Do not run an ad-hoc gcloud run services update to maintain assistant configuration; Terraform is now authoritative.
Enable through GitHub production environment variables:
ENABLE_OPENAI_ASSISTANT=true
OPENAI_API_KEY_SECRET_ID=leavemaster-openai-api-key
OPENAI_MODEL=gpt-5-mini
The deployment also sets the Spring AI model selector for the enabled assistant. The secret value never belongs in GitHub variables, Terraform state or Vite configuration.
Firebase Hosting¶
The same GCP project is associated with Firebase. Terraform can enable required Firebase services and create the environment-specific Hosting site when:
Default site ID convention:
For project leavemaster and environment production:
The corresponding default app origin is:
Provisioning the site does not deploy the React application. The frontend GitHub Actions workflow builds frontend/dist and publishes it after an eligible main push.
Production Hosting rewrites¶
frontend/firebase.json sends browser-facing backend paths to the Cloud Run service before the SPA fallback, including:
All other frontend routes fall back to index.html for React Router.
The Cloud Run session cookie is named __session because Firebase Hosting forwards this specially named cookie to the dynamic backend. It is Secure, HttpOnly and SameSite=Lax.
GitHub production environment¶
Create/protect a GitHub environment named production.
Core deployment variables include:
GCP_PROJECT_ID
GCP_REGION
SUPABASE_DB_HOST
SUPABASE_DB_USERNAME
TF_STATE_BUCKET
WIF_PROVIDER
WIF_SERVICE_ACCOUNT
BOOTSTRAP_WIF_SERVICE_ACCOUNT
ENABLE_FIREBASE_HOSTING
FRONTEND_ENVIRONMENT
ENABLE_PLATFORM_ADMIN_PASSWORD_SECRET
RESET_PLATFORM_ADMIN_PASSWORD
Current runtime/environment options include:
PUBLIC_APP_URL
ALLOWED_FRONTEND_ORIGINS
EMAIL_PROVIDER
EMAIL_FROM_ADDRESS
EMAIL_FROM_NAME
RESEND_API_KEY_SECRET_ID
ENABLE_OPENAI_ASSISTANT
OPENAI_API_KEY_SECRET_ID
OPENAI_MODEL
RESEND_API_KEY_SECRET_ID identifies the Secret Manager secret; the API key value itself must not be stored as a GitHub variable.
See docs/environments-and-domains.md for exact semantics and custom-domain/OAuth requirements.
Cloud Run deployment workflow¶
The backend production workflow runs on eligible main changes and manual dispatch.
High-level flow:
- Checkout repository.
- Authenticate to Google Cloud through WIF.
- Initialize Terraform against the production GCS backend.
- Run Terraform format/validation checks.
- Perform targeted prerequisite provisioning for APIs, buckets, registry, service accounts, managed secrets/runtime IAM, Firebase resources and optional provider bindings. Project-level IAM for the deployment identity is intentionally excluded.
- Build
backendwith GradlebootJar. - Build/push the production container using Cloud Build.
- Tag the container with the current Git commit SHA.
- Generate the full Terraform plan with Cloud Run enabled.
- Run the protected-resource plan check.
- Apply the plan.
- Print the Cloud Run URL for operator diagnostics.
The routine workflow explicitly sets TF_VAR_manage_deployment_project_iam=false and TF_VAR_manage_production_monitoring=false. Project-level monitoring IAM and the monitoring resources that require those permissions therefore do not appear in routine application plans/applies.
Project IAM bootstrap¶
Use the manual Bootstrap deployment project IAM workflow when the deployment identity needs its Terraform-managed monitoring/logging project roles reconciled. Before running it:
- create or choose a different service account with permission to update the project's IAM policy;
- allow the repository's WIF principal to impersonate that bootstrap account;
- set
BOOTSTRAP_WIF_SERVICE_ACCOUNTin the GitHubproductionenvironment; - keep
WIF_SERVICE_ACCOUNTpointing at the normal least-privileged deployment account.
The bootstrap workflow refuses to run when both service-account variables resolve to the same identity. It uses the normal production Terraform state. First it applies the deployment IAM roles, then—using the same privileged bootstrap identity—it reconciles the production log metric, alert policies, and optional notification channel. Routine Cloud Run deployment is therefore independent of this bootstrap step.
Current image tag rule¶
The workflow constructs the container URI from current deployment variables and ${github.sha}. It does not read the mutable image tag from Terraform state after the targeted prerequisite apply because state-backed outputs can be stale/incomplete in that phase.
If Cloud Run reports:
verify Artifact Registry contains the exact SHA from that workflow run.
Frontend deployment workflow¶
Frontend pull requests run the complete quality gate but do not deploy production.
An eligible main push runs:
The validated frontend/dist artifact is then deployed to Firebase Hosting using WIF/Application Default Credentials. No FIREBASE_TOKEN or service-account JSON key is required.
Terraform production resources¶
The Terraform layout under infra/terraform/ covers:
- required project APIs;
- Artifact Registry;
- Cloud Build source bucket;
- Cloud Run service account/service/public IAM;
- attachment bucket/runtime IAM;
- database secret resource/runtime IAM;
- PlatformAdmin secret resource/runtime IAM;
- existing Resend secret lookup/runtime IAM and secret-backed
RESEND_API_KEYinjection when email is enabled; - optional existing OpenAI/Gemini secret lookup/runtime IAM;
- Firebase project association and Hosting site;
- runtime public URL/CORS/email/assistant settings;
- deployment outputs.
Before applying manually, understand the production remote state and GitHub-provided variables. Do not create a second local production state.
Protected Terraform plans¶
The Cloud Run workflow runs check-protected-plan.sh before applying the full plan. A destructive action against protected backend resources fails the workflow.
Do not bypass this check to fix a state/configuration mismatch. Inspect resource addressing, conditionals/counts and imports first.
Custom domains¶
Firebase Hosting can own the browser custom domain/TLS, for example app.example.com. When changing the canonical app origin:
- configure/verify the Firebase custom domain and DNS;
- set
PUBLIC_APP_URL=https://app.example.com; - set exact
ALLOWED_FRONTEND_ORIGINSas needed; - update OAuth callback registrations to the new origin;
- deploy Cloud Run and Firebase Hosting.
A separate API domain is optional and is not needed for the browser session flow.
The email sending domain is independent from the browser custom domain. A custom email domain/subdomain is optional until production transactional delivery is required; see docs/account-activation-and-email.md. Buying a mailbox from the registrar is not required for Resend outbound delivery.
OAuth callbacks¶
Production callback URLs use the canonical public app origin:
https://<app-origin>/login/oauth2/code/google
https://<app-origin>/login/oauth2/code/microsoft
https://<app-origin>/login/oauth2/code/github
https://<app-origin>/login/oauth2/code/facebook
Firebase rewrites the callback to Cloud Run. Successful authentication returns the browser to the app.
Post-deployment verification¶
Verify:
- Firebase site loads the expected frontend release.
- Login succeeds and
/auth/mereturns200afterward. - Browser session cookie is
__sessionand secure/HttpOnly. - Browser API traffic stays on the Firebase/custom app host.
- Backend health/application logs show successful database/Flyway startup.
- CORS rejects unapproved direct browser origins.
- OAuth callbacks use the canonical app origin if providers are enabled.
- If Resend email is enabled, Cloud Run contains a secret-backed
RESEND_API_KEYreference and a controlled activation smoke test can request email without exposing the PIN/key in logs. - If assistant is enabled, Cloud Run contains the selected provider's secret-backed API key and
/api/assistant/chatworks for an authorized user. - Terraform outputs show the expected app URL/origin configuration.
Troubleshooting¶
See docs/troubleshooting.md for Firebase Site Not Found, session 401, Cloud Run image mismatch, Flyway, Terraform state/import, WIF and assistant-provider failures. See docs/account-activation-and-email.md for Resend configuration, sender/domain rejection, activation PIN delivery, expiry and rate-limit troubleshooting.