iOS项目自动化构建实践(Jenkins+Pgyer+Email
2016-10-20 本文已影响220人
Pauley
参考文章:
https://wiki.jenkins-ci.org/display/JENKINS/Keychains+and+Provisioning+Profiles+Plugin
http://www.egeek.me/2013/02/23/jenkins-and-xcode-user-interaction-is-not-allowed/
安装Jenkins
- 点击下载 Jenkins , 按照引导安装即可。
- 安装完毕,在浏览器输入 http://localhost:8080/
打开Terminal,获取管理员密码命令如下:
sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
- 点击Continue , 进入到插件安装引导页
- 选择 Install suggested plugins(建议插件)进行安装,然后倒杯茶,慢慢等一会儿~~
- 安装完成后,进入创建用户页面(建议将密码备份到印象笔记,如果忘记的话,在【遇到的坑和解决方案】可以找到解决方案)
- 安装完成,进入到自动构建管理页面
配置Jenkins
-
主要配置
- 配置Jenkins插件
- 配置login.keychain
- 配置证书
- 配置邮件脚本
-
配置Jenkins插件
系统管理 - 管理插件 - 可选插件,分别安装下列插件:- Xcode integration : 该插件是的Jenkins可以调用Xcode的命令行工具以自动化构建和打包iOS应用(iPhone,iPad等)
- GitLab Plugin : This plugin is a build trigger that allows GitLab to trigger Jenkins builds when code is pushed or a merge request is created. Configuration done on a per-job basis.
- Gitlab Authentication plugin : The GitLab Authentication Plugin provides a means of using GitLab for authentication and authorization to secure Jenkins. GitLab Enterprise is also supported.
-
配置login.keychain
iOS项目签名,需要配置Jenkins的钥匙串🔑文件,一种简单的方法是把本地User目录下的login.keychain文件copy到Jenkins目录下
sudo mkdir /Users/Shared/Jenkins/Library
sudo mkdir /Users/Shared/Jenkins/Library/Keychains
sudo cp ${HOME}/Library/Keychains/login.keychain /Users/Shared/Jenkins/Library/Keychains
- 配置证书Provisioning file
iOS项目描述文件,和配置login.keychain类似,可以直接copy一份到Jenkins目录
sudo mkdir /Users/Shared/Jenkins/Library/MobileDevice
sudo mkdir /Users/Shared/Jenkins/Library/MobileDevice/Provisioning\ Profiles
sudo cp ${HOME}/Library/MobileDevice/Provisioning\ Profiles/* /Users/Shared/Jenkins/Library/MobileDevice/Provisioning\ Profiles/
- 配置邮件脚本
可以参考下面的Python脚本(该脚本在巧哥脚本的基础上优化的,可以自动构建完成后自动发邮件给多个邮箱)
# coding=utf-8
import time
import urllib2
import time
import json
import mimetypes
import os
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
#===============需配置=================
# 上传文件的文件名
file_name = 'Demo'
# 接受邮箱地址
mailto_list=["xxxxxxxxx01@qq.com","xxxxxxxxx02@qq.com"]
# 发送邮箱的服务器、用户名、授权码、邮箱的后缀
mail_host="smtp.qq.com"
mail_user="xxxx"
mail_pass="xxxxxxxxx"
mail_postfix="qq.com"
# 蒲公英用户Key
uKey = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
# 蒲公英API Key
_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
#==========================================
#蒲公英应用上传地址
url = 'http://www.pgyer.com/apiv1/app/upload'
#安装应用时需要输入的密码,这个可不填
installPassword = '111111'
# 运行时环境变量字典
environsDict = os.environ
#此次 jenkins 构建版本号
jenkins_build_number = environsDict['BUILD_NUMBER']
#获取 ipa 文件路径
def get_ipa_file_path():
#工作目录下面的 ipa 文件
ipa_file_workspace_path = '/Users/Shared/Jenkins/Home/workspace/' + file_name + '/Build/' + file_name + '.ipa'
if os.path.exists(ipa_file_workspace_path):
return ipa_file_workspace_path
ipa_file_path = get_ipa_file_path()
print ipa_file_path
def _encode_multipart(params_dict):
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
for k, v in params_dict.items():
data.append('--%s' % boundary)
if hasattr(v, 'read'):
filename = getattr(v, 'name', '')
content = v.read()
decoded_content = content.decode('ISO-8859-1')
data.append('Content-Disposition: form-data; name="%s"; filename="kangda.ipa"' % k)
data.append('Content-Type: application/octet-stream\r\n')
data.append(decoded_content)
else:
data.append('Content-Disposition: form-data; name="%s"\r\n' % k)
data.append(v if isinstance(v, str) else v.decode('utf-8'))
data.append('--%s--\r\n' % boundary)
return '\r\n'.join(data), boundary
def handle_resule(result):
json_result = json.loads(result)
print json_result
if json_result['code'] is 0:
send_Email(json_result)
def send_Email(json_result):
print json_result
appName = json_result['data']['appName']
appKey = json_result['data']['appKey']
appVersion = json_result['data']['appVersion']
appBuildVersion = json_result['data']['appBuildVersion']
appShortcutUrl = json_result['data']['appShortcutUrl']
appUpdated = json_result['data']['appUpdated']
environsString = '安装链接 : ' + 'http://www.pgyer.com/' + str(appShortcutUrl) + ' 密码 : ' + installPassword + ' 更新于:' + str(appUpdated)
msg = MIMEText(environsString)
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg['Subject'] = '【' + str(appName) + ' ' + str(appVersion) +'】' + '(iOS)' + ' 自动构建成功,快给开发提几个Bugs!!!'
msg['From'] = me
msg['To'] = ";".join(mailto_list)
try:
s = smtplib.SMTP_SSL('smtp.qq.com')
s.connect(mail_host,port=465)
s.login(mail_user, mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
s.close()
print 'success'
except Exception, e:
print e
params = {
'uKey': uKey,
'_api_key': _api_key,
'file': open(ipa_file_path, 'rb'),
'publishRange': '2',
'password': installPassword
}
coded_params, boundary = _encode_multipart(params)
req = urllib2.Request(url, coded_params.encode('ISO-8859-1'))
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
resp = urllib2.urlopen(req)
body = resp.read().decode('utf-8')
handle_resule(body)
except urllib2.HTTPError as e:
print(e.fp.read())
- mail_pass="xxxxxxxxx" 是授权码,并不是发送邮箱的密码(如何开启SMTP功能和授权秘钥,参考这篇文章)
- uKey、_api_key为蒲公英用户Key和API Key,获取方式参照这篇文章
- 将这个脚本文件命名为Demo.py(或者你的项目名.py),移动到
/Users/Shared/Jenkins/目录下(创建自动构建任务的时候需要使用)
创建一个自动构建任务
-
在管理页面,创建一个新任务,选择构建一个自由风格的软件项目。
创建一个新任务 -
点击OK,进入到任务配置页面
任务配置页面 - 下面以GitLab托管源码演示配置流程:
- 源码管理器:选择Git,填写Repository URL,点击Add添加Credentials
遇到的坑和解决方案
- pod update的时候提示 : CocoaPods requires your terminal to be using UTF-8 encoding
解决方案:在执行pod update之前export LC_ALL="en_US.UTF-8"
【参考:http://www.tuicool.com/articles/J3mE7r】