基于Jenkins和fastlane的iOS持续集成与发布环境搭建

原文:基于Jenkins和fastlane的iOS持续集成与发布环境搭建

前言

Jenkins安装

Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。

Jenkins下载地址
选择Mac os X
下载好,一步一步安装即可。
安装成功.png

安装成功后,直接在浏览器中输入:http://localhost:8080/ ,打开jenkins;

1
2
3
http://localhost:8080/exit // 退出Jenkins
http://localhost:8080/restart // 重启Jenkins,一般装了插件重启下
http://localhost:8080/reload // 重新加载Jenkins

解锁Jenkins.png

找到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
或者如下图,执行```sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword```,将输出的密码拷贝纸页面密码区域;
![解锁Jenkins-initialAdminPassword.png](https://upload-images.jianshu.io/upload_images/949086-67a99a991002c170.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![解锁Jenkins-密码拷贝.png](https://upload-images.jianshu.io/upload_images/949086-cc407d5721a3b31a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![自定义安装.png](https://upload-images.jianshu.io/upload_images/949086-58f181439c951fe0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![下一步安装.png](https://upload-images.jianshu.io/upload_images/949086-6c58047ee1187ad6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![创建管理员.png](https://upload-images.jianshu.io/upload_images/949086-99808012363b5841.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![instace config.png](https://upload-images.jianshu.io/upload_images/949086-86d1bac2519bdd53.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![start jenkins.png](https://upload-images.jianshu.io/upload_images/949086-d69276d9a61a8cac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![安装完成.png](https://upload-images.jianshu.io/upload_images/949086-7cacaec4f6dc40c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#### 插件安装
在系统管理-->管理插件,添加如下插件:
![image.png](http://upload-images.jianshu.io/upload_images/949086-a2c63f505cbcd640.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[Xcode integration](https://wiki.jenkins-ci.org/display/JENKINS/Xcode+Plugin)
[Git plugin](http://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin)
[GitHub plugin](http://wiki.jenkins-ci.org/display/JENKINS/Github+Plugin)
[PostBuildScript Plugin](http://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin)
注意:
记得安装环境变量注入插件,然后在配置工程时,构建->增加构建步骤->引入如下路径;

PATH=$PATH:/usr/local/bin

包括pod、fastlane、git等shell的执行,都依靠这个路径

1
2
3
4
5
6
7
8
9
10
11
[Environment Injector Plugin](https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin) // 环境变量注入
![Inject environment variables.png](https://upload-images.jianshu.io/upload_images/949086-5c76f163b88f195b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![fastlane指令.png](https://upload-images.jianshu.io/upload_images/949086-c672aec0522f56de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## fastlane 安装
[fastlane工具](https://fastlane.tools)
1、Xcode 命令行安装

xcode-select –install

1
2
3
4
如果输出如下,表示已经安装
![Xcode-select.png](https://upload-images.jianshu.io/upload_images/949086-f4b1521e56153c49.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、fastlane安装

sudo gem install fastlane // 安装fastlane
fastlane -v // 查看安装版本

1
如果报错

ERROR: While executing gem … (Gem::FilePermissionError)
You don’t have write permissions for the /usr/bin directory.

1
请使用

sudo gem install -n /usr/local/bin/ fastlane

1
2
3
4
5
6
7
8
### 新建Item
![新建Item.png](http://upload-images.jianshu.io/upload_images/949086-07d2802cd1d66c63.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![create Item.png](http://upload-images.jianshu.io/upload_images/949086-e4805bb6531af2ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![iOS_test.png](http://upload-images.jianshu.io/upload_images/949086-198b277ab1f8c9b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![workspace path.png](http://upload-images.jianshu.io/upload_images/949086-731fffffa4f7b81a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

/Users/developer/.jenkins/jobs/iOS_Test/workspace/项目工程

1
2
3
4
5
6
### Jenkins通用配置
#### 注意点:
1、Keychains
获取Jenkins对钥匙串的访问权限

~/Library/Keychains/login.keychain 或者login.keychain-db

1
2
2、Provisioning Profiles
将描述文件拷贝到

~/Library/MobileDevice/Provisioning Profiles/

1
将以上文件分别拷贝到

/Users/Shared/Jenkins/Library/Keychains/

/Users/Shared/Jenkins/Library/MobileDevice/Provisioning Profiles/

1
3、证书权限

钥匙串-> 登录 -> 我的证书
找到对应证书,展开-选中私钥,双击,访问控制,允许所有应用程序访问此项目,右下角,存储更改。

1
2
3
### fastlane配置
1、进入工程主目录

fastlane init

1
2、查看Appfile文件

app_identifier(“com.XX.XX”) # The bundle identifier of your app
apple_id(“renboan@liyun.com”) # Your Apple email address

For more information about the Appfile, see:

https://docs.fastlane.tools/advanced/#appfile

1
2
3、配置Fastfile文件

This file contains the fastlane.tools configuration

You can find the documentation at https://docs.fastlane.tools

#

For a list of all available actions, check out

#

https://docs.fastlane.tools/actions

#

For a list of all available plugins, check out

#

https://docs.fastlane.tools/plugins/available-plugins

#

Uncomment the line if you want fastlane to automatically update itself

update_fastlane

平台,安卓还是iOS

default_platform(:ios)

iOS进行的操作

platform :ios do
desc “企业版”

在执行lane之前进行的操作,例如我们在每次打包之前都执行一次pod install

#before_all do

faselane中pod install的操作

#cocoapods

#end

打包的lane操作,我们可以配置多个lane来打不同环境的包

lane :enterprise do |op|

打包之前跟新指定配置的描述文件

update_project_provisioning(

之前有sigh下载的描述文件存储路径

profile:”./XXXXDis.mobileprovision”,

打包配置,Debug,Release

build_configuration:”Debug”
)

automatic_code_signing(

工程文件所在路径

path:”ProjectName.xcodeproj”,

是否使用自动签名,这里如果是打包的话应该一般都为false吧,默认也是false

use_automatic_signing:false,

打包的team ID, 也就是打包使用的证书中的team ID,这个如果不知道是什么的话可以在xCode中设置好签名用的描述文件后到xcodeproj下的pbxproj文件中搜索“DEVELOPMENT_TEAM”,它的值就是了

team_id:”XXXX”,

这个就不用说了,需要修改的targets

targets:”ProjectName”,

用哪种方式打包“iPhone Develop”还是“iPhone Distribution”

code_sign_identity:”iPhone Distribution”,

描述文件名称, 也就是使用哪个描述文件打包

profile_name:”XXXXDis”
)

currentTime = Time.new.strftime(“%Y-%m-%d-%H-%M”)
outputDirectory = “./build/#{currentTime}”
logDirectory = “#{outputDirectory}/fastlanelog”

gym(

打包方式,enterprise, adhoc,appstore,development

export_method: “enterprise”,
scheme: “ProjectName”,

pod 生成的workspace文件

workspace:”ProjectName.xcworkspace”,

输出文件夹

output_directory: outputDirectory,

输出包名称

output_name:”ProjectName.ipa”,

打包前是否clean

clean:true,
silent:true,

打包的配置 Debug Release

configuration:”Debug”,

打包日志输出文件夹

buildlog_path:logDirectory,

打包证书

codesigning_identity:’iPhone Distribution: XXXXXXXXXXXX TECHNOLOGY CO.,LTD.’,

Xcode 9 默认不允许访问钥匙串的内容,必须要设置此项才可以,运行过程可能会提示是否允许访问钥匙串,需要输入电脑密码

export_xcargs: “-allowProvisioningUpdates”,

导出选项

export_options:{

打包导出时可选描述文件 “bundleID”=>”描述文件名称”

provisioningProfiles: {
“com.XXXX.XXXX” => “XXXXDis.mobileprovision”,
},
}
)
archive_path=”Published succeed:/XXXXXXXXXXXX/build/#{currentTime}/ProjectName.ipa”
puts “#{archive_path}”
end

当lane执行完成之后进行哪些操作

after_all do |lane|

end

error do |lane, exception|
end

end

1
2
4、命令行执行打包

fastlane ios enterprise

1
2
3
4
### Jenkins配置
##### 1、shell语句

export LANG=”en_US.UTF-8”
pwd
cd $WORKSPACE/ProjectName

fastlane ios enterprise

1
2
3
##### 2、Set build description
![Set build description.png](https://upload-images.jianshu.io/upload_images/949086-7a7ac164fd367fb3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Published succeed:(.*)

单击ipa包下载

1
Regular expression:Published succeed:(.*) // 这里这样写,是因为fastlane文件中输出了以Published succeed:为前缀的一个地址;

archive_path=”Published succeed:/XXXXXXXXXXXX/build/#{currentTime}/ProjectName.ipa”
puts “#{archive_path}”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Description:构建后的描述中的```\1```的意思是获取正则表达式获取的内容。
##### 3、邮件通知
首先将系统管理员邮箱(你用来发邮件的邮箱)smtp服务打开,拿126邮箱举例,打开邮箱,进入设置,进入POP3/SMTP/IMAP设置,打开如下图几个选项。
![image.png](https://upload-images.jianshu.io/upload_images/949086-af0ed2f213f7c230.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后,在Jenkins,添加系统管理员邮箱及SMTP服务器,具体步骤是,进入Jenkins的系统管理-->系统设置,找到Jenkins Location,填写系统管理员邮件地址;
![image.png](https://upload-images.jianshu.io/upload_images/949086-25c47d155c2ead19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加SMTP服务器地址,126邮箱的是smtp.126.com,填写管理员邮箱及密码。
![image.png](https://upload-images.jianshu.io/upload_images/949086-972342ab6dad517d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后,在项目配置中,构建后操作项,添加收取邮件的邮箱,多个邮箱用空格隔开。
![image.png](https://upload-images.jianshu.io/upload_images/949086-838b145fcc58e12a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
设置完成后,在构建历史区域可以直接下载打包的ipa包。
![image.png](https://upload-images.jianshu.io/upload_images/949086-aa38f2a70ea40cfa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- Jenkins启动

sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist

1
- 停止运行Jenkins

sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

1
2
3
4
- 找回密码
找到config.xml文件

/Users/Shared/Jenkins/Home/users/admin/config.xml

1
文件中找到,fullName为用户名:```admin```,passwordHash为加密的密码:```111111

1
2
<fullName>admin</fullName>
<passwordHash>#jbcrypt:$2a$10$DdaWzN64JgUtLdvxWIflcuQu2fgrrMSAMabF5TSrGK5nXitqK9ZMS</passwordHash>

本文已在版权印备案,如需转载请在版权印获取授权。
获取版权