Maslow
Maslow

Reputation: 1104

Cloud function storage and deploy target

I'm using deploy targets on Firebase and everything is working fine except my Cloud functions.

  1. The process.env.FIREBASE_CONFIG env variable doesn't have the right values. The properties "databaseURL" and "storageBucket" are not the one I've configured the target to use
  2. When I use admin.database() it doesn't use the right RTDB I've created and that I want to use
  3. functions.storage.object().onFinalize() doesn't "watch" the right bucket

My .firebaserc file

{
    "projects": {
        "default": "hatch-rpg",
        "dev": "hatch-rpg"
    },
    "targets": {
        "hatch-rpg": {
            "hosting": {
                "shadra": [
                    "shadra"
                ]
            },
            "database": {
                "shadra": [
                    "hatch-shadra-dev"
                ]
            },
            "storage": {
                "shadra": [
                    "hatch-shadra-dev"
                ]
            }
        }
    }
}

My firebase.json file

{
    "firestore": {
        "rules": "firestore.rules",
        "indexes": "firestore.indexes.json"
    },
    "database": [
        { "target": "shadra", "rules": "database.rules.json" }
    ],
    "functions": {
        "predeploy": [
            "npm --prefix \"$RESOURCE_DIR\" run lint",
            "npm --prefix \"$RESOURCE_DIR\" run build"
        ],
        "source": "functions"
    },
    "hosting": [
        {
            "target": "shadra",
            "public": "dist/shadra",
            "ignore": [
                "firebase.json",
                "**/.*",
                "**/node_modules/**"
            ],
            "rewrites": [
                {
                    "source": "**",
                    "destination": "/index.html"
                }
            ]
        }
    ],
    "storage": [
        { "target": "shadra", "rules": "storage.rules" }
    ]
}

I'm initializing Firebase like this

admin.initializeApp(functions.config().firebase);

What am I missing ?

Thanks a lot

EDIT: I have tried to override FIREBASE_CONFIG by doing firebase functions:config:set firebase.databaseURL="hatch-shadra-dev.firebaseio.com" firebase.storageBucket="hatch-shadra-dev" but it didn't worked because Invalid config name firebase.databaseURL, cannot use upper case. then tried without uppercase and got Cannot set to reserved namespace firebase How can I override this FIREBASE_CONFIG ? or should I just not use it initialize manually firebase ?

Upvotes: 1

Views: 969

Answers (1)

Maslow
Maslow

Reputation: 1104

Ok, here's what I did to fix this issue.

Maybe there's a better option or I'm just mis-using Firebase here. Anyway, heres' the solution:

First, add some config to the project

firebase functions:config:set target.database="hatch-shadra-dev.firebaseio.com" target.bucket="hatch-shadra-dev"

Then use it in the Cloud function with admin.initializeApp()

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

const config = functions.config();
admin.initializeApp({
  ...config.firebase,
  storageBucket: config.target.bucket,
  databaseURL: `https://${config.target.database}`
});

Upvotes: 1

Related Questions