Skip to content

Commit bf38cac

Browse files
don-petryclaude
andcommitted
Epic 0: Add Firebase auth, Cloud Run deploy, and CI/CD automation
- Add Firebase Terraform module with Identity Platform config for Google Sign-In and authorized domains (localhost for dev) - Add Cloud Run Terraform module with scale-to-zero, IAM, and CI image lifecycle management - Add deploy-api CI job: WIF auth → Artifact Registry → Cloud Run (main only) - Add mobile-build CI job: .env from secrets → Expo web export → artifact - Add bootstrap-secrets.sh to populate GitHub secrets from Terraform outputs - Wire firebase and cloud-run modules into dev environment with outputs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 98378a9 commit bf38cac

13 files changed

Lines changed: 662 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,94 @@ jobs:
154154
- name: Docker build
155155
working-directory: .
156156
run: docker build -f apps/api/Dockerfile -t broodly-api:ci apps/api/
157+
158+
# ---------------------------------------------------------------------------
159+
# Deploy API to Cloud Run (main branch only)
160+
# ---------------------------------------------------------------------------
161+
deploy-api:
162+
name: Deploy API
163+
runs-on: ubuntu-latest
164+
timeout-minutes: 15
165+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
166+
needs: [go]
167+
permissions:
168+
contents: read
169+
id-token: write # Required for Workload Identity Federation
170+
steps:
171+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
172+
173+
- name: Authenticate to Google Cloud
174+
uses: google-github-actions/auth@62cf5bd3e4211a0a0b51f2c6d6a37129d828611d # v2
175+
with:
176+
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
177+
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
178+
179+
- name: Set up Cloud SDK
180+
uses: google-github-actions/setup-gcloud@6189d56e4096ee891640bb02ac264be376592d6a # v2
181+
182+
- name: Configure Docker for Artifact Registry
183+
run: gcloud auth configure-docker us-central1-docker.pkg.dev --quiet
184+
185+
- name: Build and push container image
186+
run: |
187+
IMAGE="us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/broodly/api"
188+
docker build -f apps/api/Dockerfile \
189+
-t "${IMAGE}:${{ github.sha }}" \
190+
-t "${IMAGE}:latest" \
191+
apps/api/
192+
docker push "${IMAGE}:${{ github.sha }}"
193+
docker push "${IMAGE}:latest"
194+
195+
- name: Deploy to Cloud Run
196+
run: |
197+
gcloud run deploy broodly-api-dev \
198+
--image "us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/broodly/api:${{ github.sha }}" \
199+
--region us-central1 \
200+
--project "${{ secrets.GCP_PROJECT_ID }}" \
201+
--quiet
202+
203+
# ---------------------------------------------------------------------------
204+
# Mobile Web Build (main branch only)
205+
# ---------------------------------------------------------------------------
206+
mobile-build:
207+
name: Mobile Build
208+
runs-on: ubuntu-latest
209+
timeout-minutes: 20
210+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
211+
needs: [typescript]
212+
steps:
213+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
214+
215+
- uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4
216+
217+
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
218+
with:
219+
node-version: 20
220+
cache: pnpm
221+
222+
- run: pnpm install --frozen-lockfile
223+
224+
- name: Generate .env from secrets
225+
working-directory: apps/mobile
226+
run: |
227+
cat > .env << 'ENVEOF'
228+
EXPO_PUBLIC_FIREBASE_USE_EMULATOR=false
229+
ENVEOF
230+
echo "EXPO_PUBLIC_FIREBASE_API_KEY=${{ secrets.EXPO_PUBLIC_FIREBASE_API_KEY }}" >> .env
231+
echo "EXPO_PUBLIC_FIREBASE_AUTH_DOMAIN=${{ secrets.EXPO_PUBLIC_FIREBASE_AUTH_DOMAIN }}" >> .env
232+
echo "EXPO_PUBLIC_FIREBASE_PROJECT_ID=${{ secrets.EXPO_PUBLIC_FIREBASE_PROJECT_ID }}" >> .env
233+
echo "EXPO_PUBLIC_FIREBASE_STORAGE_BUCKET=${{ secrets.EXPO_PUBLIC_FIREBASE_STORAGE_BUCKET }}" >> .env
234+
echo "EXPO_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=${{ secrets.EXPO_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}" >> .env
235+
echo "EXPO_PUBLIC_FIREBASE_APP_ID=${{ secrets.EXPO_PUBLIC_FIREBASE_APP_ID }}" >> .env
236+
echo "EXPO_PUBLIC_API_URL=${{ secrets.EXPO_PUBLIC_API_URL }}" >> .env
237+
238+
- name: Expo web export
239+
working-directory: apps/mobile
240+
run: npx expo export --platform web
241+
242+
- name: Upload web build artifact
243+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
244+
with:
245+
name: web-build
246+
path: apps/mobile/dist/
247+
retention-days: 14

‎infra/terraform/environments/dev/main.tf‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,35 @@ module "pubsub" {
171171
project_id = var.project_id
172172
environment = var.environment
173173
}
174+
175+
module "firebase" {
176+
source = "../../modules/firebase"
177+
178+
project_id = var.project_id
179+
environment = var.environment
180+
display_name = "Broodly Web"
181+
authorized_domains = ["localhost"]
182+
183+
# Google Sign-In OAuth client ID/secret — leave empty on first apply.
184+
# After Firebase auto-creates the OAuth client, retrieve it from
185+
# GCP Console → APIs & Services → Credentials and re-apply.
186+
google_sign_in_client_id = var.google_sign_in_client_id
187+
google_sign_in_client_secret = var.google_sign_in_client_secret
188+
}
189+
190+
module "cloud_run" {
191+
source = "../../modules/cloud-run"
192+
193+
project_id = var.project_id
194+
region = var.region
195+
environment = var.environment
196+
service_name = "broodly-api-${var.environment}"
197+
image = "us-central1-docker.pkg.dev/${var.project_id}/broodly/api:latest"
198+
service_account_email = google_service_account.api.email
199+
200+
env_vars = {
201+
DB_CONNECTION_SECRET = google_secret_manager_secret.db_connection_string.secret_id
202+
FIREBASE_PROJECT_ID = var.project_id
203+
ENVIRONMENT = var.environment
204+
}
205+
}

‎infra/terraform/environments/dev/outputs.tf‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,46 @@ output "worker_service_account_email" {
4646
description = "Worker service account email"
4747
value = google_service_account.worker.email
4848
}
49+
50+
# Firebase outputs
51+
output "firebase_web_app_id" {
52+
description = "Firebase web app ID"
53+
value = module.firebase.web_app_id
54+
}
55+
56+
output "firebase_api_key" {
57+
description = "Firebase API key for web app"
58+
value = module.firebase.api_key
59+
sensitive = true
60+
}
61+
62+
output "firebase_auth_domain" {
63+
description = "Firebase auth domain"
64+
value = module.firebase.auth_domain
65+
}
66+
67+
output "firebase_project_id" {
68+
description = "Firebase project ID"
69+
value = module.firebase.project_id
70+
}
71+
72+
output "firebase_storage_bucket" {
73+
description = "Firebase storage bucket"
74+
value = module.firebase.storage_bucket
75+
}
76+
77+
output "firebase_messaging_sender_id" {
78+
description = "Firebase messaging sender ID"
79+
value = module.firebase.messaging_sender_id
80+
}
81+
82+
output "firebase_app_id" {
83+
description = "Firebase app ID"
84+
value = module.firebase.app_id
85+
}
86+
87+
# Cloud Run outputs
88+
output "cloud_run_url" {
89+
description = "Cloud Run service URL"
90+
value = module.cloud_run.service_url
91+
}

‎infra/terraform/environments/dev/variables.tf‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,16 @@ variable "environment" {
1414
type = string
1515
default = "dev"
1616
}
17+
18+
variable "google_sign_in_client_id" {
19+
description = "OAuth 2.0 client ID for Google Sign-In (from GCP Console). Leave empty on first apply."
20+
type = string
21+
default = ""
22+
}
23+
24+
variable "google_sign_in_client_secret" {
25+
description = "OAuth 2.0 client secret for Google Sign-In"
26+
type = string
27+
default = ""
28+
sensitive = true
29+
}

‎infra/terraform/modules/cloud-run/.terraform.lock.hcl‎

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -----------------------------------------------------------------------------
2+
# Cloud Run Service
3+
# -----------------------------------------------------------------------------
4+
# Deploys the API container to Cloud Run. CI pushes new revisions via
5+
# `gcloud run deploy`; Terraform manages the service definition, scaling,
6+
# IAM, and environment configuration.
7+
# -----------------------------------------------------------------------------
8+
9+
resource "google_project_service" "run" {
10+
project = var.project_id
11+
service = "run.googleapis.com"
12+
13+
disable_on_destroy = false
14+
}
15+
16+
resource "google_cloud_run_v2_service" "api" {
17+
provider = google-beta
18+
project = var.project_id
19+
name = var.service_name
20+
location = var.region
21+
22+
template {
23+
service_account = var.service_account_email
24+
25+
scaling {
26+
min_instance_count = var.min_instances
27+
max_instance_count = var.max_instances
28+
}
29+
30+
containers {
31+
image = var.image
32+
33+
ports {
34+
container_port = var.port
35+
}
36+
37+
resources {
38+
limits = {
39+
cpu = var.cpu
40+
memory = var.memory
41+
}
42+
}
43+
44+
dynamic "env" {
45+
for_each = var.env_vars
46+
content {
47+
name = env.key
48+
value = env.value
49+
}
50+
}
51+
}
52+
}
53+
54+
depends_on = [google_project_service.run]
55+
56+
lifecycle {
57+
# CI deploys new images via gcloud; don't revert to the Terraform-specified tag
58+
ignore_changes = [template[0].containers[0].image]
59+
}
60+
}
61+
62+
# Allow unauthenticated access (public API endpoint)
63+
# Security note: The API enforces authentication at the application layer via
64+
# Firebase Auth bearer tokens. Cloud Run IAM is left open so the mobile app
65+
# can reach the GraphQL endpoint without GCP credentials.
66+
resource "google_cloud_run_v2_service_iam_member" "public" {
67+
count = var.allow_unauthenticated ? 1 : 0
68+
69+
project = var.project_id
70+
location = var.region
71+
name = google_cloud_run_v2_service.api.name
72+
role = "roles/run.invoker"
73+
member = "allUsers"
74+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "service_url" {
2+
description = "Cloud Run service URL"
3+
value = google_cloud_run_v2_service.api.uri
4+
}
5+
6+
output "service_name" {
7+
description = "Cloud Run service name"
8+
value = google_cloud_run_v2_service.api.name
9+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
variable "project_id" {
2+
description = "GCP project ID"
3+
type = string
4+
}
5+
6+
variable "region" {
7+
description = "GCP region"
8+
type = string
9+
}
10+
11+
variable "environment" {
12+
description = "Environment name (dev, staging, prod)"
13+
type = string
14+
}
15+
16+
variable "service_name" {
17+
description = "Cloud Run service name"
18+
type = string
19+
}
20+
21+
variable "image" {
22+
description = "Container image to deploy (e.g., us-central1-docker.pkg.dev/project/repo/api:tag)"
23+
type = string
24+
}
25+
26+
variable "service_account_email" {
27+
description = "Service account email for the Cloud Run service"
28+
type = string
29+
}
30+
31+
variable "env_vars" {
32+
description = "Environment variables to set on the Cloud Run service"
33+
type = map(string)
34+
default = {}
35+
}
36+
37+
variable "min_instances" {
38+
description = "Minimum number of instances (0 allows scale-to-zero)"
39+
type = number
40+
default = 0
41+
}
42+
43+
variable "max_instances" {
44+
description = "Maximum number of instances"
45+
type = number
46+
default = 2
47+
}
48+
49+
variable "cpu" {
50+
description = "CPU allocation (e.g., 1, 2)"
51+
type = string
52+
default = "1"
53+
}
54+
55+
variable "memory" {
56+
description = "Memory allocation (e.g., 256Mi, 512Mi)"
57+
type = string
58+
default = "256Mi"
59+
}
60+
61+
variable "port" {
62+
description = "Container port"
63+
type = number
64+
default = 8080
65+
}
66+
67+
variable "allow_unauthenticated" {
68+
description = "Allow unauthenticated access (public API)"
69+
type = bool
70+
default = true
71+
}

0 commit comments

Comments
 (0)