This page explains:
Note: The FIREBASE_CONFIG environment variable is included automatically in Cloud Functions for Firebase functions that were deployed via the Firebase CLI.
Can we get access to the same FIREBASE_CONFIG environment variable in Cloud Run environment? If yes, how?
If no, please give instructions on setting firebase-admin initializeApp and firebase initializeApp for authentication on Cloud Run.
Should we save the serviceAccount and firebase-config in our project and import them? If so, what are the best practices for doing that? When working on an open-source project, we should put these files in .gitignore. Then how should we push them to the Cloud Run environment using gcloud CLI?
Related
I have a subscription-based android app which required the used of firebase cloud functions for notification schedule.
Now I am confused about where should I create the functions directory? Should I make it in the android project directory itself or make a separate project directory for it?
Where should I create the functions directory?
You should create it as a sub-folder (or sub-directory) of your Firebase project folder. This way you will be able to deploy it with firebase deploy ....
If you didn't choose to add Cloud Functions when you created your Firebase project, you can run firebase init functions in the terminal under your Firebase project main folder.
More info here.
I usually have my Firebase projects organized with a separate repo for the web app, manager portal, and cloud functions. Because the web app and manager portal use the same firebase project I keep the cloud functions in their own separate repo. So 3 different repos/projects using the same Firebase project.
I'm testing out how to use the Firebase Local Emulator Suite with this set up but it doesn't appear I can start the emulator for the cloud functions within my Cloud Function project, then use the emulator in my web app project for firestore and calling the functions, and then the same for my manager portal project. This will cause conflicts and cause them all to run on different ports and the functions project won't have access to the firestore emulator and the web app and manager won't have access to the functions emulator.
Is there a way to make this work? The only way I can see how is to have my manager app, web app, and functions all in the same project/repo...which I don't want to do for a bunch of reasons.
Currently I just set up a 2nd Staging project on firebase and use that for testing but would love to be able to do all of this locally. Any help is much appreciated.
I was just struggling with the same today and it turns out it is as simple as:
in your main project, start your emulators as usual with e.g. firebase emulators:start --only firestore
in your dependent projects, do not try to start the firestore emulator, but instead initialise the firebase config as follows:
import * as express from 'express'
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
admin.initializeApp(functions.config().firebase)
var db = admin.firestore()
if (process.env.NODE_ENV !== 'production') {
db.settings({
host: "localhost:8080",
ssl: false
})
}
You can use symbolic links in your project directories to create a "fake" functions directory. Then you can run a single instance of the emulator.
Running two instances of the emulator can cause several weird bugs as it is not recommended.
On windows:
mklink /D "R:\my-cloud-functions-proj\functions" "R:\my-web-app\functions"
When initializing a firebase project, it creates a firebase.json file containing settings for hosting, firestore, storage, but nothing for functions. When deploying, the firebase CLI correctly finds the functions. Does the CLI goes through the whole project looking for functions or does it store the functions' folder path somewhere?
The Firebase CLI by default assumes that the Cloud Functions for your projects are in a functions folder under the project folder.
You can override this by specifying a source value in the functions configuration in firebase.json.
Also see Doug's answer here: How to deploy functions from other directory than '/functions'?
I experimented with firebase functions for the init part and it has created a folder for me. Now when I deploy each time it also picks up the functions folder. When I remove the functions folder I get an error, when deploying. How do I deploy everything but functions?
firebase deploy has a parameter --except . So to deploy everything except functions you can run:
firebase deploy --except functions
You can use the only option/flag, for example firebase deploy --only hosting,storage, see the CLI doc.
You can also use deploy targets which "are short-name identifiers (that you define yourself) for Firebase resources in your Firebase project".
Check to make sure that you have removed the implicit reference to the function in your index.js and then running firebase deploy should do the trick. If you want to explicitly delete the function completely, you can use the firebase functions:delete <myFunction> and then delete the functions folder. You can find more details in the doc here
I'm migrating a Firebase Function to Cloud Run. Everything is working as expected so far, including the Firebase Hosting link (which is great!). I'm just not sure how I should read the environment variables I've configured for this project (via https://firebase.google.com/docs/functions/config-env).
You'll have to find a different way to configure Cloud Run. You won't be able to access your environment variables set for Cloud Functions when deployed with the Firebase CLI. Those variables are only accessible for code running in Cloud Functions that uses the "firebase-functions" module.
Instead, you should set your Cloud Run environment with gcloud as documented here.