自动化打包上传

准备好两个plist文件

一个正式的Release.plist

一个测试的Debug.plist

plist文件相当于打包上传的时候配置文件

比如是应用商店的

还是本地测试的

是自动管理的

还是手动配置的等等

正式的Release.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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>app-store</string>
<key>Upload</key>
<false/>
<key>Export</key>
<true/>
<key>stripSwiftSymbols</key>
<true/>
<key>uploadSymbols</key>
<true/>
<key>signingStyle</key>
<string>automatic</string>
</dict>
</plist>

测试的Debug.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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>compileBitcode</key>
<true/>
<key>destination</key>
<string>export</string>
<key>iCloudContainerEnvironment</key>
<string>Production</string>
<key>method</key>
<string>ad-hoc</string>
<key>signingStyle</key>
<string>automatic</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>teamID</string>
<key>thinning</key>
<string>&lt;none&gt;</string>
</dict>
</plist>

然后准备好两个脚本文件

一个正式上传到应用商店的Release.sh

一个测试上传到蒲公英的Debug.sh

上传到应用商店的还有两个参数apiKeyapiIssuer

打开开发者中心

https://appstoreconnect.apple.com/access/api

然后看下图

商店的Release.sh

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
#蒲公英AppKey
uKey=""
apiKey=""
#当前工程绝对路径
project_path=$(pwd)
#生成的IPA文件存放路径
project_path_pre=$(pwd)
#工程名
project_name=591XT.XYZ
#scheme名
scheme_name=591XT.XYZ
#打包模式 Debug/Release
development_mode=Release
#plist文件所在路径
timeStr=$(date "+%Y%m%d%H%M%S")
exportOptionsPlistPath=${project_path}/Release.plist
#编译过程中产生的临时文件夹,build文件夹路径
tempBuild_path=${project_path}/${timeStr}/Releasebuild
#build文件夹路径
build_path=${project_path_pre}/${timeStr}/Releasebuild
#导出.ipa文件所在路径
exportIpaPath=${project_path_pre}/${timeStr}/${development_mode}/ipa

for file in $(ls -a)
do
temp=${file%%.xcodeproj*}
num1=$[${#file}-9]
num2=$[${#temp}+1]
if test $[num1] -eq $[num2]
then
project_name=${file%.xcodeproj}
scheme_name=${file%.xcodeproj}
fi
done

echo '正在清理工程'

xcodebuild clean -configuration ${development_mode}

echo '正在编译工程'

xcodebuild archive -workspace ${project_path}/${project_name}.xcworkspace -scheme ${scheme_name} -configuration ${development_mode} -archivePath ${build_path}/${project_name}.xcarchive

echo '开始ipa打包'

xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive -configuration ${development_mode} -exportPath ${exportIpaPath} -exportOptionsPlist ${exportOptionsPlistPath} -allowProvisioningUpdates

mv $exportIpaPath/*.ipa $exportIpaPath/${scheme_name}.ipa

if [ -e $exportIpaPath/$scheme_name.ipa ];
then
echo 'ipa包已导出正在上传到蒲公英'
curl -F "file=@$exportIpaPath/$scheme_name.ipa" \
-F "uKey=${uKey}" \
-F "_api_key=${apiKey}" \
https://www.pgyer.com/apiv1/app/upload 2>&1 | tee "$exportIpaPath/$scheme_name.txt"
echo 'ipa包正在验证'
xcrun altool --upload-app -f $exportIpaPath/${scheme_name}.ipa -t ios --apiKey apiKey --apiIssuer apiIssuer --verbose
echo 'ipa包验证成功正在上传到AppStore'
xcrun altool --upload-app -f $exportIpaPath/${scheme_name}.ipa -t ios --apiKey apiKey --apiIssuer apiIssuer --verbose
echo '上传成功'
else
echo 'ipa包导出失败 '
fi
echo '打包ipa完成'
exit 0

蒲公英的Debug.sh

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
#蒲公英AppKey
uKey=""
apiKey=""
#当前工程绝对路径
project_path=$(pwd)
#生成的IPA文件存放路径
project_path_pre=$(pwd)
#工程名
project_name=591XT.XYZ
#scheme名
scheme_name=591XT.XYZ
#打包模式 Debug/Release
development_mode=Debug
#plist文件所在路径
exportOptionsPlistPath=${project_path}/Debug.plist
#编译过程中产生的临时文件夹,build文件夹路径
timeStr=$(date "+%Y%m%d%H%M%S")
tempBuild_path=${project_path}/${timeStr}/Releasebuild
#build文件夹路径
build_path=${project_path_pre}/${timeStr}/Releasebuild
#导出.ipa文件所在路径
exportIpaPath=${project_path_pre}/${timeStr}/${development_mode}/ipa

for file in $(ls -a)
do
temp=${file%%.xcodeproj*}
num1=$[${#file}-9]
num2=$[${#temp}+1]
if test $[num1] -eq $[num2]
then
project_name=${file%.xcodeproj}
scheme_name=${file%.xcodeproj}
fi
done

echo '正在清理工程'

xcodebuild clean -configuration ${development_mode}

echo '正在编译工程'

xcodebuild archive -workspace ${project_path}/${project_name}.xcworkspace -scheme ${scheme_name} -configuration ${development_mode} -archivePath ${build_path}/${project_name}.xcarchive

echo '开始ipa打包'

xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive -configuration ${development_mode} -exportPath ${exportIpaPath} -exportOptionsPlist ${exportOptionsPlistPath} -allowProvisioningUpdates

mv $exportIpaPath/*.ipa $exportIpaPath/${scheme_name}.ipa

if [ -e $exportIpaPath/$scheme_name.ipa ];
then
echo 'ipa包已导出正在上传到蒲公英'
curl -F "file=@$exportIpaPath/$scheme_name.ipa" \
-F "uKey=${uKey}" \
-F "_api_key=${apiKey}" \
https://www.pgyer.com/apiv1/app/upload 2>&1 | tee "$exportIpaPath/$scheme_name.txt"
else
echo 'ipa包导出失败 '
fi
echo '打包ipa完成'
exit 0

这四个文件需要都放在项目目录

plist文件可能需要根据具体情况进行修改

如果显示这个

1
zsh: permission denied

那是因为sh文件没有权限

执行下面的语句即可

1
chmod u+x *.sh

如果没有用pod

还需要把命令改一下

1
2
xcodebuild archive -workspace ${project_path}/${project_name}.xcworkspace -scheme ${scheme_name} -configuration ${development_mode} -archivePath ${build_path}/${project_name}.xcarchive

改成

1
xcodebuild archive -project ${project_path}/${project_name}.xcodeproj -scheme ${scheme_name} -configuration ${development_mode} -archivePath ${build_path}/${project_name}.xcarchive