Flutter iOS项目基于Fastlane与GitHub Actions的版本同步问题
解决Fastlane Build号同步到pubspec.yaml及GitHub Actions提交变更问题
一、将Fastlane递增的Build号同步到pubspec.yaml
无需依赖外部Perl脚本,直接在Fastlane的lane中完成Build号获取和pubspec.yaml修改——利用Fastlane自带的返回值捕获能力,结合Ruby原生文件操作即可实现:
- 在
Fastfile中定义或修改lane:
lane :sync_build_number do # 执行Xcode Build号递增,同时捕获新生成的Build号 new_build = increment_build_number.return_value # 定位并修改pubspec.yaml中的build_number字段 pubspec_file = File.join(Dir.pwd, "pubspec.yaml") pubspec_content = File.read(pubspec_file) pubspec_content.gsub!(/build_number:\s*\d+/, "build_number: #{new_build}") File.write(pubspec_file, pubspec_content) # 若需同步version号,可同理处理: # new_version = increment_version_number.return_value # pubspec_content.gsub!(/version:\s*\d+\.\d+\.\d+/, "version: #{new_version}+#{new_build}") end
执行fastlane sync_build_number时,会自动完成Xcode Build号递增和pubspec.yaml的同步,无需额外传递参数。
二、GitHub Actions中同步文件变更回仓库
在GitHub Actions的Workflow中添加提交推送步骤,确保修改后的pubspec.yaml和Xcode项目文件同步回主分支:
- 先在Fastlane lane中把新Build号输出到GitHub环境变量(用于提交信息):
在上述lane末尾追加:
sh("echo \"NEW_BUILD_NUMBER=#{new_build}\" >> $GITHUB_ENV")
- 在Workflow文件(如
.github/workflows/build.yml)中添加提交步骤:
- name: Commit and Push Build Number Changes run: | # 配置git用户身份 git config --global user.name "GitHub Actions Bot" git config --global user.email "actions@github.com" # 添加变更文件 git add pubspec.yaml ios/Runner.xcodeproj/project.pbxproj # 提交并推送 git commit -m "chore: increment build number to ${{ env.NEW_BUILD_NUMBER }}" git push origin main
本地仓库同步说明
如果是本地执行Fastlane需要同步到仓库,直接在lane中添加git命令即可:
sh("git add pubspec.yaml ios/Runner.xcodeproj/project.pbxproj") sh("git commit -m \"chore: increment build number to #{new_build}\"") sh("git push origin main")
内容的提问来源于stack exchange,提问作者Hassan Chmsdn
相关产品推荐
相关产品推荐

