配置 fastlane 框架自动化打包发布 iOS 应用

fastlane/fastlane

安装RVM

curl -sSL https://get.rvm.io | bash -s stable --ruby

安装 RVM 是需要管理多版本的 Ruby 环境

安装fastlane

gem install fastlane --verbose

fastlane初始化

在项目目录下运行

fastlane init

命令运行时需要输入Apple ID, 运行成功后会生成 fastlane/Fastlane 文件, Fastlane 是一个 ruby 脚本

使用 bundler 管理依赖

官网介绍
bundler 用来管理 fastlane 自身版本和 fastlane 运行时的相关依赖版本, 相当于 iOS 开发中的 CocoaPods 框架, 使用方法也和 CocoaPods 如出一辙

  • 安装 bundler
sudo gem install bundler
  • 在项目根目录下新建 Gemfile 文件并写入

    source "https://gems.ruby-china.org"
    
    gem "fastlane"
    gem "cocoapods"
    
  • 安装依赖库, 生成 Gemfile.lock 文件, 这个文件和我们平常接触的 Podfile.lock 文件功能一致, 配置 CI 时也需要在每次构建前调用该命令

    [sudo] bundle install
    
  • 更新 Gemfile.lock 文件

[sudo] bundle update
  • 运行 fastlane 框架
bundle exec fastlane [lane]

安装match

github官网介绍
match 是 fastlane 的一个功能组件, 采取了集中化方式来管理证书和 profile, 新建一个私有远程 git 库用来保存证书和 profile, 一个 team 的开发者共用同一套证书, 方便了管理和配置, 同时 match 在证书过期时还会自动从苹果官网下载新的证书并 push 到私有的 git 库中, 保证证书同步, 不得不为这个想法点赞!

gem install match
xcode-select --install

match初始化

match init

命令运行时需要输入新建的用来保存证书的git地址, 运行成功后会生成 Matchfile 配置文件

.
└── fastlane
    ├── Appfile
    ├── Fastfile
    ├── Matchfile
    └── README.md

接着运行下面的两条命令来生成证书和 profile

match appstore
match development

命令运行时需要输入Apple ID 和 密码, 该密码保存在系统的 keychain 中

如果不希望用 match 重新生成证书和 profile, 可以参考Michał Laskowski的博客

如果当前项目的证书和 profile 比较混乱, 可以用一下两个命令来清空 Apple 官网上当前全部证书和 profile 文件

match nuke development
match nuke distribution

match 扩展

如果 Team 来了新成员, 在 clone 了项目工程后运行下面的命令后, match 就从私有证书库中下载develop 证书和 profile, 并安装到新机上, 新成员就可以调试代码了

match development --readonly

Jenkins 配置

jekins.png

遇到的问题

  • 运行 match appstore 时遇到 openssl 库版本问题, 更新 openssl 库
AfluyFileSystem:Remote afluy$ match appstore
[19:53:01]: Successfully loaded '/Users/afluy/WorkCodeIOS/Remote/fastlane/Matchfile' 📄
....
[19:53:01]: Cloning remote git repo...
/Library/Ruby/Gems/2.0.0/gems/fastlane_core-0.53.0/lib/fastlane_core/command_executor.rb:46: warning: Insecure world writable dir /Users/afluy/Applications/dex2jar-2.0 in PATH, mode 040777
[19:53:02]: 🔓  Successfully decrypted certificates repo
[19:53:02]: Verifying that the certificate and profile are still valid on the Dev Portal...
[19:53:20]: 🔒  Successfully encrypted certificates repo
[19:53:20]: -----------------------------------------------------------------------
[19:53:20]: Connection reset by peer - SSL_connect
[19:53:20]:
[19:53:20]: SSL errors can be caused by various components on your local machine.
[19:53:20]: Apple has recently changed their servers to require TLS 1.2, which may
[19:53:20]: not be available to your system installed Ruby (2.0.0)
[19:53:20]:
[19:53:20]: The best solution is to install a new version of Ruby
[19:53:20]:
[19:53:20]: - Make sure OpenSSL is installed with Homebrew: `brew update && brew upgrade openssl`
[19:53:20]: - If you use system Ruby:
[19:53:20]:   - Run `brew update && brew install ruby`
[19:53:20]: - If you use rbenv with ruby-build:
[19:53:20]:   - Run `brew update && brew upgrade ruby-build && rbenv install ruby-2.3.1`
[19:53:20]:   - Run `rbenv global ruby-2.3.1` to make it the new global default Ruby version
[19:53:20]: - If you use rvm:
[19:53:20]:   - First run `rvm osx-ssl-certs update all`
[19:53:20]:   - Then run `rvm reinstall ruby-2.3.1 --with-openssl-dir=/usr/local
[19:53:20]:
[19:53:20]: If that doesn't fix your issue, please google for the following error message:
[19:53:20]:   'Connection reset by peer - SSL_connect'
[19:53:20]: -----------------------------------------------------------------------

[!] Connection reset by peer - SSL_connect
  • 运行 match development 时遇到 Apple 账号权限问题, 请使用可以上传 profile 的账号
AfluyFileSystem:Remote afluy$ match development
[20:35:02]: Successfully loaded '/Users/afluy/WorkCodeIOS/Remote/fastlane/Matchfile' 📄
...
[20:35:02]: Cloning remote git repo...
[20:35:03]: 🔓  Successfully decrypted certificates repo
[20:35:03]: Verifying that the certificate and profile are still valid on the Dev Portal...
[20:35:17]: Couldn't find a valid code signing identity in the git repo for development... creating one for you now
...
[20:35:17]: Starting login with user 'zhao.xianhua@whaley.cn'
[20:35:22]: Successfully logged in
[20:35:25]: $ security import /var/folders/_n/kyvgh8495bv5zmmdg7f85_1r0000gn/T/d20161027-57678-otquli/certs/development/K2935G9MQS.p12 -k '/Users/afluy/Library/Keychains/login.keychain' -T /usr/bin/codesign -T /usr/bin/security
[20:35:25]: ▸ 1 key imported.
[20:35:25]: $ security import /var/folders/_n/kyvgh8495bv5zmmdg7f85_1r0000gn/T/d20161027-57678-otquli/certs/development/K2935G9MQS.cer -k '/Users/afluy/Library/Keychains/login.keychain' -T /usr/bin/codesign -T /usr/bin/security
[20:35:25]: ▸ 1 certificate imported.
[20:35:25]: Successfully generated K2935G9MQS which was imported to the local machine.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1062  100  1062    0     0    825      0  0:00:01  0:00:01 --:--:--   825
[20:35:28]: Verifying the certificate is properly installed locally...
[20:35:28]: Successfully installed certificate K2935G9MQS

...

[20:35:29]: Starting login with user '...'
[20:35:31]: Successfully logged in
[20:35:31]: Fetching profiles...
[20:35:31]: No existing profiles found, that match the certificates you have installed locally! Creating a new provisioning profile for you
[20:35:35]: Creating new provisioning profile for '...' with name 'match Development ...'
[20:35:38]: An error occured while verifying your certificates and profiles with the Apple Developer Portal.
[20:35:38]: If you already have your certificates stored in git, you can run `match` in readonly mode
[20:35:38]: to just install the certificates and profiles without accessing the Dev Portal.
[20:35:38]: To do so, just pass `readonly: true` to your match call.
[20:35:38]: 🔒  Successfully encrypted certificates repo

Looking for related GitHub issues on fastlane/fastlane...

Found no similar issues. To create a new issue, please visit:
https://github.com/fastlane/fastlane/issues/new

[!] Apple provided the following error info:
    You are not permitted to create provisioning profiles for team.  Please contact one of your team admins, who can create a profile on your behalf.
  • Apple 官网和本地机器上已经有证书, 需要删除官网和本地的证书, 也可以运行上面说的 match nuke 命令来清空已有的证书
AfluyFileSystem:Remote afluy$ match development
[10:53:48]: Successfully loaded '/Users/afluy/WorkCodeIOS/Remote/fastlane/Matchfile' 📄
...
[10:53:48]: Cloning remote git repo...
[10:53:54]: 🔓  Successfully decrypted certificates repo
[10:53:54]: Verifying that the certificate and profile are still valid on the Dev Portal...
[10:54:00]: Couldn't find a valid code signing identity in the git repo for development... creating one for you now
...
[10:54:00]: Starting login with user 'zhao.xianhua@whaley.cn'
[10:54:02]: Successfully logged in
[10:54:05]: 🔒  Successfully encrypted certificates repo

Looking for related GitHub issues on fastlane/fastlane...

➡️  match: Could not create another certificate, reached the maximum number of available certificates.
   https://github.com/fastlane/fastlane/issues/5765 [closed] 4 💬
   17 Aug 2016

➡️  Could not create another certificate, reached the maximum number of available certificates.
   https://github.com/fastlane/fastlane/issues/3472 [closed] 9 💬
   5 weeks ago

➡️  Could not create another certificate, reached the maximum number of available certificates
   https://github.com/fastlane/fastlane/issues/3123 [closed] 10 💬
   5 weeks ago

and 10 more at: https://github.com/fastlane/fastlane/search?q=Could%20not%20create%20another%20Development%20certificate,%20reached%20the%20maximum%20number%20of%20available%20Development%20certificates.&type=Issues&utf8=✓

[!] Could not create another Development certificate, reached the maximum number of available Development certificates.
  • Team 中其他开发者需要在 Xcode 中选择 Automatically manage signing
  • 在 Setting up CocoaPods master repo 卡住, 这个是 CocoaPods 需要全部 clone 下来, 我最终下载了大概 700M 的文件~~
  • 如果不能将改动提交到私有的存储证书的远程 git 库, 可以检查该 git 库的master分支是否被保护

参考资料

https://codesigning.guide/
https://github.com/fastlane/fastlane
https://icyleaf.com/2016/07/intro-fastlane-automation-for-ios-and-android/
https://rvm.io/rvm/install
https://ruby-china.org/wiki/rvm-guide
https://ruby-china.org/wiki/install_ruby_guide
http://macoscope.com/blog/simplify-your-life-with-fastlane-match/#main
http://stackoverflow.com/questions/36963467/how-do-i-manually-add-existing-provisioning-profiles-and-certificates-to-fastlan
http://macoscope.com/blog/simplify-your-life-with-fastlane-match/#migration

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容