IOS 打包证书签名 shell脚本

shell 脚本:

#!/bin/bash
# 
#--------------------------------------------
# 功能:
# 作者:
# 创建日期:
#--------------------------------------------

#上传服务器配置
appId="0001"
versionId="1.0.0"
uploadUrl="http://*********/test/upload.json"

#证书及描述文件
CODE_SIGN_IDENTITY="iPhone Distribution: *** Appliance Co., Ltd."
DEVELOPMENT_TEAM="5E2PGY2377***"
PROVISIONING_PROFILE_SPECIFIER="****Enterprise"

#打包环境检测
if [ $1 != "sit" -a $1 != "pre" -a $1 != "Pre" -a $1 != "pre2" -a $1 != "Pre2" -a $1 != "pre3" -a $1 != "Pre3" -a $1 != "prd" ]; then
	echo "请输入正确的构建环境:[sit | pre | Pre | pre2 | Pre2 | pre3 | Pre3 | prd]"
    exit 1
else
	build_env=$(echo $1 | perl -pe 's/.*/\u$&/')	
fi

#工程路径读取
workspace_path="$(cd "$(dirname $0)" && pwd)"
cd ${workspace_path}
workspace_name=$(ls | grep xcworkspace)
project_dir="epa_project_ios"
project_path=${workspace_path}/${project_dir}
project_name=$(ls ${project_dir} | grep xcodeproj | awk -F.xcodeproj '{print $1}')
target_name=${project_name}
info_plist=${project_path}/Project/${project_name}-Info.plist
result_path=${workspace_path}/build/${build_env}_$(date +%Y-%m-%d_%H_%M)

#工程代码更新
echo "======开始更新代码======"
cd ${project_path}
git stash
git pull --rebase
cd ${workspace_path}
pod update --no-repo-update --verbose
if [ $? != 0 ]; then
    echo "======代码更新失败======"
    exit 1
else
    echo "======完成代码更新======"
fi

#修改版本号
if [ -n "$2" ]; then
    bundleShortVersion=$(echo $2 | perl -pe 's/.*/\u$&/')
    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${bundleShortVersion}" ${info_plist}
else
    #主版本号读取
    bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" ${info_plist})
fi

#应用标识及名称修改
case "$1" in
	sit|pre|pre2|pre3)
		bundle_identifier=com.suning.${build_env}
    	bundle_name=${build_env}${bundleShortVersion}
    	;;
	Sit|Pre|Pre2|Pre3|Prd|prd)
		bundle_identifier=com.suning.SuningEfubao
    	bundle_name="****test"
	;;
esac

/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier ${bundle_identifier}" ${info_plist}
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName ${bundle_name}" ${info_plist}

#Today Extension
te_info_plist=${project_path}/NotificationBarForYiFuBao/Info.plist
if [ -f ${nse_info_plist} ]; then
    te_bundle_identifier=${bundle_identifier}.NSExtension
    /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier ${te_bundle_identifier}" ${te_info_plist}
fi

#Notification Service Extension
nse_info_plist=${project_path}/NotificationServiceExtension/Info.plist
if [ -f ${nse_info_plist} ]; then
    nse_bundle_identifier=${bundle_identifier}.NotificationServiceExtension
    /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier ${nse_bundle_identifier}" ${nse_info_plist}
fi

#打包环境修改
config_file=${project_path}/Project/Src/Constants/BuildConfig.h

if [ -f ${config_file} ]; then

	upper_env=$(echo ${build_env} | tr '[a-z]' '[A-Z]')

	echo "======修改环境配置======"
	sed -i '' "/TARGET_ENV_/ s/1/0/" ${config_file}
	sed -i '' "/TARGET_ENV_${upper_env}/ s/0/1/" ${config_file}

	sed -i '' "/DEBUG_ENABLE/ s/1/0/" ${config_file}
	
fi

#构建路径设置
mkdir -p "${result_path}"
setting_out=${result_path}/build_setting.txt
xcodebuild -showBuildSettings -workspace "${workspace_name}" -scheme "${target_name}" -configuration Release > ${setting_out}
build_path=`echo $(grep -w  "CONFIGURATION_BUILD_DIR" ${setting_out} | awk -F= '{print $2}')`
echo "======build_path: ${build_path}======"
archive_path=${build_path}/${target_name}.xcarchive


#清除旧包
rm -rf ${archive_path}
#归档打包
echo "======开始构建======"
xcodebuild clean archive -workspace "${workspace_name}" \
-scheme "${target_name}" \
-archivePath ${archive_path} \
-configuration Release \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
DEVELOPMENT_TEAM="${DEVELOPMENT_TEAM}" \
PROVISIONING_PROFILE_SPECIFIER="${PROVISIONING_PROFILE_SPECIFIER}"

echo "======检查是否构建成功======"
if [ -d "${archive_path}" ]; then
	
	echo "构建成功......"
	
else

	echo -e "\033[31m 构建失败,请修正后重新运行! \033[0m"
	rm -rf ${result_path}
	exit 1

fi

cd ${project_path}
git checkout -- ${config_file}
git checkout -- ${info_plist}
git checkout -- SNYifubao.xcodeproj/project.pbxproj

ipa_name=${target_name}_${bundleShortVersion}.ipa
ipa_path=${result_path}
ExportOptionsPlist=${project_path}/ExportOptions.plist

#XCODE8 需要使用系统的ruby
which rvm > /dev/null
if [[ $? -eq 0 ]]; then
	echo "RVM detected, forcing to use system ruby"
	[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
	rvm use system
fi

#导出ipa
xcodebuild -exportArchive \
-archivePath ${archive_path} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${ipa_path}

echo "======检查是否成功导出ipa======"
ipa_temp_path=${ipa_path}/${target_name}.ipa
if [ -f "${ipa_temp_path}" ]; then
	echo "导出ipa成功......"
	#ipa重命名
	mv "${ipa_temp_path}" "${ipa_path}/${ipa_name}"
else
	echo -e "\033[31m 导出ipa失败...... \033[0m"
	exit 1
fi

#备份dSYM
dsym_path=${archive_path}/dSYMs/${target_name}.app.dSYM/Contents/Resources/DWARF/${target_name}
cp -R "${dsym_path}" "${result_path}/${target_name}.dSYM"

#info名称
ipa_info=${target_name}_${bundleShortVersion}.info
echo ${target_name} ${bundleShortVersion} $1"($(date "+%Y-%m-%d %H:%M"))" > ${result_path}/${ipa_info}
ipaDesc="****test"$1

echo "======result_path: ${result_path}======"

###########################################文件上传###########################################

echo "======开始上传测试包======"
case "$build_env" in
Pre|Pre2|Pre3)
build_env="Pre";;
esac
cd ${result_path}
#curl -T "{${ipa_name},${ipa_info}}" -u epp:ios ftp://*********/${build_env}/ipa/


curl -F "appId=${appId}" -F "versionId=${versionId}" -F "bundleId=${bundle_identifier}" -F "ipaDesc=${ipaDesc}" -F "ipaFile=@${ipa_path}/${ipa_name}" ${uploadUrl}

echo "======共耗时${SECONDS}秒======"



猜你喜欢

转载自blog.csdn.net/lxlmycsdnfree/article/details/81015455