Create multiple database instances via Firebase CLI - firebase

I am looking into automating the deployment of a project that is made up of multiple functions that are triggers on multiple realtime database instances.
Using firebase deploy allows you to deploy both functions and database rules.
I have also created targets so i can deploy different rules for different database instances.
The only missing part of my automation is actually creating those different database instances via the Firebase CLI tool; Since deploying a trigger function to a specific database that doesn't exist will fail. Ideally i would like to make sure to create those database instances if they don't exist and then continue my deployment.
Is there a way to create different database instances via the CLI ?

I don't know when it was added, but this might be useful for others having the same question.
You can create a Firebase Realtime Database instance using the following CLI command:
firebase database:instances:create [instance-name]
I don't know why this isn't described in the CLI reference, but it is mentioned here: https://firebaseopensource.com/projects/firebase/firebase-tools/

You can Create realtime database instance by firebase CLI using the below command
firebase database:instances:create -P <alias_or_project_id> <DBinstanceName>
This Command work fine if you have paid plan.
For Free Plan it throw error:- FirebaseError: HTTP Error: 400, Precondition check failed.
Note : <DBinstanceName> should be alpha-numeric with hyphens.

Related

Can I create and configure a GCP project + Firebase completely from the CLI or a script?

I am using gcloud + firebase cli to explore a reproducible way to create and configure a GCP + Firebase project.
I have created the GCP project using gcloud cli tool. I have then used firebase cli to run the command firebase init firestore.
Ulimately it ended up outputting...
Error: It looks like you haven't used Cloud Firestore in this project before. Go to https://console.firebase.google.com/project/my-project/firestore to create your Cloud Firestore database.
Is there a way I can "create my firestore database" using a cli tool or scripting api, instead of having to navigate to a web GUI tool and manually execute steps?
Unless Firebase do more behind the scenes, creating a Firestore instance could be as simple as this command from the GCloud CLI reference:
gcloud firestore databases create --region=us-central --project=my-project
The documentation for the entire create-provision cycle (from GCP's perspective) is here - including Terraform details (some commands may have been released since the docs were written).

How to create/initialise default Realtime Database instance in a Firebase project using CLI?

I am trying to set up a Firebase project that needs a default Realtime Database instance using non-interactive CLI.
I've tried deploying the rules:
❯ firebase deploy --only database
Error: It looks like you haven't created a Realtime Database instance in this project before.
Please run firebase init database to create your default Realtime Database instance.
but firebase init is an interactive command, so I can't use it in my CI pipeline.
An attempt to use firebase database:instances:create command also fails with the same message:
❯ firebase database:instances:create default
Error: It looks like you haven't created a Realtime Database instance in this project before.
Please run firebase init database to create your default Realtime Database instance.
I've tried googling for anything around creating default realtime database instances, but can't find anything besides going the interactive CLI way or doing it through the console - both of which are ill-suited for CI.
Is there a way to achieve what I'm after without resorting to interactive tools?
From the second line of that second error message:
Please run firebase init database to create your default Realtime Database instance.
It seems that you'll need to run firebase init database to create the initial/default database instance for the project. After that you can run firebase database:instances:create to create additional instances.

Firebase cloud functions init not working

I want to use Firebase cloud functions but when I try to init exists project I get error.
This is the error "Error: HTTP Error: 403, The caller does not have permission"
I created project before and then I moved project folder. When I had tried deploy, it wasn't work. I don't remember which error I got. And then I tried init same project but I got this error.
I tried logout and login again but it wasn't work.
I deleted "firebase-tools" but it wasn't work too.
And after I tried on different computer and I got same error.
And also I have Firebase Admin permission.
The firebase init command initializes/enables the project to use Cloud Functions for Firebase. Owners or Editors should use this command. Owners/Editors have higher permissions which allows APIs to be enabled (e.g. allowing the project to create VMs, Cloud Functions, or create a NoSQL Database).
Therefore I think you should perform this operation with Owner/Editor role.
You need to have Owner or Editor IAM permission on the GCP project to use firebase init command.
The firebase init command initializes/enables the project to use Cloud Functions/Hosting for Firebase. Owners/Editors have higher permissions that allow APIs to be enabled etc.
it is a first-time task when you starting the development, and then init command can be avoided if you have set up your cloud functions code in VCS for multiple developers or you.
i.e, Usually after writing code, you will replicate the project to other developers, who will then also write code and use firebase use yourprojectid further.

"Error: Permission denied" when running firebase init and choosing database

So I’m testing out the CLI today and firebase init with db/hosting selected, and choosing to create a new project, always says Error: Permission denied..
sudo firebase init didn’t help.
firebase-tools version 3.0.3 on OS X 10.11.5.
firebase init
You're about to initialize a Firebase project in this directory:
/Users/splaktar/Git/tmp
Before we get started, keep in mind:
* You are initializing in an existing Firebase project directory
? What Firebase CLI features do you want to setup for this folder? Database: Deploy Firebase Realtime Database Rules
=== Project Setup
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.
? What Firebase project do you want to associate as default? [don't setup a default project]
=== Database Setup
Firebase Realtime Database Rules allow you to define how your data should be
structured and when your data can be read from and written to.
? What file should be used for Database Rules? database.rules.json
Error: Permission denied.
I've tried the answers here but they did not help. Is this just a bug in the firebase-tools?
I have the same issue and this is how I fixed it.
run firebase init
Deselect ❯◯ Database: Deploy Firebase Realtime Database Rules
Go through other steps to setup the initial app, selecting the default for each should work just fine.
run firebase serve
Hope that helps.
Sometimes it could happen when .firebaserc file exists.
Remove .firebaserc file and reattempt firebase init.
I was with the same problem, but I was not accessing the right account.
After accessed the right account, I created a new project and run "firebase list" (before this not happened) and my new project was there.
This solved my problem.
You need to have Owner or Editor IAM permission on the GCP project to use firebase init command.
The firebase init command initializes/enables the project to use Cloud Functions/Hosting etc for Firebase. Owners/Editors have higher permissions that allow APIs to be enabled etc.
it is a first-time task when you starting the development, and then init command can be avoided if you have set up your cloud functions code in VCS for multiple developers or you.
i.e, Usually after writing code, you will replicate the project to other developers, who will then also write code and use firebase use yourprojectid further.

How to delete site via CLI

Is there a way to delete an app via Firebase CLI?
Our CI creates a temporary Firebase app for a feature branch (based on the CI build number), and then runs tests on it. At the end it needs to delete the app. I'm not entirely sure how this is done. firebase disable:hosting just disables hosting and does not delete the app. This is similar to how we would have used Heroku review apps or Heroku forked apps. A similar CI workflow can also be achieved on Google App engine via versions.
Any pointers would be much appreciated.
The correct command is firebase hosting:disable, which make your site offline. You can not remove your project, instead you can overwrite it by creating a new one with firebase init.
More info here https://firebase.google.com/docs/cli/.
There is currently no public API to delete a Firebase backend.
The recommended practice is to use the same database for testing and (if needed) put each run in its own node under the root. So instead of creating/deleting a new database, you're just create/deleting a node in a single database.
Using the command firebase use <alias> --unaliasand then delete the project from the console https://console.firebase.google.com/
Check before the list of commands with the command -h

Resources