Firebase CLI — Command-Line Tools for Deploying and Managing Firebase Projects
In this tutorial, you will learn about Firebase CLI. We cover key concepts, practical examples, and best practices to help you master this topic.
The Firebase CLI provides command-line tools for deploying hosting, managing Cloud Functions, updating Firestore security rules and indexes, and automating Firebase project workflows.
What You'll Learn
- Installing and configuring the Firebase CLI
- Deploying hosting, functions, and rules
- Automating deployments in CI/CD
Why It Matters
The CLI enables repeatable, automated Firebase management. Manual console configuration does not scale. DodaTech uses Firebase CLI in its CI/CD pipeline for automated deployments.
flowchart LR
A["Firebase CLI"] --> B["firebase init"]
A --> C["firebase deploy"]
A --> D["firebase serve"]
A --> E["firebase functions:shell"]
B --> F["Configure project"]
C --> G["Deploy hosting, functions, rules"]
D --> H["Local emulation"]
E --> I["Test functions locally"]
Code Examples
# Install Firebase CLI
npm install -g firebase-tools
# Login and initialize
firebase login
firebase init
# Deploy specific services
firebase deploy --only hosting
firebase deploy --only functions
firebase deploy --only firestore:rules
firebase deploy --only firestore:indexes
# Deploy everything
firebase deploy
# Local emulation
firebase emulators:start
# Run functions shell for testing
firebase functions:shell
# CI/CD deployment script
# .github/workflows/deploy.yml
name: Deploy to Firebase
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm ci
- run: npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}'
channelId: live
// firebase.json configuration
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"source": "functions",
"runtime": "nodejs18"
},
"hosting": {
"public": "build",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{"source": "**", "destination": "/index.html"}
]
},
"storage": {
"rules": "storage.rules"
}
}
# Manage multiple projects
firebase use --add
firebase use default # or staging, production
firebase deploy --project staging
Common Mistakes
1. Forgetting to Run firebase init in a New Project
The init command creates the firebase.json configuration file.
2. Deploying Without Building First
Always run the build command before firebase deploy --only hosting.
3. Using Default firebase.json Without Customization
Configure rewrites, headers, and redirects in firebase.json for production.
4. Not Using .firebaserc for Multiple Environments
The .firebaserc file manages project aliases for dev, staging, and production.
5. Ignoring firebase deploy --only for Targeted Deployments
Full deployments take longer. Deploy only changed services when possible.
Practice Questions
- What command initializes Firebase in a project directory?
- How do you deploy only Cloud Functions?
- What does firebase emulators:start do?
- How do you set up multiple deployment environments?
- What file configures Firebase services?
Answers:
- firebase init.
- firebase deploy --only functions.
- It starts local emulators for Firestore, Functions, Auth, and other services.
- Use .firebaserc to define project aliases.
- firebase.json.
Challenge: Set up a Firebase project with CI/CD deployment using GitHub Actions. Configure the pipeline to deploy hosting, functions, and Firestore rules on every push to the main branch. Add a preview deployment for pull requests.
FAQ
Mini Project
Create a deployment workflow: initialize a Firebase project with Hosting, Functions, and Firestore. Configure .firebaserc for dev, staging, and production environments. Write deployment scripts for each environment. Set up GitHub Actions for automated deployments with preview channels for PRs.
What's Next
Learn about the Firestore data model to design your database schema, then explore Firestore queries for data retrieval.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro