IOS项目单独脚本自动化打包

通过脚本自动化打包IOS项目

如果想要单个包批量打包请查看IOS脚本批量打包

一、Shell脚本

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。shell脚本基础学习

在终端输入man xcodebuild,可以看到Description里面有介绍用法。
xcodebuild指令文档学习

项目目录结构

pp9LgDP.png

结构说明:

SinglePackageScript.sh : 为脚本文件 (可不修改)

各个Plist:为打包4种打包模式 AdHoc、AppStore、Development、Enterprise (不需要动)

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>development</string>//这儿development、enterprise、ad-hoc、app-store四种
<key>signingStyle</key>
<string>automatic</string>
<key>compileBitcode</key>
<false/>
</dict>
</plist>

二、开始打包操作

  • 操作脚本
1
2
cd /Users/mac/Desktop/您的项目名称/SinglePackageScript
sh SinglePackageScript.sh

上面要cd到当前项目文件夹下的SinglePackageScript脚本目录,原因是为了当其他项目在不同的目录路径下时,获取到项目层级会不一致,不同的电脑上可能不是同一个,所以cd项目时需要先项目目录文件下SinglePackageScript目录下。

所有脚本如下:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash

# Script.sh
# Package
#
# Created by wws on 2023/02/25.
# Copyright © 2023 年 wws. All rights reserved.

# 1.Configuration Info

# 是否编译工作空间 (例:若是用Cocopods管理的.xcworkspace项目,赋值true;用Xcode默认创建的.xcodeproj,赋值false)
is_workspace="true"
# 指定要打包编译的方式 : Release,Debug...
build_configuration="Release"

# 返回上一级目录,进入项目工程目录
cd ..
# 获取项目名称
projectName=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'`

# Scheme Name
schemeName=${projectName}
# 打包所有生成路径
ipaPath=~/Desktop/${schemeName}
# 临时生成APP路径
buildDir="${ipaPath}/${schemeName}.xcarchive"

plistPath="./SinglePackageScript"
# 导出ipa所需要的plist文件路径 (默认为AdHocExportOptionsPlist.plist)
exportOptionsPlistPath="${plistPath}/AdHocExportOptionsPlist.plist"

# 开始时间
beginTime=`date +%s`

# 蒲公英分发参数 不分发可忽略 默认不分发 下面的两个KEY是默认测试的网址对应KEY
isUploadPgyer=0
USERKEY="xxx"
APIKEY="xxx"

# fir平台分发参数 默认分发
isUploadFir=1
FirApiToken="931b41960fd41496b946e2442c910adb97"

# AdHoc,AppStore,Enterprise三种打包方式的区别: http://blog.csdn.net/lwjok2007/article/details/46379945
echo "\033[36;1m请选择打包方式(输入序号,按回车即可) \033[0m"
echo "\033[33;1m1. AdHoc \033[0m"
echo "\033[33;1m2. AppStore \033[0m"
echo "\033[33;1m3. Enterprise \033[0m"
echo "\033[33;1m4. Development \033[0m"
#读取用户输入并存到变量里
read parameter
sleep 0.5
method="$parameter"

# 判读用户是否有输入
if [ -n "$method" ]
then
if [ "$method" = "1" ] ; then
exportOptionsPlistPath="${plistPath}/AdHocExportOptionsPlist.plist"
elif [ "$method" = "2" ] ; then
exportOptionsPlistPath="${plistPath}/AppStoreExportOptionsPlist.plist"
elif [ "$method" = "3" ] ; then
exportOptionsPlistPath="${plistPath}/EnterpriseExportOptionsPlist.plist"
elif [ "$method" = "4" ] ; then
exportOptionsPlistPath="${plistPath}/DevelopmentExportOptionsPlist.plist"
else
echo "输入的参数无效!!!"
exit 1
fi
fi

echo "\033[32m************************* 开始构建项目 ************************* \033[0m"

# 清除缓存
rm -rf mkdir ${ipaPath}
# 创建打包目录
mkdir ${ipaPath}

# 指定输出文件目录不存在则创建
if [ -d "$ipaPath" ] ; then
echo $ipaPath
else
mkdir -pv $ipaPath
fi

# Build 生成 APPs
# 判断编译的项目类型是workspace还是project
if $is_workspace ; then
# 编译前清理工程
xcodebuild clean -workspace ${projectName}.xcworkspace \
-scheme ${schemeName} \
-configuration ${build_configuration}

xcodebuild archive -workspace ${projectName}.xcworkspace \
-scheme ${schemeName} \
-configuration ${build_configuration} \
-archivePath ${buildDir}
else
# 编译前清理工程
xcodebuild clean -project ${projectName}.xcodeproj \
-scheme ${schemeName} \
-configuration ${build_configuration}

xcodebuild archive -project ${projectName}.xcodeproj \
-scheme ${schemeName} \
-configuration ${build_configuration} \
-archivePath ${buildDir}
fi

# 检查是否构建成功
# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if [ -d "$buildDir" ] ; then
echo "\033[32;1m项目构建成功 \033[0m"
else
echo "\033[31;1m项目构建失败 \033[0m"
exit 1
fi

echo "33[31m appName:$projectName n 33[0m"

# 生成 ipa
xcodebuild -exportArchive -archivePath ${buildDir} -exportOptionsPlist ${exportOptionsPlistPath} -exportPath ${ipaPath} -allowProvisioningUpdates ${YES} -allowProvisioningDeviceRegistration ${YES}

if [[ $? = 0 ]]; then
echo "33[31m n 生成 IPA 成功 nnnnn33[0m"
else
echo "33[31m n 生成 IPA 失败 nnnnn33[0m"
fi

rm -rf ${buildDir}

# 移动
#mv ${ipaPath}/$displayName.ipa ${displayName}/$appName

# 上传fir
if [[ $isUploadFir = 1 ]]; then
echo "正在上传fir..."
fir p "${ipaPath}/${displayName}/${displayName}.ipa" -T ${FirApiToken}
fi

# 上传蒲公英分发平台
if [[ $ISUPLOAD = 1 ]]; then
echo "正在上传蒲公英..."
curl -F "file=@$ipaPath/${displayName}/${displayName}.ipa" -F "uKey=$USERKEY" -F "_api_key=$APIKEY" http://www.pgyer.com/apiv1/app/upload

fi

# 结束时间
endTime=`date +%s`
echo -e "打包时间$[ endTime - beginTime ]秒"

判读用户是否有输入 1、AdHoc 2、AppStore 3、Enterprise 4、Development 等待生成ipa

如果打包出现证书匹配问题,如果选择的auto 脚本里需要配置,上面脚本已经配置。

-allowProvisioningUpdates ${YES}

-allowProvisioningDeviceRegistration ${YES}

如果打包报其他错误,请先在Xcode项目上保证能archive,先解决掉代码上的错误才能保证脚本打包。

三、生成产品包

  • 打包生成

pp9Lp0f.png

如果上传fri,对应的二维码就是生成在fir平台的安装包

  • 如果需要上传fir平台

如果没有安装fir插件,请先安装

1
sudo gem install fir-cli –no-ri –no-rdoc

执行成功会生成对应的项目文件目录

1
2
echo "fir平台..."
fir p ${ipaPath}/${displayName}/${displayName}.ipa -T 931b41960f7496b946ei2c910pdb97

上面-T后面的token需要替换成你自己,上面只是例子随便的值,如果想要知道如果获取fir的token登陆fir官网看以下图

pp9q2Y4.png

  • 如果需要上传蒲公英pgyer分发平台
1
2
echo "正在上传蒲公英..."
curl -F "file=@$ipaPath/$displayName/$displayName.ipa" -F "uKey=$USERKEY" -F "_api_key=$APIKEY" http://www.pgyer.com/apiv1/app/upload

方法不累述 USERKEY和APIKEY,上面默认的脚本不上传fir和pgyer,你可以根据自己需求,打开注释,和修改是否需要上传蒲公英的状态开关。