美文网首页
iOS自动打包并发布脚本

iOS自动打包并发布脚本

作者: 闲云清烟 | 来源:发表于2017-09-11 10:24 被阅读118次

    直接上代码:

    文件:autobuild.py

    #!/usr/bin/env python

    # -*- coding:utf-8 -*-

    #./autobuild.py -p youproject.xcodeproj -s schemename

    #./autobuild.py -w youproject.xcworkspace -s schemename

    import argparse

    import subprocess

    import requests

    import os

    #configuration for iOS build setting

    CONFIGURATION = "Debug"

    EXPORT_OPTIONS_PLIST = "exportOptions.plist"

    #会在桌面创建输出ipa文件的目录

    EXPORT_MAIN_DIRECTORY = "~/Desktop/"

    # configuration for pgyer

    PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"

    DOWNLOAD_BASE_URL = "http://www.pgyer.com"

    USER_KEY = "d7fda23e2e6d11c2a3af54e7c56e4377"

    API_KEY = "100a76c21d6e5202069223ea19018696"

    #设置从蒲公英下载应用时的密码

    PYGER_PASSWORD = "123"

    # 蒲公英更新描述

    PGYDESC = "testtest"

    def cleanArchiveFile(archiveFile):

    cleanCmd = "rm -r %s" %(archiveFile)

    process = subprocess.Popen(cleanCmd, shell = True)

    process.wait()

    print "cleaned archiveFile: %s" %(archiveFile)

    def parserUploadResult(jsonResult):

    resultCode = jsonResult['code']

    if resultCode == 0:

    downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']

    print "Upload Success"

    print "DownUrl is:" + downUrl

    else:

    print "Upload Fail!"

    print "Reason:"+jsonResult['message']

    def uploadIpaToPgyer(ipaPath):

    print "ipaPath:"+ipaPath

    ipaPath = os.path.expanduser(ipaPath)

    ipaPath = unicode(ipaPath, "utf-8")

    files = {'file': open(ipaPath, 'rb')}

    headers = {'enctype':'multipart/form-data'}

    payload = {'uKey':USER_KEY,'_api_key':API_KEY,'publishRange':'2','isPublishToPublic':'2', 'password':PYGER_PASSWORD, 'updateDescription':PGYDESC}

    print "update desc:" + PGYDESC

    print "uploading...."

    r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers)

    if r.status_code == requests.codes.ok:

    result = r.json()

    parserUploadResult(result)

    else:

    print 'HTTPError,Code:'+r.status_code

    #创建输出ipa文件路径: ~/Desktop/{scheme}{2016-12-28_08-08-10}

    def buildExportDirectory(scheme):

    dateCmd = 'date "+%Y-%m-%d_%H-%M-%S"'

    process = subprocess.Popen(dateCmd, stdout=subprocess.PIPE, shell=True)

    (stdoutdata, stderrdata) = process.communicate()

    exportDirectory = "%s%s%s" %(EXPORT_MAIN_DIRECTORY, scheme, stdoutdata.strip())

    return exportDirectory

    def buildArchivePath(tempName):

    process = subprocess.Popen("pwd", stdout=subprocess.PIPE)

    (stdoutdata, stderrdata) = process.communicate()

    archiveName = "%s.xcarchive" %(tempName)

    archivePath = stdoutdata.strip() + '/' + archiveName

    return archivePath

    def getIpaPath(exportPath):

    cmd = "ls %s" %(exportPath)

    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

    (stdoutdata, stderrdata) = process.communicate()

    ipaName = stdoutdata.strip()

    ipaPath = exportPath + "/" + ipaName

    return ipaPath

    def exportArchive(scheme, archivePath):

    exportDirectory = buildExportDirectory(scheme)

    exportCmd = "xcodebuild -exportArchive -archivePath %s -exportPath %s -exportOptionsPlist %s" %(archivePath, exportDirectory, EXPORT_OPTIONS_PLIST)

    process = subprocess.Popen(exportCmd, shell=True)

    (stdoutdata, stderrdata) = process.communicate()

    signReturnCode = process.returncode

    if signReturnCode != 0:

    print "export %s failed" %(scheme)

    return ""

    else:

    return exportDirectory

    def buildProject(project, scheme):

    archivePath = buildArchivePath(scheme)

    print "archivePath: " + archivePath

    archiveCmd = 'xcodebuild -project %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(project, scheme, CONFIGURATION, archivePath)

    process = subprocess.Popen(archiveCmd, shell=True)

    process.wait()

    archiveReturnCode = process.returncode

    if archiveReturnCode != 0:

    print "archive workspace %s failed" %(workspace)

    cleanArchiveFile(archivePath)

    else:

    exportDirectory = exportArchive(scheme, archivePath)

    cleanArchiveFile(archivePath)

    if exportDirectory != "":

    ipaPath = getIpaPath(exportDirectory)

    uploadIpaToPgyer(ipaPath)

    def buildWorkspace(workspace, scheme):

    archivePath = buildArchivePath(scheme)

    print "archivePath: " + archivePath

    archiveCmd = 'xcodebuild -workspace %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(workspace, scheme, CONFIGURATION, archivePath)

    process = subprocess.Popen(archiveCmd, shell=True)

    process.wait()

    archiveReturnCode = process.returncode

    if archiveReturnCode != 0:

    print "archive workspace %s failed" %(workspace)

    cleanArchiveFile(archivePath)

    else:

    exportDirectory = exportArchive(scheme, archivePath)

    cleanArchiveFile(archivePath)

    if exportDirectory != "":

    ipaPath = getIpaPath(exportDirectory)

    uploadIpaToPgyer(ipaPath)

    def xcbuild(options):

    project = options.project

    workspace = options.workspace

    scheme = options.scheme

    desc = options.desc

    # global PGYDESC

    PGYDESC = desc

    if project is None and workspace is None:

    pass

    elif project is not None:

    buildProject(project, scheme)

    elif workspace is not None:

    buildWorkspace(workspace, scheme)

    def main():

    parser = argparse.ArgumentParser()

    parser.add_argument("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")

    parser.add_argument("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")

    parser.add_argument("-s", "--scheme", help="Build the scheme specified by schemename. Required if building a workspace.", metavar="schemename")

    parser.add_argument("-m", "--desc", help="Pgyer update description.", metavar="description")

    options = parser.parse_args()

    print "options: %s" % (options)

    xcbuild(options)

    if __name__ == '__main__':

    main()

    文件:exportOptions.plist

    文件:README.md

    #autobuild.py

    iOS 自动化打包脚本,并上传*ipa*文件至蒲公英。参数说明:

    ```

    Usage: autobuild.py [options]

    Options:

    -h, --help            show this help message and exit

    -w name.xcworkspace, --workspace=name.xcworkspace

    Build the workspace name.xcworkspace.

    -p name.xcodeproj, --project=name.xcodeproj

    Build the project name.xcodeproj.

    -s schemename, --scheme=schemename

    Build the scheme specified by schemename. Required if

    building a workspace.

    -m description, --desc=description

    Pgyer update description.

    ```

    使用方式:

    ```

    ./autobuild.py -p youproject.xcodeproj -s schemename

    或者

    ./autobuild.py -w youproject.xcworkspace -s schemename

    ```

    脚本中有几个全局变量,根据自己项目设置进行修改其值, 包括:

    ```

    CONFIGURATION = "Release"

    EXPORT_OPTIONS_PLIST = "exportOptions.plist"

    #会在桌面创建输出ipa文件的目录

    EXPORT_MAIN_DIRECTORY = "~/Desktop/"

    ```

    *CONFIGURATION*:可在项目工程文件所在目录执行`xcodebuild -list`查看所有可选取值。

    *EXPORT_OPTIONS_PLIST*:导出*ipa*文件时的配置参数,该参数值是你的配置参数文件名,请在*autobuild.py*文件所在目录下创建*exportOptions.plist*文件,配置参数({app-store, ad-hoc, enterprise, development})可使用`xcodebuild --help`查看所有可选取值。

    *EXPORT_MAIN_DIRECTORY*:默认情况下会在桌面创建*ipa*文件导出的文件夹,文件夹命名如:*~/Desktop/{scheme}{2016-12-28_08-08-10}*。

    ```

    # configuration for pgyer

    PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"

    DOWNLOAD_BASE_URL = "http://www.pgyer.com"

    USER_KEY = "15d6xxxxxxxxxxxxxxxxxx"

    API_KEY = "efxxxxxxxxxxxxxxxxxxxx"

    #设置从蒲公英下载应用时的密码

    PYGER_PASSWORD = ""

    # 蒲公英更新描述

    PGYDESC = ""

    ```

    上传*ipa*文件至蒲公英的配置,你需要设置的是:*USER_KEY*, *API_KEY*, *PYGER_PASSWORD*。

    本文最终实现的是使用脚本打 Ad-hoc 包,并发布测试,当然稍微修改一下脚本参数就可以打其他类型的 ipa 包了。另外该脚本还实现了将生成的 ipa 包上传至蒲公英进行测试分发。

    结合蒲公英分发平台,将 ipa 文件上传至蒲公英分发平台,同时在终端会打印上传结果以及上传应用后该应用的 URL。蒲公英分发平台能够方便地将 ipa 文件尽快分发到测试人员,该平台有开放 API,可避免人工上传。

    常见问题:

    找不到request module.

    import requests

    ImportError: No module named requests

    找不到request module,参考stackoverflow, 使用$ sudo pip install requests或者sudo easy_install -U requests;

    如果使用了上传蒲公英,且安装需要密码,请打开脚本,搜一下脚本里的password,将其值设置为空。

    ========== update 2016-12-28 ==========

    github上脚本进行更新:

    使用xcodebuild -exportArchive替换PackageApplication进行打包.

    解析传入参数使用argparse替换OptionParser.

    去掉对PROVISIONING_PROFILECODE_SIGN_IDENTITY的配置,请使用Xcode8的自动证书管理。

    新增exportOptions.plist文件,用于设置导出ipa文件的参数,该文件中的可配置参数可使用xcodebuild --help查看。

    脚本传入参数去掉--target和--output,ipa文件默认会存放在Desktop创建诸如{scheme}{2016-12-28_08-08-10}格式的文件夹中。

    假如你的项目目录如下所示:

    |____AOP

    | |____AppDelegate.h

    | |____AppDelegate.m

    | |____Base.lproj

    | | |____LaunchScreen.xib

    | | |____Main.storyboard

    | |____Images.xcassets

    | |____Info.plist

    | |____main.m

    | |____ViewController.h

    | |____ViewController.m

    |____AOP.xcodeproj

    |____autobuild

    | |____autobuild.py

    | |____exportOptions.plist

    先进入autobuild目录,使用脚本打包的命令如下:

    python autobuild.py -p ../AOP.xcodeproj -s AOP

    脚本执行完毕,若成功,则会在桌面生成ipa文件。

    若是打包xcworkspace项目,则打包命令格式如下所示:

    python autobuild.py -w ../yourworkspace.xcworkspace -s yourscheme

    exportOptions.plist文件中的可选配置参数如下:

    {app-store, ad-hoc, enterprise, development}

    如果还有什么问题,可在下面回复留言。

    相关文章

      网友评论

          本文标题:iOS自动打包并发布脚本

          本文链接:https://www.haomeiwen.com/subject/mkbxsxtx.html