使用 GOOGLE_APPLICATION_CREDENTIALS 部署的 Firebase 函数无法在全局范围内获取环境配置
Firebase functions deploy using GOOGLE_APPLICATION_CREDENTIALS failing to get an environment config on global scope
我正在使用 Service Account authentication 部署我的 firebase 函数(使用 GOOGLE_APPLICATION_CREDENTIALS
变量)。这是我的脚本:
cross-env GOOGLE_APPLICATION_CREDENTIALS=./file.json firebase --project=dev deploy --only functions
效果很好,成功部署了我的 firebase 函数。
但是,当我尝试使用 Environment configuration 时,我在尝试使用以下标准代码获取我的配置变量之一时遇到错误:
const domain = functions.config().env.domain;
这是仅在执行此代码行外部函数时发生,意思是在全局范围内。
这是我遇到的错误:
Error: Error occurred while parsing your function triggers.
TypeError: Cannot read property 'domain' of undefined
重要说明:当我使用 firebase login
命令登录到 firebase 时,我没有收到此错误,只有在我注销时才会收到该错误!
我做错了什么?
谢谢!
我已经尝试复制您的代码,并且在我这边运行良好。
这是我在 index.js 文件中包含的内容。
const functions = require("firebase-functions");
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
const domain = functions.config().env.domain;
response.send(domain);
});
这是我的 package.json 文件:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "cross-env GOOGLE_APPLICATION_CREDENTIALS=./key-file.json firebase --project=[Project-ID] deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"shared-types": "file:shared-types"
},
"devDependencies": {
"cross-env": "^7.0.3",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
在我在 Firebase 注销的情况下部署函数后,我 运行 这个 CLI 命令进行验证:firebase functions:config:get
,得到这个结果:
{
"env": {
"domain": "test"
}
}
由于您的错误是Cannot read property 'domain' of undefined
,错误似乎来自您的环境配置。您应该仔细检查 dev environment/project 中的环境配置是否正确设置。为此,您应该使用 firebase functions:config:get
CLI 命令,如我发现的 documentation. You can also check this 中所述。
我的答案来自 firebase/firebase-tools 回购团队,在 this issue。
It seems likely that this is a permissions issue given the 403 response. Can you give your service account the roles/firebase.developAdmin role and see if it works after that?
这确实是问题所在。将该角色 roles/firebase.developAdmin
添加到我正在部署的服务帐户后,我成功部署了!
我正在使用 Service Account authentication 部署我的 firebase 函数(使用 GOOGLE_APPLICATION_CREDENTIALS
变量)。这是我的脚本:
cross-env GOOGLE_APPLICATION_CREDENTIALS=./file.json firebase --project=dev deploy --only functions
效果很好,成功部署了我的 firebase 函数。
但是,当我尝试使用 Environment configuration 时,我在尝试使用以下标准代码获取我的配置变量之一时遇到错误:
const domain = functions.config().env.domain;
这是仅在执行此代码行外部函数时发生,意思是在全局范围内。
这是我遇到的错误:
Error: Error occurred while parsing your function triggers. TypeError: Cannot read property 'domain' of undefined
重要说明:当我使用 firebase login
命令登录到 firebase 时,我没有收到此错误,只有在我注销时才会收到该错误!
我做错了什么?
谢谢!
我已经尝试复制您的代码,并且在我这边运行良好。
这是我在 index.js 文件中包含的内容。
const functions = require("firebase-functions");
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
const domain = functions.config().env.domain;
response.send(domain);
});
这是我的 package.json 文件:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "cross-env GOOGLE_APPLICATION_CREDENTIALS=./key-file.json firebase --project=[Project-ID] deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"shared-types": "file:shared-types"
},
"devDependencies": {
"cross-env": "^7.0.3",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
在我在 Firebase 注销的情况下部署函数后,我 运行 这个 CLI 命令进行验证:firebase functions:config:get
,得到这个结果:
{
"env": {
"domain": "test"
}
}
由于您的错误是Cannot read property 'domain' of undefined
,错误似乎来自您的环境配置。您应该仔细检查 dev environment/project 中的环境配置是否正确设置。为此,您应该使用 firebase functions:config:get
CLI 命令,如我发现的 documentation. You can also check this
我的答案来自 firebase/firebase-tools 回购团队,在 this issue。
It seems likely that this is a permissions issue given the 403 response. Can you give your service account the roles/firebase.developAdmin role and see if it works after that?
这确实是问题所在。将该角色 roles/firebase.developAdmin
添加到我正在部署的服务帐户后,我成功部署了!