CI/CD Setup Guide
This guide will help you set up automatic deployment to Firebase whenever you push to your GitHub repository.
Prerequisites
- Firebase project already set up
- GitHub repository
- Firebase CLI installed locally (
npm install -g firebase-tools)
Step 1: Generate Firebase Service Account
- Go to Firebase Console
- Select your project (your-project-id)
- Click the gear icon ⚙️ and select “Project settings”
- Go to the “Service accounts” tab
- Click “Generate new private key”
- Save the downloaded JSON file securely
Step 2: Generate Firebase CI Token
Run this command in your terminal:
firebase login:ciThis will open a browser window for authentication. After logging in, you’ll receive a token that looks like:
1//0e-xxx...xxxxxSave this token securely.
Step 3: Configure GitHub Secrets
Go to your GitHub repository settings:
- Navigate to Settings → Secrets and variables → Actions
- Click “New repository secret”
- Add the following secrets:
Required Secrets
| Secret Name | Description | Where to Find |
|---|---|---|
FIREBASE_SERVICE_ACCOUNT | Entire JSON content from Step 1 | Service account JSON file |
FIREBASE_TOKEN | Token from Step 2 | Output of firebase login:ci |
FIREBASE_PROJECT_ID | Your Firebase project ID | your-project-id |
VITE_FIREBASE_API_KEY | Firebase API key | Firebase Console → Project settings |
VITE_FIREBASE_AUTH_DOMAIN | Auth domain | your-project-id.firebaseapp.com |
VITE_FIREBASE_PROJECT_ID | Project ID | your-project-id |
VITE_FIREBASE_STORAGE_BUCKET | Storage bucket | your-project-id.firebasestorage.app |
VITE_FIREBASE_MESSAGING_SENDER_ID | Messaging sender ID | Firebase Console → Project settings |
VITE_FIREBASE_APP_ID | App ID | Firebase Console → Project settings |
VITE_FIREBASE_MEASUREMENT_ID | Measurement ID | Firebase Console → Project settings |
VITE_FIREBASE_FUNCTIONS_URL | Functions URL | https://us-central1-your-project-id.cloudfunctions.net |
How to Add Secrets
For simple values:
Name: VITE_FIREBASE_API_KEYValue: YOUR_FIREBASE_API_KEYFor the service account JSON:
Name: FIREBASE_SERVICE_ACCOUNTValue: [Paste the entire JSON content including the curly braces]Step 4: Update Firebase Configuration
Make sure your firebase.json in the root directory is configured correctly:
{ "hosting": { "public": "react/dist", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "**", "destination": "/index.html" } ] }, "functions": { "source": "functions", "runtime": "nodejs20" }, "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "storage": { "rules": "storage.rules" }}Step 5: Test the Pipeline
- Make a small change to your code
- Commit and push to the main branch:
Terminal window git add .git commit -m "Test CI/CD pipeline"git push origin main - Go to the Actions tab in your GitHub repository
- Watch the workflow run
- Check your Firebase Hosting URL after completion
Features of This CI/CD Pipeline
- Automatic Deployment: Deploys to Firebase on every push to main
- PR Previews: Creates preview deployments for pull requests
- Full Stack: Deploys React app, Functions, Firestore rules, and Storage rules
- Build Validation: Ensures the app builds successfully before deployment
- Environment Variables: Securely injects Firebase config during build
Troubleshooting
Build Fails
- Check the Actions tab for error logs
- Ensure all secrets are set correctly
- Verify the React app builds locally with
cd react && npm run build
Deployment Fails
- Check Firebase service account permissions
- Verify the Firebase token is still valid
- Ensure the project ID matches your Firebase project
Missing Environment Variables
- Double-check all VITE_ prefixed secrets are set
- Ensure no typos in secret names
- Secrets are case-sensitive
Additional Notes
- The pipeline runs on Ubuntu latest
- Node.js 20 is used for builds
- Dependencies are cached for faster builds
- PR previews expire after 7 days