自动打包 ipa 并上传到蒲公英测试平台的 shell 脚本。


  • 2021-10-15 脚本更新

Archive

1
2
3
4
5
6
7
xcodebuild archive \
-quiet
-workspace PROJECT_WORKSPACE_FILE_PATH \
-scheme SCHEME \
-configuration CONFIGURATION \
-destination generic/platform=iOS \
-archivePath ARCHIVE_PATH

使用 $ xcodebuild -list 命令查看 Targets、Build Configurations 和 Schemes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ xcodebuild -list
Information about project "TempProjectOC":
Targets:
TempProjectOC
TempProjectOCTests
TempProjectOCUITests

Build Configurations:
Debug
Release

If no build configuration is specified and -scheme is not passed then "Release" is used.

Schemes:
TempProjectOC

Export ipa

1
2
3
4
5
xcodebuild \
-exportArchive \
-archivePath XCARCHIVE_FILE_PATH \
-exportPath EXPORT_IPA_PATH \
-exportOptionsPlist EXPORT_OPTIONS_PLIST_PATH

Upload to pgyer

使用 crul 命令上传 App 示例:

1
$ curl -F "file=@/tmp/example.ipa" -F "_api_key=API_KEY" https://www.pgyer.com/apiv2/app/upload

完整的 shell 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash

# 环境变量
# 使用 CocoaPods 或 Carthage 管理第三方库
XCWORKSPACE_PATH="/Users/wy/Develop/Project/Temp/TempProjectOC/TempProjectOC.xcworkspace"
SCHEME="TempProjectOC"
# 编译后 xxx.xcarchive 文件路径
ARCHIVE_PATH="/Users/wy/Desktop/archive.xcarchive"
# 导出 ipa 文件所在文件夹
EXPORT_PATH="/Users/wy/Desktop/TPOC"
# 导出 ipa 所需的配置文件,可以使用 Xcode 9 导出 ipa 后自动生成的 ExportOptions.plist 文件
EXPORTOPTIONSPLIST_PATH="/Users/wy/Develop/Project/Temp/ExportOptions.plist"
# 蒲公英上传 ipa 时的 _api_key
PGYER_API_KEY="API_KEY"

# tput
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux#answer-20983251
green=`tput setaf 2`
reset=`tput sgr0`

echo "${green}Xcode archiving...${reset}"
# archive
xcodebuild archive \
-quiet \
-workspace ${XCWORKSPACE_PATH} \
-scheme ${SCHEME} \
-configuration Release \
-destination generic/platform=iOS \
-archivePath ${ARCHIVE_PATH} \
|| { exit 1; }

echo ''
echo "${green}Exporting ipa...${reset}"
# 导出 ipa
xcodebuild \
-exportArchive \
-archivePath ${ARCHIVE_PATH} \
-exportPath ${EXPORT_PATH} \
-exportOptionsPlist ${EXPORTOPTIONSPLIST_PATH} \
|| { exit 1; }

echo ''
echo "${green}Uploading ipa...${reset}"
# 将 ipa 上传到蒲公英,并删除打包文件
curl -F "file=@${EXPORT_PATH}/${SCHEME}.ipa" \
-F "_api_key=${PGYER_API_KEY}" \
https://www.pgyer.com/apiv2/app/upload

参考

man xcodebuild
how-to-change-the-output-color-of-echo-in-linux
Shell 教程
[iOS] 从零开始写个自动打包 IPA 脚本
蒲公英 API 文档
Google, …