多个项目/环境的Firebase配置 [英] Firebase configuration for multiple projects/environments

查看:227
本文介绍了多个项目/环境的Firebase配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Cloud Functions用于Firebase和三个不同的项目用于开发,测试和生产目的.每个项目都有一个service-account.json.当我将源部署到环境中时,初始化看起来像这样:

I'm using Cloud Functions for Firebase with three different projects for development, testing and production purposes. Each project has a service-account.json. When I deploy the sources to an environment, the initialization looks like this:

var serviceAccount = require("./service-account-dev.json");

firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "https://nwDEV.firebaseio.com"
});

这有点难以处理,因为每次要部署到不同的环境时,我都必须更改代码.有没有一种方法可以进行整体配置,例如在firebase.json或.firebasesrc中,它可以集成服务帐户并决定部署时选择哪种配置?

This is a bit difficult to handle, because I have to change the code everytime I want to deploy to a different environment. Is there a way to have an overall configuration, e.g. in firebase.json or.firebasesrc, which allows to integrate the service-account and decides on deployment which configuration to choose?

是否有可能检测代码在哪种环境下运行并加载特定的service-account.json并设置数据库的URL属性?

Otherwise is there a possibility to detect under which environment the code is running and to load the specific service-account.json and to set the databaseURL-property?

推荐答案

您可以使用环境变量. https://firebase.google.com/docs/functions/config-env

You can use environment variables. https://firebase.google.com/docs/functions/config-env

  1. 选择项目(您可以使用命令firebase projects:list来查看它们): firebase use my-project-development

设置环境变量 firebase functions:config:set app.environment="dev"

在您的函数文件中,应用条件来选择文件: const serviceAccount = functions.config().app.environment === 'dev' ? 'credentials-dev.json' : 'credentials-prod.json';

In your functions file, apply a conditional to choose the file: const serviceAccount = functions.config().app.environment === 'dev' ? 'credentials-dev.json' : 'credentials-prod.json';

然后您可以根据项目使用文件:

Then you can use the file depending on the project:

firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "https://nwDEV.firebaseio.com"
});

这篇关于多个项目/环境的Firebase配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆