使用fastlane构建的React Native应用程序-设置ENV变量并从process.env加载JS代码.<



我为ios和android应用设置了不同的《Fastlane》通道。我想在应用程序中显示一些值,例如最新构建号(在测试飞行或google play),最新提交哈希,更改日志等。

我可以把它放在这样的车道上:

previous_build_number = latest_testflight_build_number(
app_identifier: app_identifier,
api_key: api_key
)
build_number_int = previous_build_number + 1
commit = last_git_commit
commit_message = commit[:message]
commit_hash = commit[:abbreviated_commit_hash]

然后我认为有可能将这些值设置为ENV并在应用程序中获取:

ENV["BUILD_NUMBER"] = "#{build_number_int}"
ENV["COMMIT_HASH"] = "#{commit_hash}"
ENV["CHANGELOG"] = "[#{git_branch}] - #{commit_message}"
ENV["VERSION"] = "#{version}" 

但是当我试图在JavaScript代码中使用ENV["VARIABLE"]设置这些变量时:

export const getAppVersion = () => process.env.VERSION || '';
export const getCommitHash = () => process.env.COMMIT_HASH || '';
export const getBuildVersionNumber = () => process.env.BUILD_NUMBER || '';
export const getChangelog = () => process.env.CHANGELOG || '';

它们的结果都是一个空字符串。你能给我一些建议吗?

react-native-dotenv模块可以用来完成这个任务。

有一篇关于如何在应用程序中实现模块的好文章:https://blog.logrocket.com/understanding-react-native-env-variables/

必须采取的步骤:

  1. 在项目的根级别创建(或移动)fastlane文件夹,而不是在iosandroid文件夹中创建。在这种情况下,./ios/路径必须添加到action行,如cocoapods(clean_install: true, podfile: "./ios/Podfile")

  2. 安装react-native-dotenv,执行npm install -D react-native-dotenvyarn add -D react-native-dotenv命令之一

  3. 使用fastlane文件夹路径

    设置.env文件路径
{
plugins: [
[
"module:react-native-dotenv",
{
envName: "APP_ENV",
moduleName: "@env",
path: "fasltane/.env"  <-- set your env file path here
}
]
]
};

最新更新