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

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).

Related

Firebase and json-server

I'm currently using json-server repo to emulate my back-end db and server for a prototype. To use/start json-server, I need to execute npm run json:server.
I've deployed my project to firebase but can't find a way to access a cmd or run that command from my own git-bash.
Is there any way of doing so or firebase doesn't allow to run commands on their end?
If you want to run small Node.js snippets on Firebase, have a look at the integration between Firebase Hosting and Cloud Functions.
Also see the Firebase documentation on what can you host on Firebase? and the overview documentation for running micro-services on Firebase Hosting and Cloud Functions.

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.

How can I automate the creation of a firebase firestore database for a given project?

For CI/CD purpose, I'd like to generate my firebase infrastructure as code (so using the web console is not an option).
I'm already able to create the firebase project with the Firebase CLI:
firebase projects:create --display-name "MyApp DEV" --token ${FIREBASE_TOKEN} myapp-dev
I've check the firebase reference manual, but I can't find any command line tool, API or Admin SDK in any language to create the firestore database.
I know it's possible to create a real time database with firebase database:instances:create <instanceName>but, again, i'm not interested by this database, i want to create a firestore database.
I've quickly check the gcloud CLI but i haven't found any possibility ere either.
Did I miss anything ? Do you have any solution / workaround.
At the time of writing, you can use alpha version of gcloud for firestore
to create a firestore database. Example gcloud alpha firestore databases create --region=europe-west --project <PROJECT_ID>. But before that you must first create / have created a Google App Engine app in the corresponding region, in out example europe-west.
So a complete working example should be gcloud app create --region=europe-west --project <PROJECT_ID> && gcloud alpha firestore databases create --region=europe-west --project <PROJECT_ID>
Based on MBA_Phoenix response i have create pipeline to deploy firebase stack from GITLAB CI.
First you need to create empty project to manage entities and api deployment on GCLOUD,add this API to this project :
https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com
https://console.developers.google.com/apis/api/appengine.googleapis.com/
Then you need to create service account for the "manager project" with empty rules, copy the email address (something like service-account#manager-project.iam.gserviceaccount.com ).
Go back to organization level and add this email into IAM policy > Add user (paste service account email you have created). Set project.creator role. Go back to project > service account and generate new private key at Json format and download it.
.gitlab-ci.yml :
# This variables need to be setup in gitlab project CI/CD variables
variables:
GCLOUD_SERVICE_KEY: Content of service-account.json
PROJECT_ID : Name of new project
GCLOUD_ORGANIZATION_ID : Main organization to attach new project
FIREBASE_TOKEN : Result of command : firebase login:ci
image: google/cloud-sdk:297.0.1-alpine
deploy-firestore-project:
stage: deploy
before_script:
- apk add --update nodejs npm
- npm install -g firebase-tools
- gcloud components install alpha
- echo $GCLOUD_SERVICE_KEY > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
script:
- gcloud projects create $PROJECT_ID --organization $GCLOUD_ORGANIZATION_ID
- firebase projects:addfirebase $PROJECT_ID --token $FIREBASE_TOKEN
- gcloud app create --region=europe-west --project $PROJECT_ID
- gcloud alpha firestore databases create --region=europe-west --project $PROJECT_ID --quiet
Note :
If you don't run : firebase projects:addfirebase i don't see my new project in firebase dashboard and i'm not enable to use it.
You can use the same process to create a service-account for all firestore project of organization (depends of your security requirements).

Programmatically adding Firestore to a new Firebase project [duplicate]

I'm writing a script for setting up a Google Cloud project and I'd like to use Firestore. A new project, however, is by default in Datastore mode.
Switching to Firestore can easily be done using the web interface in the cloud console but I'd like to automate it. Is there any API or command line tool (gcloud? firebase?) for enabling it?
Unfortunately, as of today (30 Apr 2019), there is no API or command line to set the mode for Cloud Firestore.
Things seem to have changed by now (03 Jan 2022). I managed to set up a new Firestore with gcloud using the following commands.
Native mode is chosen by default:
gcloud app create --region=us-central
gcloud firestore databases create --region=us-central
also see the documentation for
app create and firestore database create.

Create multiple database instances via Firebase CLI

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.

Resources