Target Deploy for Firebase Functions - firebase

I'm trying to set a target deploy for Firebase Functions.
https://firebase.google.com/docs/cli/targets
This is the firebase.json file
{
"functions": [{
"target": "production",
"source": "src/prod/functions",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}]
}
The predeploy runs normally but when deploying gets an error
functions[prod]: Finished running predeploy script.
Error: An unexpected error has occurred.
If I go to firebase-debug.log it shows:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
Putting the Functions folder on the project root it will deploy normally.
What is missing here?

Try running firebase init and set the options as best you can so it doesn't overwrite your existing project. I had to run it from the parent directory of /functions otherwise it will create a new directory but maybe there is some option to set a different directory name to suit your case.

From the link that you posted:
Firebase supports deploy targets for:
Firebase Hosting sites
Cloud Storage for Firebase storage buckets
Firebase Realtime Database instances
I was also looking for the same answer, but it seems that it is still not supported in the end of 2022

Related

firebase firestore index building [duplicate]

I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

How to uninstall Firebase functions emulator after installing it with "firebase init"

I installed the Firebase Functions and Firestore emulators in my project with firebase init > emulators but now realize I don't need the Functions emulator any more so I'm trying to delete it.
How can I uninstall an emulator so that the firebase emulators:start command doesn't try to start it by default?
I tried the following but firebase init > emulators continues to show that the functions emulator is still enabled (green dot) and firebase emulators:start continues to start the functions emulator by default if I don't include the --only firestore flag.
I reran firebase init > emulators and deselected functions
This didn't do anything
I manually removed the entry from firebase.json
"emulators": {
"functions": { // <-- I removed this block
"port": 5001
},
"firestore": {
"port": 8080
},
"ui": {
"enabled": true
}
}
I cleared the emulator cache from ~/.cache/firebase/emulators
I removed the functions block from firebase.json just to see if that was triggering it.
{
"functions": { // <-- I removed this block
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run clean",
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"firestore": {
"port": 8080
},
"ui": {
"enabled": true
}
}
}
After doing all of the above, firebase emulators:start still tries to start functions. I wondered if it might be because the firestore emulator depends on the functions emulator but if that were the case I don't know why it would let me run the firestore emulator alone with --only firestore
In my case, I deleted the Function directory from node_modules and I removed the functions block from firebase.json too.
I fixed it just running npm install firebase again.
If you don't want to use Cloud Functions in your project, you should remove the "functions" folder from your project directory as well.
Also, suggest that you file a bug for this on the firebase-tools GitHub. The fact that the functions emulator runs without configuration in firebase.json feels like a bug, and it didn't used to work that way.

Copy Composite Indexes on GCP [duplicate]

I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

How to export security and index rules from Firestore?

I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.
It's possible!
Run from CLI firebase firestore:indexes inside your firebase project folder.
Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.
Example:
{
"indexes": [
{
"collectionId": "teslaData",
"fields": [
{
"fieldPath": "Model",
"mode": "ASCENDING"
},
{
"fieldPath": "Price",
"mode": "ASCENDING"
}
]
}
]
}
Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.
https://firebase.google.com/docs/firestore/query-data/indexing
You can also deploy indexes with the Firebase CLI. To get started, run
firebase init firestore in your project directory. During setup, the
Firebase CLI generates a JSON file with the default indexes in the
correct format. Edit the file to add more indexes and deploy it with
the firebase deploy command. If you only want to deploy indexes, add
the --only firestore:indexes flag. If you make edits to the indexes
using the Firebase console, make sure you also update your local
indexes file.
I'm using Firebase CLI 4.2.1 if that helps.
Edit: It's still working as of 9.6.0.
In your Firebase project folder execute this in the terminal:
firebase firestore:indexes > firestore.indexes.json
And it will save a file called firestore.indexes.json with your indexes.
You can then upload that file onto other Firebase projects.
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
This is how my project files are laid out
myProjectFolder
.firebaserc
firebase.json
firestore.indexes.json
functions
Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file
myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)
In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"firestore": {
"indexes": "firestore.indexes.json"
}
}
Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.
If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file.
Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'
Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab
for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.
sample firestore.indexes.json file
{
"indexes": [
{
"collectionGroup": "faq",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "searchKeywords",
"arrayConfig": "CONTAINS"
},
{
"fieldPath": "answered",
"order": "ASCENDING"
},
{
"fieldPath": "relevanceScore",
"order": "ASCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

How do you setup local environment variables for Cloud Functions for Firebase

I'm using http cloud functions to listen for a request and then return a simple message.
I'm developing cloud functions locally using:
firebase serve --only functions
I've setup some custom environment variables using
firebase functions:config:set
Accessing the custom config variables using the below code works fine when the project is deployed
functions.config()
but it does not work when developing locally. When the function is triggered by hitting: http://localhost:5002/my-project-name/us-central1/functionName I can't access the custom config variables. when using functions.config() locally, I can see the default config, just not my custom config variables
Is there an alternate solution or best practice for environment variables when working locally?
As of now, you have to manually create a .runtimeconfig.json file inside your functions directory by running this command. Then run the serve command.
firebase functions:config:get > .runtimeconfig.json
If you are using Windows Powershell, replace the above with:
firebase functions:config:get | ac .runtimeconfig.json
You can learn more in https://firebase.google.com/docs/functions/local-emulator
For those who want to use the environment variables (process.env), I follow this workaround.
Set the config values before deploying
firebase functions:config:set envs.db_host=$DB_HOST_PROD envs.db_user=$DB_USER_PROD envs.db_password=$DB_PASSWORD_PROD envs.db_name=$DB_NAME_PROD envs.db_use_ssl=false
Read the config and update the env variables first thing under your functions code.
const functions = require('firebase-functions');
const config = functions.config();
// Porting envs from firebase config
for (const key in config.envs) {
process.env[key.toUpperCase()] = config.envs[key];
}
You can keep a file called .env.json and load it when you trigger deploy command
{
"name": "project",
"version": "0.0.0",
"scripts": {
"deploy": "npm run env && firebase deploy --only functions",
"env": "test -f env.json && firebase functions:config:unset env && firebase functions:config:set env=\"$(cat env.json)\" || echo \"Please add the file env.json before deploy.\""
},
"dependencies": {
"firebase-functions": "^3.1.0"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
}
}
I've narrowed down the issue to Windows Powershell.
Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json I don't know why, which when parsed gives Unexpected token � in JSON at position 0.
I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.
If you are using Nrwl NX, you will have to generate your .runtimeconfig.json inside of the dist/apps/functions directory.
Example package.json:
{
"scripts": {
"firebase:emulators:start": "firebase functions:config:get > dist/apps/functions/.runtimeconfig.json && env-cmd firebase emulators:start --export-on-exit=\".firebase-emulator\" --import=\".firebase-emulator\""
}
}
I am not sure if the top-rated answer works or not but for firebase function on mac (to-serve locally), I do something like this
npm run admin-keys && export dev=true && firebase emulators:start
Where admin keys is
"admin-keys": "export GOOGLE_APPLICATION_CREDENTIALS='./.keys/admin.keys.json'"
This will load configuration from .runtimeconfig.json
For production, you would manually have to set it by doing something like this
firebase functions:config:set facebookCred.secret="something"

Resources