如何在Expo EAS构建中向Maestro传递环境变量?
在Expo EAS中为Maestro传递环境变量的解决方案
方案1:构建前动态替换Maestro配置文件占位符
在EAS构建流程中添加预处理步骤,直接替换maestro.yml中的${APP_ID}为实际环境变量值,绕过Maestro的变量解析逻辑:
- 在项目
package.json中添加预处理脚本:
"scripts": { "pre-maestro-test": "sed -i '' 's/${APP_ID}/'\"$APP_ID\"'/g' maestro.yml" }
(注:Linux环境可简化为sed -i 's/${APP_ID}/'\"$APP_ID\"'/g' maestro.yml)
- 在
eas.json的构建配置中指定测试命令:
{ "build": { "production": { "scripts": { "test": "npm run pre-maestro-test && maestro test maestro.yml" } } } }
方案2:通过EAS环境变量直接传递给Maestro命令
和本地执行逻辑对齐,显式在Maestro命令中传递变量:
- 在EAS Dashboard的项目Secrets中添加
APP_ID,设置对应环境的应用ID。 - 在
eas.json中配置测试脚本:
{ "build": { "production": { "scripts": { "test": "maestro test -e APP_ID=$APP_ID maestro.yml" } } } }
方案3:让Maestro读取系统环境变量
Maestro支持直接读取进程环境变量,只要EAS构建时APP_ID已注入:
- 在EAS Dashboard添加
APP_ID为项目Secret。 - 在
eas.json中显式声明要暴露的环境变量并执行测试:
{ "build": { "production": { "env": { "APP_ID": "$APP_ID" }, "scripts": { "test": "maestro test maestro.yml" } } } }
关键注意事项
- 可在测试脚本中添加
echo $APP_ID验证变量是否正常加载。 - 多环境场景可在
eas.json的不同构建配置中绑定对应环境的APP_ID值,或通过Secrets分组管理。
内容的提问来源于stack exchange,提问作者RightHot
相关产品推荐
相关产品推荐

