简体   繁体   English

Firebase 功能配置不会部署

[英]Firebase functions config won't deploy

I'm trying to deploy a function that uses a firebase config variable based on the environment variable I set before deploying.我正在尝试部署一个 function,它使用基于部署前设置的环境变量的 firebase 配置变量。 This works well on staging (a separate project that looks almost identical to production, except for a couple of env vars), but is failing on production with error TypeError: Cannot read property 'env' of undefined pointing to functions.config().app.env (see code below).这在暂存时效果很好(一个看起来几乎与生产相同的单独项目,除了几个 env var),但在生产中失败并出现错误TypeError: Cannot read property 'env' of undefined指向functions.config().app.env (见下面的代码)。

If I browse the function online in https://console.cloud.google.com/functions/details/us-central1/<functionName>?project=<projectName> the .runtimeconfig.json file appears to not have picked up the env variable I have set before deploying to production, while it does that perfectly on staging. If I browse the function online in https://console.cloud.google.com/functions/details/us-central1/<functionName>?project=<projectName> the .runtimeconfig.json file appears to not have picked up the env我在部署到生产之前设置的变量,而它在登台时完美地做到了这一点。

If I set app.env to "staging", it works great for my staging environment, and doesn't show any errors.如果我将app.env设置为“staging”,它适用于我的 staging 环境,并且不会显示任何错误。 I tried cd -ing into the functions folder to set the env variable, then back out before deploy, but it didn't change a thing.我尝试cd -ing 进入函数文件夹以设置 env 变量,然后在部署之前退出,但它没有改变任何事情。 I even tried writing it into .runtimeconfig.json in the functions folder (though that should only be needed for local testing), but that had no effect either.我什至尝试将它写入函数文件夹中的.runtimeconfig.json (尽管这应该只需要本地测试),但这也没有效果。

EDIT : following some more tests it came out that the .runtimeconfig I can see when browsing the function code online is generated from what I set locally via CLI to functions config, and is uploaded when I deploy.编辑:经过一些更多的测试后,我发现在在线浏览 function 代码时可以看到的.runtimeconfig是从我通过 CLI 本地设置到函数配置的内容生成的,并在我部署时上传。 It appears this copies fine, whenever I update it, onto staging, but does not do the same into production.每当我更新它时,它似乎都可以很好地复制到登台上,但在生产中却没有这样做。 I just can't figure out why...我就是想不通为什么...

My function:我的 function:

const functions = require('firebase-functions')
// Stripe needs to be set with its key when the library is being required
const stripe = require("stripe")(functions.config().app.env === 'staging' ? functions.config().stripe.key.staging : functions.config().stripe.key.production)
//                                                      ^^^ this is what the error is pointing to

module.exports = functions.https.onCall(async (data, context) => {
    // ... doin' some stripe magic
})

I'm using npm in a shell command line environment:我在 shell 命令行环境中使用 npm :

firebase functions:config:set app.env="production" && firebase deploy -P production --only functions

Output of firebase use : firebase 的firebase use

% firebase use                                                      
Active Project: default (<stagingProjectName>)

Project aliases for <projectFolderPath>:

* default (<stagingProjectName>)
  production (<productionProjectName>)

Run firebase use --add to define a new project alias.

Output of firebase functions:config:get (among other vars): firebase 的firebase functions:config:get (在其他变量中):

{
  "app": {
    "env": "production"
  }
}

I resolved this by first swapping to the production project, setting variables, then deploying from there.我首先切换到生产项目,设置变量,然后从那里部署来解决这个问题。

firebase use production
firebase functions:config:set someservice.key="keyvalue" ...
firebase deploy --only functions

Even though I did use the -P flag in my deploy command previously ( firebase -P production - see question), that did not set the appropriate config values, apparently.尽管我之前确实在部署命令中使用了-P标志( firebase -P production - 见问题),但显然没有设置适当的配置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM