前言
刚开始是按照某博客配置的,博客直接用的xcodebuild打包,证书也是通过jenkins来管理,但打包总是有问题,大概率跟证书有关,调了好长时间,不行,后续突然想起来了还有fastlane,fastlane
真牛逼,但看文档个人感觉有些累人
管理证书
fastlane提供有match
命令来轻松的管理证书,这么牛X的东西Fastlane的官方文档竟然没有单独开一个篇幅来介绍(或许是我没有找到)。哎,在这里浪费了太多的时间。附上fastlane之使用match同步证书和配置文件
使用证书
很多文档介绍了match的用法,但想找到在Fastfile如何使用,又是一关。下面附上我拼凑到的
match(
type: "adhoc", # 使用哪种证书,共四种值:development/adhoc/distribution/appstore
app_identifier: "com.xxx.xxx",
keychain_password: 'xxxx' # 不管是使用match配置和使用证书都需要密码,建议都配置成一个,好像是用于加密解密证书的一个秘钥
)
在jenkins上打包有时会碰到让设置MATCH_PASSWORD
,这个内容跟上文提到的keychain_password
一致,配置方式如下
before_all do
ENV["MATCH_PASSWORD"] = 'xxx'
end
关于MATCH_PASSWORD
,也可以直接放在环境变量中,但在jenkins环境下有时可能不管用,此时就需要在fastfile里按照上面方法配置
构建命令
构建算这里面最简单的了,直接贴上我的,更多的配置查看文档
iOS构建build app和android构建gradle,android记得在项目中的build gradle配置realease版本的签名文件
# iOS
build_app(
workspace: "./ios/xxx.xcworkspace",
configuration: "Release",
scheme: "xxx",
silent: true,
clean: true,
export_method: "ad-hoc",
output_directory: "/Users/wlg/work/xxx/dist/ios"
)
# android
gradle(
project_dir: "/Users/wlg/work/xxx/android",
gradle_path: './gradlew',
task: 'assemble',
build_type: 'debug',
print_command: true,
);
关于app托管
- 最开始使用的是jenkins的firim插件,不能用,咨询官方后,人家说已经暂停维护了
- 随后使用的是fir-cli然后在jenkins中执行fir命令,高高兴兴的用了一段时间,但突然某一天,竟然无法上传app了,头疼。附上fir文档
- 最后使用的是fastlane的firim插件来上传,但好像也不太稳定,附上文档
- 该做的都做了,再上传不了就只能手动上传了
iOS的版本自增
increment_build_number({
build_number: latest_testflight_build_number + 1,
xcodeproj: './ios/xxx.xcodeproj',
})
latest_testflight_build_number
这是获取testflight
上的最新build号,否则每次打包都要提交一下自动修改后的版本号
testflight上传
upload_to_testflight(
skip_waiting_for_build_processing: true
)
在将apk上传到testflight后,苹果会对该apk进行检验,如果不加skip_waiting_for_build_processing: true
,在苹果检验(时间不可控,可能长可能短)没有结果之前该脚本会一直在等待,无法执行后续任务。
fastlane下苹果的账号验证问题
主要是MATCH_PASSWORD
和FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD
问题
传送门
iOS缓存问题
fastlane 有个clear_derived_data
专门处理这个问题
iOS每次打包都会生成几个G的缓存文件,太痛苦了
iOS打包会开启8081服务
sh("((lsof -t -i:8081) && (kill -9 `lsof -t -i:8081`) || pwd)")
iOS每次打包都会开启RN的8081服务,第二次打包需要关闭该8081服务,就诞生了这个脚本
fastlane获取最新代码
之所以这么搞是因为我在我电脑上配置的jenkins隔山差五出问题(比如证书某天突然让你输入什么密码),心累了。就直接摆脱了jenkins,全部用fastlane搞了
sh('git fetch')
hash=sh("git rev-parse refs/remotes/origin/master^{commit}")
sh("git checkout -f #{hash}")
Fastlane完整代码
# iOS
default_platform(:ios)
platform :ios do
desc "ios adhoc"
before_all do
ENV["MATCH_PASSWORD"] = 'xxx'
ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "xxx"
end
lane :adhoc do |op|
sh("((lsof -t -i:8081) && (kill -9 `lsof -t -i:8081`) || pwd)")
sh('git fetch')
hash=sh("git rev-parse refs/remotes/origin/master^{commit}")
sh("git checkout -f #{hash}")
sh('yarn install')
cocoapods(
podfile: "./ios/Podfile"
)
# type供四种: appstore/adhoc/enterprise/development
match(type: "adhoc", app_identifier: "xxx", keychain_password: 'xxx', force_for_new_devices: true)
build_app(
workspace: "./ios/xxx.xcworkspace",
configuration: "Release",
scheme: "xxx",
silent: true,
clean: true,
export_method: "ad-hoc",
output_directory: "./dist/ios"
)
fir_cli(
api_token: "xxx",
specify_file_path: "./dist/ios/xxx.ipa",
changelog: 'adhoc',
need_release_id: true,
)
clear_derived_data
end
lane :testflight do |op|
sh("((lsof -t -i:8081) && (kill -9 `lsof -t -i:8081`) || pwd)")
sh('git fetch')
hash=sh("git rev-parse refs/remotes/origin/master^{commit}")
sh("git checkout -f #{hash}")
sh('yarn install')
cocoapods(
podfile: "./ios/Podfile"
)
# type供四种: appstore/adhoc/enterprise/development
match(type: "appstore", app_identifier: "xxx", keychain_password: 'xxx', force_for_new_devices: true)
# UI.message(latest_testflight_build_number) UI.message可以打印日志
increment_build_number({
build_number: latest_testflight_build_number + 1,
xcodeproj: './ios/xxx.xcodeproj',
})
build_app(
workspace: "./ios/xxxx.xcworkspace",
configuration: "Release",
scheme: "xxx",
silent: true,
clean: true,
export_method: 'app-store',
output_directory: "./dist/ios"
)
upload_to_testflight(
skip_waiting_for_build_processing: true
)
clear_derived_data
end
end
default_platform(:android)
platform :android do
desc "android Debug"
lane :debug do |op|
sh('git fetch')
hash=sh("git rev-parse refs/remotes/origin/master^{commit}")
sh("git checkout -f #{hash}")
sh('yarn install')
gradle(
project_dir: "./android",
gradle_path: './gradlew',
task: 'assemble',
build_type: 'Debug',
print_command: true,
);
fir_cli(
api_token: "xxx",
specify_file_path: "./android/app/build/outputs/apk/debug/app-debug.apk",
changelog: 'debug',
need_release_id: true,
)
end
lane :release do |op|
sh('git fetch')
hash=sh("git rev-parse refs/remotes/origin/master^{commit}")
sh("git checkout -f #{hash}")
sh('yarn install')
gradle(
project_dir: "./android",
gradle_path: './gradlew',
task: 'assemble',
build_type: 'Release',
print_command: true,
);
fir_cli(
api_token: "xxx",
specify_file_path: "./android/app/build/outputs/apk/release/app-release.apk",
changelog: 'release',
need_release_id: true,
)
end
end
mac上jenkins打包出现Java heap space
目前分析有如下两种原因:
-
电脑硬盘空间小只有120G,删了很多文件才腾出12个G左右的空闲空间
-
分配给jenkins的空间小,我这里使用jenkins
启动给分配的空间是2G,不够用,后来在网上找资料都是在说linux上的处理方法试验后发现在mac上都无效,后续找JAVA的同事分析说自动的时候可以指定空间,然后就有了如下这个操作
alias jenkinsJVM4G="/usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home/bin/java -jar -Xmx4096m /usr/local/Cellar/jenkins/2.251/libexec/jenkins.war"