iOS 持续化集成iOS程序猿程序员

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

开始

打开Terminal,获取管理员密码命令如下:

sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
插件安装 安装中 创建用户 自动构建管理页面

配置Jenkins

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

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())

创建一个自动构建任务

遇到的坑和解决方案

  1. 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

上一篇 下一篇

猜你喜欢

热点阅读