GitHub Action中Firebase Functions模拟器加载失败排查求助
可能的核心原因
远程配置拉取覆盖本地合法文件
你的start:automation脚本里执行了firebase functions:config:get > .runtimeconfig.json,这会从Firebase远程拉取配置并覆盖本地的.runtimeconfig.json。在GitHub Action环境中,可能因权限不足、dev环境未配置远程config,或firebase use dev未正确生效,导致拉取的内容为空/格式错误,最终覆盖了原本合法的文件,引发读取key时的未定义错误。JSON文件存在语法错误
你提供的.runtimeconfig.json示例最后一个属性后有多余逗号:"field": { "list": [ "SOME_STRING" ] }, // 此处逗号为无效JSON语法部分本地JSON解析器可能宽容这类错误,但GitHub Action环境中的Node.js或Firebase工具严格遵循JSON规范,会导致文件无法被正确解析。
环境变量传递或路径问题
虽然设置了CLOUD_RUNTIME_CONFIG绝对路径,但firebase emulators:exec执行时工作目录可能变化,或环境变量未正确传递到模拟器进程,导致工具无法找到指定配置文件。
具体解决方案
1. 移除远程配置拉取步骤
既然项目中已包含合法的.runtimeconfig.json,无需从远程拉取。修改start:automation脚本,删除firebase functions:config:get > .runtimeconfig.json这一行:
"start:automation": "CLOUD_RUNTIME_CONFIG=/home/runner/work/uvcamp/uvcamp/firebase/cloud-functions/.runtimeconfig.json firebase use dev && firebase emulators:exec --import=./emulator-data --only=auth,firestore,functions,storage \"npm run start:playwright-test\""
2. 修复JSON语法错误
删除.runtimeconfig.json中最后一个属性后的多余逗号,修正后文件如下:
{ "coockie": { "key": "SOME_STRING" }, "field": { "list": [ "SOME_STRING" ] } }
3. 验证环境变量与工作目录
- 在GitHub Action步骤中添加调试步骤,检查配置文件的路径和内容:
- name: Debug config file run: | cd firebase/cloud-functions echo "Config file path: $(pwd)/.runtimeconfig.json" cat .runtimeconfig.json - 将
CLOUD_RUNTIME_CONFIG改为相对路径,确保在firebase/cloud-functions目录下执行时能正确定位:"start:automation": "CLOUD_RUNTIME_CONFIG=./.runtimeconfig.json firebase use dev && firebase emulators:exec --import=./emulator-data --only=auth,firestore,functions,storage \"npm run start:playwright-test\""
4. 确保脚本执行原子性
在start:automation脚本开头添加set -e,保证前面的命令失败时立即停止,避免错误操作覆盖文件:
"start:automation": "set -e; CLOUD_RUNTIME_CONFIG=./.runtimeconfig.json firebase use dev && firebase emulators:exec --import=./emulator-data --only=auth,firestore,functions,storage \"npm run start:playwright-test\""
内容的提问来源于stack exchange,提问作者hagay paravy

