身为一名iOS开发人员,面临必不可少的一个阶段就是打包测试。虽然xcode可有实现打包,但在测试阶段面临测试人员不断的向你催促须要新的包进行测试就会很让人头疼,打包又是一个重复性极高又很费时间的事情。因此研究了一下jenkins和fastlane的自动打包流程,但愿对你们有所帮助,途中也遇到了众多的问题和坑。html
如需帮助可直接 联系我java
Ruby:linux
fastlane的安装须要Ruby2.0版本以上,虽然mac自带ruby,可是版本可能较低,而且权限较少,因此推荐使用RVM管理ruby版本。ios
按照这篇文章安装既能够了 Mac安装Ruby版本管理器RVMgit
xcode命令行工具github
xcode-select --install
复制代码
若是已经安装会提示 docker
若是没有安装会出现 shell
直接点击安装就能够了,安装完成后再次执行 xcode-select --install
以后出现已经安装的提示了。swift
Java JDK (这个是Jenkins安装用的,若是不须要jenkins可直接忽略)windows
可先查看 当前安装的java版本
java -version
复制代码
若是未安装请先前往 JDK下载连接 下载
由于jenkins是用java语言编写的,因此须要java的支持,建议使用1.8版本的JDK
须要先接受协议以后才能够下载
下载完成以后直接安装就能够了,这里就再也不赘述。
前期准备工做到这里就完成了
fastlane是为iOS和Android应用程序自动化beta部署和发布最简单的方法。🚀能够处理各类繁琐任务,例如生成屏幕截图,处理代码签名和发布应用该程序等.fastlane整合了一系列移动端开发中签名,编译,发布等工具,堪称打包神器。
Homebrew
brew cask install fastlane
复制代码
安装程序脚本
下载zip文件。后双击shell脚本便可
RubyGems
gem install fastlane -NV
复制代码
这里咱们在最后使用了 Jenkins上传蒲公英,为了获取下载二维码。
本文也提到了使用Fastlane上传蒲公英的介绍
Fastlane 安装成功以后 执行安装蒲公英的插件
fastlane add_plugin pgyer
复制代码
若是没什么意外就直接成功了
刚开始我使用的本机自带的ruby, 失败了。而后使用了rvm管理的ruby以后就成功了。
若是遇到了其余问题请你们google一下吧,或者联系我咱们一块儿探讨。
cd /到本身的项目目录中
复制代码
初始化fastlane
fastlane init
复制代码
若是以前的都没有问题 会出现如下选项

复制代码
这里咱们选的是第3个,而后会出现一些选项
1. 📸 Automate screenshots
2. 👩✈️ Automate beta distribution to TestFlight
3. 🚀 Automate App Store distribution
4. 🛠 Manual setup - manually setup your project to automate your tasks
? 3
[10:37:53]: ----------------------------------------------------------
[10:37:53]: --- Setting up fastlane for iOS App Store distribution ---
[10:37:53]: ----------------------------------------------------------
[10:37:53]: Parsing your local Xcode project to find the available schemes and the app identifier
[10:37:53]: $ xcodebuild -showBuildSettings -workspace FastlaneDev.xcworkspace -scheme FastlaneDev
[10:37:55]: $ cd `你项目的路径` && agvtool what-version -terse
[10:37:55]: --------------------------------
[10:37:55]: --- Login with your Apple ID ---
[10:37:55]: --------------------------------
[10:37:55]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
[10:37:55]: we will ask you for your Apple ID username and password
[10:37:55]: This is necessary for certain fastlane features, for example:
[10:37:55]:
[10:37:55]: - Create and manage your provisioning profiles on the Developer Portal
[10:37:55]: - Upload and manage TestFlight and App Store builds on App Store Connect
[10:37:55]: - Manage your App Store Connect app metadata and screenshots
[10:37:55]:
[10:37:55]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
[10:37:55]: For more information, check out
[10:37:55]: https://github.com/fastlane/fastlane/tree/master/credentials_manager
[10:37:55]:
[10:37:55]: Please enter your Apple ID developer credentials
[10:37:55]: Apple ID Username:
`你的Apple ID`
[10:38:16]: Logging in...
复制代码

复制代码
若是你帐号下有多个team会让你选择一下
而后等待安装
在这期间我遇到了bundle update这句话一直不动,我从新init了一遍就行了
成功以后 打开咱们的项目目录发现多了这个文件
恭喜你 初始化成功了 !!!
文件名 | 描述 |
---|---|
Appfile | 从 Apple Developer Portal 获取和项目相关的信息 详情 |
Fastfile | 核心文件,存放lane任务 |
Deliverfile | deliver的配置文件,从 iTunes Connect 获取和项目相关的信息详细 |
metadata | 同步iTC中的元数据 |
screenshots | 同步iTC中的截图 |
这里给打家推荐一篇配置的文章Fastfile参考配置
下面是个人配置仅供参考,没有用fastlane上传蒲公英,由于想要在jenkins里面获取二维码暂时的解决办法使用jenkins上传蒲公英 若是须要fastlane上传直接打开的注释进行了
# 导出路径
$ipa_output_directory = "/Users/xx/xx/fastlaneBuild"
# 上传蒲公英的包 路径
$ipa_output_upload_directory = "/Users/xx/.jenkins/workspace/xx/ipa"
# jenkins匹配的包名
$jenkins_name = "项目名"
default_platform(:ios)
# 应用名词
scheme_name = "项目名字"
# 获取version
version = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleShortVersionString")
# 获取build版本号
build = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleVersion")
# 指定xcode打包 这里可选择不要,咱们的项目须要用10.1版本xcode
xcode_select("/Applications/Xcode_10.1.app")
# 默认内测打包方式,目前支持app-store, package, ad-hoc, enterprise, development
ipa_exportMethod = "ad-hoc"
ipa_exportMethod_appStore = "app-store"
# 计算buildNumber
def updateProjectBuildNumber
currentTime = Time.new.strftime("%Y%m%d")
scheme_name = "项目名字"
build = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleVersion").to_s
if build.include?"#{currentTime}."
# 为当天版本 计算迭代版本号
lastStr = build[build.length-2..build.length-1]
lastNum = lastStr.to_i
lastNum = lastNum + 1
lastStr = lastNum.to_s
if lastNum < 10
lastStr = lastStr.insert(0,"0")
end
build = "#{currentTime}.#{lastStr}"
else
# 非当天版本 build 号重置
build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# 更改项目 build 号
increment_build_number(
build_number: "#{build}"
)
end
# 打包成功 jenkins 开始提示上传log
# 使用fastlane上传蒲公英拿不到二维码展现, 因此使用jenkins上传蒲公英
def archiveSuccessLog(outputName,buildConfig)
# 打好的包 移动到 jenkins的 workspace的目录下,用于jenkins上传蒲公英匹配包内容。永远只保留一份
# FileUtils.cp_r("#{$ipa_output_directory}" + "/#{outputName}", "#{$ipa_output_upload_directory}" + "/#{$jenkins_name}.ipa")
puts" --------------------------------------------------------------------------"
puts"| |"
puts"| |"
puts"| 🎉🎉🎉 --------->>>>> #{buildConfig}版本_打包成功 <<<<<-----------🎉🎉🎉 |"
puts"| |"
puts"| 🎉🎉🎉 --------->>>>> Jenkins 能够开始上传蒲公英了 <<<<<-----------🎉🎉🎉 |"
puts"| |"
puts"| |"
puts" --------------------------------------------------------------------------"
end
#
# 全部任务脚本
#
platform :ios do
#
# *************| 上传到测试版本到蒲公英_测试包 |*************
#
desc "上传到测试版本到蒲公英_测试包"
lane :topgyer_debug do|option|
puts "*************| 开始打包.ipa文件... |*************"
# 自动增长build
updateProjectBuildNumber
# 自动生成证书
cert
# 导出名称
output_name = "#{scheme_name}_#{version}_#{build}_#{option[:desc]}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(
# 指定输出的ipa名称
output_name: "#{output_name}",
# 指定项目的scheme
scheme: "xxx",
# 是否清空之前的编译信息 true:是
clean: true,
export_method: "#{ipa_exportMethod}",
export_options: {iCloudContainerEnvironment: 'Development'},
# 指定打包方式,Release 或者 Debug 按需修改
configuration:"Debug",
# 包导出的位置
output_directory: "#{$ipa_output_directory}",
)
# 使用fastlane上传蒲公英拿不到二维码展现, 因此使用jenkins上传蒲公英
archiveSuccessLog(output_name," Debug ")
# 这里是 fastlane 直接上传蒲公英
# 若是须要请打开一下注释,并注释掉archiveSuccessLog(output_name," Debug ")
# puts "*************| 开始上传__测试版__本到蒲公英... |*************"
# 配置蒲公英 api_key 和 user_key
# pgyer(api_key: "你的api_key", user_key: "你的user_key", update_description: "#{option[:desc]}")
# puts "*************| 上传__测试版__到蒲公英🎉🎉🎉成功🎉🎉🎉 |*************"
end
#
# *************| 上传App Store |*************
#
lane :toappstore do
project_identifier = ""
puts "*************| 开始上传AppStore... |*************"
# 自动增长build
updateProjectBuildNumber
# 自动生成证书
# cert
# 导出名称
codesigning_identity = "证书名字"
output_name = "#{scheme_name}_#{version}_#{build}_#{ipa_exportMethod_appStore}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(
# 指定输出的ipa名称
output_name:output_name,
# 指定项目的scheme
scheme:"xxx",
# 是否清空之前的编译信息 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
export_method:"app-store",
# 上传的环境
export_options: {iCloudContainerEnvironment: 'Production'},
# 指定输出文件夹
output_directory:"#{$ipa_output_directory}",
)
deliver(
# 选择跳过图片和元数据上传,本身去配置
skip_screenshots:true,
skip_metadata:true,
# 上传完成后提交新版本进行审查
submit_for_review: false,
force:true,
)
puts "*************| 上传AppStore成功🎉 |*************"
puts "*************| 开始上传__测试版__本到蒲公英... |*************"
# 配置蒲公英 api_key 和 user_key
pgyer(api_key: "你的api_key", user_key: "你的user_key", update_description: "#{option[:desc]}")
puts "*************| 上传__测试版__到蒲公英🎉🎉🎉成功🎉🎉🎉 |*************"
# 发布testflight测试
# pilot
end
end
复制代码
在本身的项目目录中执行
fastlane topgyer_debug desc:测试
复制代码
这句话根据本身的lane :topgyer_debug do|option|
修改
成功以后能够看见 , 若是有上传蒲公英给的操做 则会告知蒲公英上传成功,前往蒲公英查看就有新版本了。
17:58:59: --------------------------------------------------------------------------
17:58:59: | |
17:58:59: | |
17:58:59: | 🎉🎉🎉 --------->>>>> Debug 版本_打包成功 <<<<<-----------🎉🎉🎉 |
17:58:59: | |
17:58:59: | 🎉🎉🎉 --------->>>>> Jenkins 能够开始上传蒲公英了 <<<<<-----------🎉🎉🎉 |
17:58:59: | |
17:58:59: | |
17:58:59: --------------------------------------------------------------------------
复制代码
接下来思考的问题是: 不能仍是由咱们开发人员执行 脚本任务来打包上传蒲公英。
有没有办法可以让测试人员本身去打包呢? 因此接下来咱们要作的就是 让jenkins执行咱们的fastlane脚本
而且自动生成二维码展现。测试人员须要测试包的时候开发人员只须要把代码上传到对应的分支就行了。
我选择了一台专门的机器做为服务器 来作这件事情, 毕竟在本身电脑上多少会有点影响的。
Jenkins是一个独立的开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工做,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。前身是Hudson是一个可扩展的持续集成引擎。可用于自动化各类任务,如构建,测试和部署软件。Jenkins能够经过本机系统包Docker安装,甚至能够经过安装Java Runtime Environment的任何机器独立运行。
前面咱们已经安装了 java JDK 若未安装请前往上方查看安装步骤
安装方式
Jenkins官网地址:jenkins-ci.org/ 下载最新的war包双击安装
使用brew安装
// 使用brew安装
brew install jenkins
// 安装完成后启动,直接运行jenkins便可启动服务
jenkins
复制代码
默认访问http://localhost:8080/, 可进入jenkins配置页面。 不要关闭终端不然jenkins会停掉
第一次运行的时候会出现如下界面
根据提示的路径去 将密码输入进去 而后点击 Continue
会进入:

复制代码
选择第一个就能够了

复制代码
等待安装完成

复制代码
本身设置帐户密码
在主页选择 manage Jenkins --> Manage Plugins 去下载插件
在这个选项中下载须要的插件,一些基本插件在安装jenkins的时候都已经下好了,如今只须要下载须要的就能够了
```ruby
GitLab
Xcode integration // 这个其实能够不用
Keychains and Provisioning Profiles Management // 配置文件管理
/// 上面这三个 若是须要在jenkins中打包 证书添加须要导入
Upload to pgyer // 上传蒲公英
Build Name and Description Setter // 设置打好包的名字
description setter // 设置图片
```
复制代码
插件安装好了以后,开始建立项目
回到主页点击新建Item

`选择: 自由风格的项目`

General --> Discard old builds 构建的天数的保持最大的个数。

General --> This project is parameterized --> 添加参数 --> Choice Parameter
这个就是 你在构建的时候能够先择 构建环境和分支,(按需添加!)

```ruby
名称: BUILD_TYPE
选项: debug
release
描述: 打包环境
----------------------------------------------
名称: GIT_BRANCH
选项: develop
master
3.1.0
3.0.0
2.9.1
2.9.0
描述: 拉取代码的 git分支
```
源码管理 --> Git
使用Git管理源码 我这里用的是gitlab 的SSH方式管理

```ruby
*/${GIT_BRANCH}
```
git管理就是为了让jenkins知道咱们的源码在哪里
在第一次构建的时候他回去拉取代码放到.jenkins的workspace中进行管理
填写完项目地址,咱们点击添加按钮。(branch Specifier 不要忘记修改 这里上面设置的选择参数,拉取代码的分支)
点击添加按钮

密钥获取: 首先咱们要保证 在git中配置了本身的秘钥。配置方法 --> [Mac 配置ssh秘钥](https://www.jianshu.com/p/17fa8d34beb2)
- 类型SSH
- userName 就是git中的名字 能知道是谁上传下载的就能够
- PrivateKey ssh中的私钥,/Users/用户名/.ssh/id_rsa
- Passphrase git密码。
- 点击添加 既能够了。
若是你的项目很大,须要的clone时间较长的话,须要设置一下超时时间.
jenkins默认的超时时间是10分钟,根据本身项目设置时间,
这里有一个[git clone 太慢解决方案](git clone 太慢)。


构建环境 --> Set build Name 设置打出来的包的展现名字 ,若是没有添加插件 是没有这个选项的

```ruby
iOS_XXX_${GIT_BRANCH}_${BUILD_TYPE}_build(${BUILD_NUMBER})
```
构建 --> 增长构建步骤 --> Execute shell
复制代码
fastlane topgyer_debug desc:测试版_Debug
```
而后 保存退出就能够了
回到首页 --> build with paramters --> 选择环境和分支,开始构建


第一次构建须要从Git拉去代码 若是超时了 更改一下 超时时间 上面有介绍
若是提示 fastlaneCommand not found 点击此连接 [jenkins执行shell脚本时候出现 fastlaneCommand not found解决方案](jenkins执行shell脚本时候出现 fastlaneCommand not found)
**静候佳音 Jenkins打包操做 搞定了!!!**
打包成功了以后发现 每次仍是要点击去蒲公英的连接而后才能下载
经研究了一下怎么直接把蒲公英的连接放在构建挖成以后
如: 酱事儿~

复制代码
最开始我使用的是 fastlane 上传蒲公英的操做,可是想要拿到二维码不是很容易
最后想到了直接让 jenkins哪去fastlane打的包而后上传蒲公英就行了,而后成功了!
下面介绍一下操做步骤
上传蒲公英
仍是进入到咱们当前项目的配置里面去 --> 到最后的构建后操做 --> 增长构建后操做步骤
pgyer api_key : 你的蒲公英api_key
// 这个位置本身决定就行了,和fastfile文件中的ipa_output_upload_directory地址 是同样就行
Scandir: 打的api包的地址
file wildcard: 包名字.ipa
注意你须要 吧 fastlane中的fastfile文件中
###### 这句话的注释打开 #####
FileUtils.cp_r("#{$ipa_output_directory}" + "/#{outputName}", "#{$ipa_output_upload_directory}" + "/#{$jenkins_name}.ipa")
这个是讲打出来的包拷贝一份到 workspace中,保证ipa文件夹的包只有一个最新的。这个包用于上传蒲公英
复制代码
获取二维码操做:
构建后操做 --> 增长构建后操做步骤 -->Set build description
直接拷贝
<a href="${appBuildURL}"><img src="${appQRCodeURL}" width="118" height="118"/></a>
从上传蒲公英的信息中获取图片
复制代码
最后一步 回到首页 --> manager Jenkins --> Configure Floable Security
OK!! 大功告成!!!去首页去 构建吧! !!
介绍了一下 fastlane 和 Jenkins 的结合使用的过程。比较流水线,若是哪里不对 还望各位指出,
而后咱们拿到本身电脑的ip http://电脑ip:8080/ 交给测试就行了 要包本身去点构建把,
建议使用网线,这样ip不容易变。
以前也研究过 所有使用Jenkins实现打包上传等过程, 可是jenkins须要配置的东西真的不少,证书、xcode、一系列操做,配置错误就会失败 并且还不容易找出问题,因此选择了fastlane进行打包上传蒲公英,以后为了获取到二维码直接使用选择了 jenkins上传蒲公英。
暂时的解决方法 若是有更好的方法 但愿你们多多指出,我也会继续研究更好的方法。
感谢!
若是你在输入pod命令的时候出现如下错误
/Library/Ruby/Site/2.3.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException) from /Library/Ruby/Site/2.3.0/rubygems.rb:308:in `activate_bin_path' from /usr/local/bin/pod:23:in `<main>'
复制代码
缘由1:
缘由2:
若是肯定了都没有问题 那么执行一下命令
gem install cocoapods
复制代码
pod install
复制代码
以后会发现 pod 已经能够正常使用了
查找域名所对应的ip 地址,并修改host文件
获取 github/gitlab.global.ssl.fastly.Net的ip地址
nslookup github.global.ssl.fastly.Net
复制代码
获取github.com 的ip地址
nslookup github.com
复制代码
在etc/hosts 文件中添加
151.101.76.249 github.global.ssl.fastly.net
192.30.253.112 github.com
复制代码
强制更新DNS缓存
windows
ipconfig /flushdns
复制代码
linux
sudo /etc/init.d/networking restart
复制代码
mac
sudo killall -HUP mDNSResponder
复制代码
在安装完fastkane以后执行 fastlane xxx desc:xxx 能够成功打包执行 在jenkins中的shell脚本没法执行出现如下错误
16:20:30 xxx/xxx/xxx/xxx.sh: line 4: fastlane: command not found
16:20:30 Build step 'Execute shell' marked build as failure
16:20:30 New run name is 'iOS_xxx_develop_debug_build(17)'
16:20:30 [UPLOAD TO PGYER] - The build FAILURE, so the file was not uploaded.
16:20:30 [description-setter] Description set:
16:20:30 Finished: FAILURE
复制代码
解决方法:
在jenkins的shell脚本命令前添加 #!/bin/bash -ilex
命令 eg:
#!/bin/bash -ilex
cd /Users/xxxx/xxxx # 你的项目地址
fastlane topgyer_debug desc:测试版_Debug
复制代码
**再去执行构建就能够了。 **
复制代码
参考:
再次感谢!!