Jenkins 打包构建及自动化测试初探

目标:

提升工作效率、人效,将开发人员从打包任务中解放出来,开开心心的打豆豆。

流程:

来个图,清晰明了

正文开始:

1、        安装

有两种安装方式


1.1、homebrew方式安装


安装homebrew命令:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


Jenkins安装:

$ brew install Jenkins


jenkins启动

$ jenkins


1.2、下载安装包进行安装

官网地址:https://jenkins.io/download/

根据系统选择下载相应的版本



以上安装方式,安装成功后,自动打开浏览器

http://localhost:8080/


进行安装配置


2、配置



执行命令:

sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword

获取密码


注意:


插件安装建议使用推荐选项即可


2.1、插件清单

项目采用gitlab管理,fir上传,需要的插件清单如下(参考):


gitLab Plugin

gitLab Hook Plugin

Xcode integration

keychains and provisioning profiles Management

CocoaPods Jenkins Integration

build Timeout

description setter plugin

Email Extension Plugin

SSH Agent Plugin

workSpace Cleanup Plugin

fir-Plugin


2.2、插件安装失败处理

Jenkins界面-à点击系统管理

上图是安装失败的插件,在管理插件里面搜索、安装即可。


3、创建新任务

3.1、新建


3.2、源码管理

需要配置SSH获取访问权限


配置方法:


说明:配置SSH需要在git上和Jenkins都要配置.因为Jenkins系统使用的是Jenkins这个系统账号,所以需要使用jenkins账户这个SSH.


切到jenkins账号生成新的ssh密钥,公钥在上传的git服务器就好了


打开终端名为jenkins的用户

sudo dscl . passwd /Users/Jenkins


设置密码:


切换到jenkins用户

su jenkins


以jenkins用户身份生成ssh key

ssh-keygen -t rsa -C "你的邮箱标识"


把公共密钥放到git服务器, jenkins 私钥,jenkins.pub 公钥


获取公钥

$ cat /Users/Shared/Jenkins/.ssh/id_rsa.pub


将私钥放到jenkins中进行创建Credentials

$cat /Users/Shared/Jenkins/.ssh/id_rsa

配置完毕,执行构建即可


4、组件化相关配置

4.1、更新ruby 源

gem sources -r https://rubygems.org/

gem sources -a https://ruby.taobao.org/


4.2、常见问题解决

1:pod:command not found, 

2:wget:command not found 这类问题,可通过配置PATH解决,如果没有这样的问题,可不配置.

查看当前path信息:

打开Jenkins->系统管理->系统设置->全局属性->Environment variables->键值对列表中设置


5、打包脚本

#!/bin/bash -l


ln -s /Users/chris/Library/Keychains/login.keychain-db /Users/chris/Library/Keychains/login.keychain

security unlock-keychain -p (电脑登录密码)/Users/chris/Library/Keychains/login.keychain



# 【配置项目的工程名】

project_name=RSProject

# 【配置项目的scheme】

scheme_name=RSProject

# [日期]

DATE="$(date +%Y%m%d)"

# 【分支名】

BRANCHNAME="${Branch##*/}"

#根据工程目录结构调整该项配置

WORKSPACEDir=${WORKSPACE}/master/RSProject/


build_path=${WORKSPACEDir}/build


exportIpaPath=${WORKSPACEDir}/IPADir/${DATE}/${BRANCHNAME}/${Configuration}


exportOptionsPlistPath=${WORKSPACEDir}/ipa.plist


echo '----------------更新仓库----------------'


cd $WORKSPACEDir


rm -rf $build_path


pod update


echo '----------------正在清理工程----------------'


xcodebuild \

clean -configuration ${Configuration} -quiet || exit


echo '----------------清理完成----------------'


echo '正在编译工程:'${Configuration}


xcodebuild \

archive -workspace ${WORKSPACEDir}/${project_name}.xcworkspace \

-scheme ${scheme_name} \

-configuration ${Configuration} \

-archivePath ${build_path}/${project_name}.xcarchive -quiet || exit


echo '----------------编译完成----------------'


echo '----------------开始ipa打包----------------'


xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \

-configuration ${Configuration} \

-exportPath ${exportIpaPath} \

-exportOptionsPlist ${exportOptionsPlistPath} \

-quiet || exit


echo '----------------上传小飞机>>>>>>>>>>----------------'

#上传fir


if [ "$UPLOAD_FIR" = "true" ]


then

#TOKEN

ZOUZH="7de5bdf118c2e21ccf28b81446310096"

YINM5="b690b12cc484680dc8087e10b4a4e780"


#路径

JOBNAME="ios_ziroom_test"

IPAPATH=${exportIpaPath}/ZiroomerProject.ipa


echo "上传fir"


if [ "$FIR_LOGINNAME" = "zouzh" ]

then

TOKEN=${ZOUZH}

fir p "${IPAPATH}" -T "${TOKEN}" -c "类型-${Configuration}分支-${BRANCHNAME}"

elif [ "$FIR_LOGINNAME" = "yinm5" ]

then

TOKEN=${YINM5}

fir p "${IPAPATH}" -T "${TOKEN}" -c "类型-${Configuration}分支-${BRANCHNAME}"

else

echo "其他FIR_LOGINNAME"

fi


echo "上传成功"

else

echo "没有上传fir"

fi


6、自动化测试

6.1、设置

Scheme->Edit->Gather coverage data,打开覆盖率收集

6.2、编写UnitTestCase

6.3、构建任务

在jenkins内新建任务,用于自动化测试

6.4、自动化测试脚本

#!/bin/bash -l

#新建目录用于保存报告

mkdir test-reports


#pod可能失败的全局参数设置

export LANG=en_US.UTF-8

export LANGUAGE=en_US.UTF-8

export LC_ALL=en_US.UTF-8


pod install


#xcodebuild test -workspace XXX.xcworkspace -scheme XXXTests -destination 'platform=iOS Simulator,name=iPhone 6s'跑测试用例

#-enableCodeCoverage YES 收集测试覆盖率

#ocunit2junit 输出报告转换为jenkins可读的junit报告

xcodebuild test -workspace XXX.xcworkspace -scheme XXXTests -destination 'platform=iOS Simulator,name=iPhone 6s' -configuration Debug -enableCodeCoverage YES 2>&1 | ocunit2junit


#slather coverage转换覆盖率报告为html文件,jenkins可读

#--input-format profdata xcode生成的为profdata格式的文件,转换为html以便jenkins显示

#--ignore 排除筛选需要计算的文件,多个格式写多个ignore表达式

slather coverage --html --input-format profdata --binary-basename XXXApp --scheme XXXTests --workspace XXX.xcworkspace --configuration Debug --ignore **View** --ignore **AppText** --output-directory reports XXX.xcodeproj


6.5、获取报告

安装插件:JUnit Plugin 、HTML Publisher plugin

Jenkins增加下面两个配置

1、        增加构建后操作,选择Publish Junit test result report,配置xml文件路劲为第三步配置的test-reports/*.xml

2、    再增加一个构建后操作,选择Publish HTML reports, 配置html路劲为第三步配置的reports,Index文件为index.html,可以设置标题Reports title为Coverage Report

点击构建,等待结果


7、日常问题汇总

钥匙串的错误

系统管理->Keychains and Provisioning Profiles Management 

项目配置里面->构建环境下的Keychains and Code Signing Identities 

两个地方,查看是否不匹配


cocoapods错误

检查项目配置里面->构建->Execute shell



iOS编译签名文件找不到

用户目录

~/Library/MobileDevice/Provisioning Profiles

拷贝到

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


iOS编译证书找不到

打开“钥匙串访问”,从“登录”拷贝安装的证书到“系统”下


User interaction is not allowed

拷贝~/Library/Keychains/login.keychain到

Users/Shared/Jenkins/Library/Keychains/login.keychain



security / codesign in Sierra:

Keychain ignores access control settings and UI-prompts for permission

https://stackoverflow.com/questions/39868578/security-codesign-in-sierra-keychain-ignores-access-control-settings-and-ui-p


xcodebuild error - SecKey API

returned: -25308


https://stackoverflow.com/questions/41451502/xcodebuild-error-seckey-api-returned-25308

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容