firebase.json and multiple projects
This is connected to https://github.com/firebase/firebase-tools/issues/560
I followed @justinrosenthal 's instructions, but I don't understand how I can have multiple projects in firebase.json.
My configuration is as follows:
.firebaserc:
{
"projects": {
"dev": "project-dev",
"prod": "project-prod"
},
"targets": {
"project-prod": {
"database": {
"main": [
"prod-db-01",
"prod-db-02",
"prod-db-03",
"prod-db-04"
]
}
},
"project-dev": {
"database": {
"main": [
"dev-db-01",
"dev-db-02"
]
}
}
}
}
And firebase.json:
{
"database": [
{ "target" : "main", "rules": "database.rules.json" }
]
}
I want to have different rules for all databases in project-dev and all databases in project-prod. Something like this:
{
"database": [
{ "target" : "main", "project" : "project-dev", "rules": "dev-database.rules.json" },
{ "target" : "main", "project" : "project-prod", "rules": "prod-database.rules.json" }
]
}
Basically I want to do this:
# firebase use dev
# firebase deploy --only database # uploads dev-database.rules.json to dev-db-01 and dev-db-02
# firebase use prod
# firebase deploy --only database # uploads prod-database.rules.json to prod-db-01, prod-db-02, prod-db-03 and prod-db-04
Thanks in advance.
Right now the Firebase CLI is built to treat projects as anonymous environments that are functionally identical. This is important to be able to deploy the same assets to multiple projects without having to alter the code (including in firebase.json).
To achieve what you want, you'll need to set up a dev and prod folder, each with their own firebase.json and each with a target only for that specific project. Deploying different assets to different projects is not supported now and is unlikely to be supported in the future (however, we may allow configuring the location of firebase.json via a flag at some point).
How about just letting us specify which firebase.json file to use like almost any other tool that uses a config file?
It would solve pretty much every problem we have with firebase atm.
Specifying config file is something I'd like to support in the future.
On Fri, Jul 20, 2018, 3:31 AM Andri MΓ‘r SigurΓ°sson [email protected] wrote:
How about just letting us specify which firebase.json file to use like almost any other tool that uses a config file?
It would solve pretty much every problem we have with firebase atm.
β You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-tools/issues/590#issuecomment-406560971, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAD_uVyVgg86foP59VHY_pPI3F4E2nZks5uIbFmgaJpZM4RUdVX .
is it the future yet?
This is really an issue for me as well. The workaround with different folders seems like a dirty hack. Please provide the CLI argument to specify a path to the firebase.json file to use.
It is this in progress? It is really needed on Monorepos
I struggled on this for almost 2 hours now. Tried all sorts of firebase configurations (2 sites, separate projects/ 2 sites, single project). I basically want a landing page hosted on both a production and staging environment, but configuring the .firebaserc and firebase.json files is pretty confusing, given the target/alias naming (which you define).
This makes me wonder under what possible use cases can shared resources across multiple sites be leveraged if we cannot easily deploy identical apps in different environments/configurations. I may be missing something here.
I realized the issue was having both websites' hosting configs in a single firebase.json file. as @mbleigh mentioned, one solution is creating a prod and dev folder, each with a separate firebase.json. Personally, I prefer just swapping my firebase.json files in my npm deploy scripts in package.json
// package.json
"deploy": "npm run swap-prod && ng build --aot --prod && firebase use production && firebase deploy",
"deploy-staging": "npm run swap-staging && ng build --configuration=staging && firebase use staging && firebase deploy",
"swap-prod": "cp prod.firebase.json firebase.json",
"swap-staging": "cp staging.firebase.json firebase.json",
Note, you can swap these files in angular.json's fileReplacements array, since firebase.json is not in your src directory (at least I don't have it that way).
tldr
create a prod.firebase.json and a staging.firebase.json along with your firebase.json at the root of your project. swap them out in your deploy commands in package.json.
project here -> https://github.com/superfitapp/superfit-main-landing-site
Deploying multiple sites to a single project and multiple projects seems like such a basic requirement: cdn / website / app etc to dev / `prod etc. I hope this gets added to the config soon
We support both multiple sites in a single project and multiple projects. This bug is specifically about using different firebase.json configs for these things, which is not currently supported.
Thanks for clarifying @mbleigh
I hope this gets added to the config https://github.com/firebase/firebase-tools/issues/590#issuecomment-528557023
Thanks @mbleigh for the quick reply. I managed to do it with cli parameters, but in the firebase.json would make it cleaner
It's a great project and a big thank you to everyones hard work, I know it is not easy maintaining open source projects π€
This is connected to #560
I followed @justinrosenthal 's instructions, but I don't understand how I can have multiple projects in firebase.json.
My configuration is as follows:
.firebaserc:
{ "projects": { "dev": "project-dev", "prod": "project-prod" }, "targets": { "project-prod": { "database": { "main": [ "prod-db-01", "prod-db-02", "prod-db-03", "prod-db-04" ] } }, "project-dev": { "database": { "main": [ "dev-db-01", "dev-db-02" ] } } } }And firebase.json:
{ "database": [ { "target" : "main", "rules": "database.rules.json" } ] }I want to have different rules for all databases in project-dev and all databases in project-prod. Something like this:
{ "database": [ { "target" : "main", "project" : "project-dev", "rules": "dev-database.rules.json" }, { "target" : "main", "project" : "project-prod", "rules": "prod-database.rules.json" } ] }Basically I want to do this:
# firebase use dev # firebase deploy --only database # uploads dev-database.rules.json to dev-db-01 and dev-db-02 # firebase use prod # firebase deploy --only database # uploads prod-database.rules.json to prod-db-01, prod-db-02, prod-db-03 and prod-db-04Thanks in advance.
Thanks
Ran into a similar problem. I have 3 projects dev, staging and production. I want to deploy different security rules for storage and firestore to different projects. I am thinking of maintaining the following project structure
./
βββ README.md
βββ firebase.json
βββ load_firestore_rules.sh
βββ load_storage_rules.sh
βββ functions
βΒ Β βββ ...
βΒ Β βββ ...
βΒ Β βββ index.js
βΒ Β βββ node_modules
βΒ Β βββ package-lock.json
βΒ Β βββ package.json
βΒ Β βββ test
βββ rules
βΒ Β βββ dev
βΒ Β βββ production
βΒ Β βββ staging
The rules files reside in project dependent rules folders i.e for example rules/dev for project id project_name-dev.
Modify firebase.json like so
"firestore": {
// ...
"rules": "firestore.rules",
"predeploy": "./load_firestore_rules.sh $GCLOUD_PROJECT",
"public": "public"
},
"storage": {
"rules": "storage.rules",
"predeploy": "./load_storage_rules.sh $GCLOUD_PROJECT"
}
The load_firestore_rules.sh bash script looks like so
#!/bin/bash
project_id=$1
if [ project_id == "project_name-dev"]
then
cat ./rules/dev/rules > ./firestore.rules. #overwrite firestore_rules
elif [ project_id == "project_name-staging"]
then
cat ./rules/staging/rules > ./firestore.rules
elif [ project_id == "project_name-production"]
then
cat ./rules/production/rules > ./firestore.rules
else
fi
Finally, this is how it will be used
> cd project_directory
> firebase use project_name-dev
# Following command runs ./load_firestore_rules script which loads project_name-dev's firestore dev rules into firestore.rules, followed by deployment
> firebase deploy --firestore:rules
All of this can go into source control. firestore.rules and storage.rules in top-level dir don't need to be part of src control though as they are frequently overwritten
@ziphawkteamusa and others we just released version 8.7.0 which has support for the --config flag to point at mulitple firebase.json files.
So for example:
firebase-main.json
{
"firestore" : { "rules": "firestore-main.rules" }
}
firebase-alt.json
{
"firestore" : { "rules": "firestore-alt.rules" }
}
Then you can do:
$ firebase --config=./firebase-main.json deploy
or
$ firebase --config=./firebase-alt.json deploy
@samtstern Is there a way I can specify the configuration file path to be used by different projects in .firebaserc? Something like:
{
"projects": {
"dev": "project-dev",
"prod": "project-prod"
},
"targets": {
"project-prod": {
"database": {
"main": [
"prod-db-01",
"prod-db-02",
"prod-db-03",
"prod-db-04"
]
},
"config": "<path-to-prod-firebase.json>"
},
"project-dev": {
"database": {
"main": [
"dev-db-01",
"dev-db-02"
]
},
"config": "<path-to-dev-firebase.json>"
}
}
}
@waseem no sorry there's no way to do that. I'd suggest writing your own script to wrap the Firebase CLI to get advanced config switching like that.
@ziphawkteamusa and others we just released version
8.7.0which has support for the--configflag to point at mulitplefirebase.jsonfiles.So for example:
firebase-main.json
{ "firestore" : { "rules": "firestore-main.rules" } }firebase-alt.json
{ "firestore" : { "rules": "firestore-alt.rules" } }Then you can do:
$ firebase --config=./firebase-main.json deployor
$ firebase --config=./firebase-alt.json deploy
@samtstern I've been looking for this for so long. So great !
Is it me or is it not written in the documentation of firebase.json file ? Should be added imo, as it enables a whole bunch of flexibility in a monorepo.
As i understand this, it's a full replacement. One firebase.json for another alternate firebase.json.
Is there a plan to have the following feature: extend 2 firebase.json files (for example) from a base firebase.json, just like we have in tsconfig.json files ?
Would be perfect to set commun rules in the base.firebase.json, and then, write specific parts of a target let's say in dedicated firebase.json, or a specific configuration for functions.
@johanchouquet no sorry we don't have any plans for that, we don't see --config as a common use case but more of a backdoor and we don't plan to expand the complexity of it in the future.
Thanks for your feedback @samtstern.
Not sure this is the best place to discuss about that btw, but your answer makes me thinking about the following:
Without the --config feature, I'm very curious to get feedbacks on how to implement best practices on how to develop in a monorepo multiples projects (functionally speaking) with the Firebase stack (Hosting / Functions / Firestore / Rules etc...), with all goodies Firebase has to offer (channels, targets), with support of different environments (staging / prod) for each project.
My experience is that we need more flexibility, specially towards functions. This has become very hard as soon as we have multiple targets for one project, and not all functions have to be deployed every time. The standard config sure is nice, but it's kind of restrictive when we have to face the enterprise extensibility.
One good point about this --config feature is that it offers some flexibility. So, for now, I'm happy to take it :)!! If you have a better channel for me to elaborate, would gladly discuss more about that topic.