原文 : tomczhen的博客

运行环境

建议根据实际情况安装:

  1. 如项目中没有使用CocoaPods管理类库则不用安装CocoaPods;
  2. xctool是一个很好的构建工具, 有更加友好的log输出和使用方法. 如使用Xcode自带的xcodebuild构建则不需要安装xctool.

安装 CocoaPods

  1. 更新Ruby
    gem update --system

  2. 修改Ruby安装源为国内源
    gem sources --remove https://rubygems.org/
    gem sources -a https://ruby.taobao.org/
    注:使用gem sources -l命令查看源列表

  3. 安装cocoapods
    sudo gem install cocoapods

安装 xctool

  1. 安装brew
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  2. 安装Xcode’s Command Line Tools
    xcode-select install

  3. 使用brew安装xctool
    brew install xctool

使用说明

  • 运行脚本前需要在xcode中配置好证书及描述文件。
  • 如果不需要修改代码请注释掉相关sed语句。
  • 默认使用xctool进行构建,需要先安装好xctool。
  • 使用xcodebuild构建需要注释xctool相关语句,并取消xcodebuild相关语句的注释。
  • 在项目目录创建脚本文件,例如:iosbuild.sh(与.xcworkspace文件同级目录),使用命令chmod 777 iosbuild.sh为脚本文件增加可执行权限,执行脚本会根据配置在EXPORT_PATH中生成ipa文件并上传至蒲公英平台。

脚本代码

#!/bin/sh
set -e

####################
#Project Setting  
####################
#project name
PROJECT_NAME=yourprojectname
#workspace name
PROJECT_WORKSPACE_NAME=yourworkspacename
#scheme name
PROJECT_SCHEME_NAME=yourschemename
#profile name
PROFILE_NAME="yourprofilename"
#project Path
PROJECT_PATH=$(pwd)
#build time
BUILD_TIME=$(date +%Y%m%d%H%M)
#project temp path
PROJECT_TEMP_PATH=${PROJECT_PATH}_TEMP
#archive path
ARCHIVE_PATH=${PROJECT_PATH}/build/release/${PROJECT_NAME}_r${BUILD_TIME}
#export path
EXPORT_PATH=${ARCHIVE_PATH}.ipa
#sdk version
SDK_VERSION=iphoneos

######################
#api url
######################
API_SERVER_URL='"http:\/\/api.xxxx.com"'

######################
#third-party key
######################

#thirdpartykey
THIRD_PARTY_KEY='"keyvalue"'

######################
#pgyer key
######################
#pgyer ukey
PGY_UKEY=yourpgyukey
#pgyer api key
PGY_API_KEY=yourpgyapikey

######################
#build
######################
#pod install
pod install --verbose --no-repo-update

#copy project to temp path
echo "copy project files to temp path."
cp -r -f ${PROJECT_PATH} ${PROJECT_TEMP_PATH}
cd ${PROJECT_TEMP_PATH}

#modify code
echo "config api server url."
sed -i "" "/^\/\//!s/.* APIServerUrl.*/NSString * const APIServerUrl= @${API_SERVER_URL};/" yourcodefilepath
echo "config third-party keys."

#rongcloud api key
sed -i "" "/^\/\//!s/.*THIRD_PARTY_KEY.*/#define THIRD_PARTY_KEY @${THIRD_PARTY_KEY}/g" yourcodefilepath

#clean project
echo "clean project."

#xcodebuild -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} clean
xctool -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} clean

#archive project
echo "archive project."
#xcodebuild -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} build archive -archivePath ${ARCHIVE_PATH}
xctool -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} build archive -archivePath ${ARCHIVE_PATH}

#delete project temp path
echo "delete project temp path."
cd ${PROJECT_PATH}
rm -r -f ${PROJECT_TEMP_PATH}

#export archive file to ipa
echo "export ipa file to export path."
xcodebuild -exportArchive -archivePath ${ARCHIVE_PATH}.xcarchive -exportPath ${EXPORT_PATH} -exportFormat ipa -exportProvisioningProfile ${PROFILE_NAME}

#upload to pgyer
echo "upload to pgyer."
curl -F "file=@${EXPORT_PATH}" -F "uKey=${PGY_UKEY}" -F "_api_key=${PGY_API_KEY}" http://www.pgyer.com/apiv1/app/upload
echo "upload finish."

扩展阅读

iOS自动打包并发布脚本