Firebase 功能 - 使用参数化配置根据逻辑有条件地部署功能

问题描述 投票:0回答:1

我们曾经将 firebase 函数配置与第一代函数一起使用,如下代码片段所示。像这样的代码过去只能在生产环境中部署该功能:

const functions = require('firebase-functions')


exports.onlyUseThisInProduction = functions.config().someValue === 'production' &&
 functions.pubsub
    .schedule('* * * * *')
    // ...


但现在我们使用此处描述的参数化配置和第二代函数:https://firebase.google.com/docs/functions/config-env?gen=2nd

现在代码更像是这样:

const { defineString } = require('firebase-functions/params')
const { onSchedule } = require('firebase-functions/v2/scheduler')
const someValue = defineString('SOME_VALUE').value()

exports.onlyUseThisInProduction = someValue === 'production' && onSchedule({ schedule: '* * * *' ... })

函数

onlyUseThisInProduction
永远不会与上面的代码一起部署。我们的其他功能运行正常,但本应有条件部署的功能却从未部署。我知道我们可以做一些事情,例如通过 CLI 仅部署特定功能,但我要控制可以通过代码部署哪些功能,因此如果我们只运行
firebase deploy
它不会部署某些功能。

node.js firebase google-cloud-functions firebase-tools
1个回答
0
投票

为此不可能使用 DefineString 和其他 parameters,因为参数仅在函数运行时可用,而不是在部署时可用。

您可以尝试找到其他方法在部署时注入值,但我怀疑这是不可能的。我认为您应该使用 Firebase CLI 确定要部署哪些功能,因为这是唯一直接支持的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.