# 配置ios发布所需的环境
准备mac编译的主机
# 发布脚本
编译脚本
代码语言:javascript复制time=$(date " %Y%m%d%H%M%S")
commit_num=$(git rev-parse --short HEAD)
product_code=gf
# 打包类型
build_type=$1
# 当前分支
branch_tag=$2
# 版本库版本号
version_number=$3
# app版本号
app_version=$4
export LANG=en_US.UTF-8
#JOB_NAME=gf.ios
run_build() {
if [ "$1" == "app_dev" ]; then
fir publish ./build/gf_dev.ipa -c "jenkins自动打包上传" >/opt/jenkins/logs/gf/gf_dev.log
time=$(date " %Y%m%d%H%M%S")
aa=$(cat /opt/jenkins/logs/gf/gf_dev.log | grep Published | awk '{print $9}' | awk -F'/' '{print $4}')
bb=$(cat /opt/jenkins/logs/gf/gf_dev.log | grep Release | awk '{print $10}')
new_package_name="$JOB_BASE_NAME"_dev_"$time"-"$commit_num"-"$aa"?release_id="$bb"
echo $new_package_name
echo $version_number
curl -H "Content-Type:application/json" -XPOST http://172.16.30.xxx:9110/version/modify -d '{"product_code":"'"$product_code"'","'"version_number"'":"'"$version_number"'","new_package_name":"'"$new_package_name"'"}'
elif [ "$1" == "app_test" ]; then
fir publish ./build/gf_test.ipa -c "jenkins自动打包上传" >/opt/jenkins/logs/gf/gf_test.log
time=$(date " %Y%m%d%H%M%S")
aa=$(cat /opt/jenkins/logs/gf/gf_test.log | grep Published | awk '{print $9}' | awk -F'/' '{print $4}')
bb=$(cat /opt/jenkins/logs/gf/gf_test.log | grep Release | awk '{print $10}')
new_package_name="$JOB_BASE_NAME"_test_"$time"-"$commit_num"-"$aa"?release_id="$bb"
echo $new_package_name
curl -H "Content-Type:application/json" -XPOST http://172.16.30.xxx:9110/version/modify -d '{"product_code":"'"$product_code"'","'"version_number"'":"'"$version_number"'","new_package_name":"'"$new_package_name"'"}'
elif [ "$1" == "app_release" ]; then
fir publish ./build/gf_release.ipa -c "jenkins自动打包上传" >/opt/jenkins/logs/gf/gf_pro.log
time=$(date " %Y%m%d%H%M%S")
aa=$(cat /opt/jenkins/logs/gf/gf_pro.log | grep Published | awk '{print $9}' | awk -F'/' '{print $4}')
bb=$(cat /opt/jenkins/logs/gf/gf_pro.log | grep Release | awk '{print $10}')
new_package_name="$JOB_BASE_NAME"_pro_"$time"-"$commit_num"-"$aa"?release_id="$bb"
echo $new_package_name
curl -H "Content-Type:application/json" -XPOST http://172.16.30.xxx:9110/version/modify -d '{"product_code":"'"$product_code"'","'"version_number"'":"'"$version_number"'","new_package_name":"'"$new_package_name"'"}'
else
exit 1
fi
}
clean() {
echo "清空build文件夹"
rm -rf ./build
mkdir -p ./build
echo "清空Pod文件夹"
rm -rf Pods/
echo "更新Specs仓库"
pod repo update $HOME/.cocoapods/repos/GYSpecs
echo "安装Pod依赖"
pod install --verbose
}
/usr/bin/security unlock-keychain -p GYGJ1688 ~/Library/Keychains/login.keychain
run_build1(){
if [ "$build_type" == "ios_dev" ]; then
clean
./config/build.sh dev "gf_dev" "$branch_tag" "$version_number" "$app_version"
run_build app_dev
elif [ "$build_type" == "ios_test" ]; then
clean
./config/build.sh test "gf_test" "$branch_tag" "$version_number" "$app_version"
run_build app_test
elif [ "$build_type" == "ios_release" ]; then
clean
./config/build.sh release "gf_release" "$branch_tag" "$version_number" "$app_version"
run_build app_release
elif [ "$build_type" == "ios_appstore" ]; then
fastlane ios sign_xcarchive_and_publish
else
exit 2
fi
}
function checkBuild(){
product=$1
version=$2
if [[ -z $product ]] || [[ -z $version ]];then
# 参数为空.
return 1
fi
info_path=http://172.16.30.xxx:9110/version/status
value=`curl -XGET "$info_path?product_code=$product_code&version_number=$version_number" |awk -F'lock_status":' '{print $2}'|awk -F, '{print $1}'`
if [[ $value -eq 0 ]];then
# 可以正常构建.
return 0
else
# 版本已锁定.
return 3
fi
}
res1=$(checkBuild $product_code $version_number)
res2=`echo $?`
if [[ $res2 -eq 1 ]];then
echo "版本号没有输入"
exit $res2
elif [[ $res2 -eq 3 ]];then
echo "对应的版本已锁定"
exit $res2
else
run_build1
fi
jenkinsfile
代码语言:javascript复制pipeline {
agent { label "mac" }
parameters {
string(name: 'version_number', defaultValue: '', description: '')
string(name: 'product_code', defaultValue: 'gf', description: '')
choice choices: ['否', '是'], description: '请选择是否上传到appstore', name: 'choice_value'
choice choices: ['all', 'dev', 'test', 'release'], description: '请选择要打包的环境', name: 'choice_env'
gitParameter (branch:'', branchFilter: 'origin/(.*)', defaultValue: 'master', description: '选择将要构建的分支', name: 'TAG', quickFilterEnabled: true, selectedValue: 'TOP', sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH_TAG', useRepository: 'ssh://git@172.16.30.xxx:2224/gygjqh/gf_app_ios.git')
}
stages {
stage('CleanWorkspace') {
steps {
cleanWs()
}
}
stage('拉取代码') {
steps {
dir("${WORKSPACE}/"){
script {
try {
checkout([$class: 'GitSCM', branches: [[name: '$TAG']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]], submoduleCfg: [], userRemoteConfigs: [[url: "ssh://git@172.16.30.xxx:2224/gygjqh/gf_app_ios.git"]]])
env.COMMIT_ID = sh(script: 'git log --pretty=format:%h', returnStdout: true).trim() // 提交ID
env.COMMIT_USER = sh(script: 'git log --pretty=format:%an', returnStdout: true).trim() // 提交者
env.COMMIT_TIME = sh(script: 'git log --pretty=format:%ai', returnStdout: true).trim() // 提交时间
env.COMMIT_INFO = sh(script: 'git log --pretty=format:%s', returnStdout: true).trim() // 提交信息
}catch(exc) {
env.REASON = "拉取代码出错"
throw(exc)
}
}
}
}
}
stage('打包dev环境') {
when {
expression { params.choice_env ==~ /(dev|all)/ }
}
steps {
sh label: '', script: '''
export LANG=en_US.UTF-8
chmod x ./build_script.sh
./build_script.sh ios_dev $TAG $version_number $app_version
'''
}
}
stage('打包test环境') {
when {
expression { params.choice_env ==~ /(test|all)/ }
}
steps {
sh label: '', script: '''
export LANG=en_US.UTF-8
chmod x ./build_script.sh
./build_script.sh ios_test $TAG $version_number $app_version
'''
}
}
stage('打包release环境') {
when {
expression { params.choice_env ==~ /(release|all)/ }
}
steps {
sh label: '', script: '''
export LANG=en_US.UTF-8
chmod x ./build_script.sh
./build_script.sh ios_release $TAG $version_number $app_version
'''
}
}
stage('发布到appstore') {
when {
expression { params.choice_value == '是'}
}
steps {
sh label: '', script: '''
export LANG=en_US.UTF-8
chmod x ./build_script.sh
./build_script.sh ios_appstore $TAG $version_number $app_version
'''
}
}
}
post {
always {
wrap([$class: 'BuildUser']){
script{
currentBuild.description = "提交者: ${COMMIT_USER}" // 添加说明信息
currentBuild.description = "n提交ID: ${COMMIT_ID}" // 添加说明信息
currentBuild.description = "n提交时间: ${COMMIT_TIME}" // 添加说明信息
currentBuild.description = "n版本号: ${version_number}" // 添加说明信息
sh "printenv"
}
}
}
}
}