iOS自動打包并發布腳本

直接上代碼:

文件: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}

如果還有什么問題,可在下面回復留言。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,936評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,744評論 3 421
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,879評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,181評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,935評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,325評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,384評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,534評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,084評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,892評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,623評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,322評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,735評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,990評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,800評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,084評論 2 375

推薦閱讀更多精彩內容

  • 本文轉自CaryaLiu's Blog。 本文最終實現的是使用腳本打 Ad-hoc 包,并發布測試,當然稍微修改一...
    M_Baron閱讀 1,017評論 1 0
  • 本文最終實現的是使用腳本打 Ad-hoc 包,并發布測試,當然稍微修改一下腳本參數就可以打其他類型的 ipa 包了...
    Crazy2015閱讀 327評論 0 0
  • 一般使用企業證書打包的朋友可能會想到使用腳本進行打包,因為打包的次數相對普通開發者比較多,So本人自己網搜一堆Py...
    iOS_ITCode閱讀 2,272評論 1 4
  • 起初想法: 基于公司原有的Jenkins服務的基礎上,最近在公司自動化打包的時,遇到一個尷尬的問題?為什么不能直接...
    Evans_Xiao閱讀 3,818評論 0 3
  • 雅虎走到今天這個悲催的地步有很多原因,其中有一條就是他們的員工做了太多的偽工作。只要看看他們得產品就知道這一點:他...
    海灘之子閱讀 208評論 1 1