第一步 新建任務(wù)
第二步 輸入項(xiàng)目名稱和選擇自由風(fēng)格的軟件項(xiàng)目點(diǎn)擊確定
第三步 配置下項(xiàng)目地址和賬號(hào)密碼
第四步 配置 build steps 選擇 shell 腳本?
第五步 shell 配置 (注意shell 必須以#!/bin/sh開(kāi)頭,否則會(huì)報(bào) 找不到shell 命令的錯(cuò))
#!/bin/sh
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
reset=$(tput sgr0)
color_cyan="\033[0;36m"
color_red="\033[41;37m"
color_default="\033[0;m"
function echo_log() {
echo -e "${color_cyan}$*${color_default}"
}
function echo_error() {
echo -e "${color_red}$*${color_default}"
}
RunCommand() {
echo_log "-> ????? $* ?????<-"
"$@"
return $?
}
function delete_white_space() {
echo -n "$1" | sed 's/^[ t]*//;s/[ t]*$//'
}
function log_separator() {
echo_log "----------------------------執(zhí)行新的任務(wù)------------------------------"
}
Root=`pwd`
basedir=`cd $(dirname $0); pwd -P`
#current_file="${basedir##*/}"
current_file="AliMobileOneLogin"
RepoName='dp_base_ios_spec'
SpecName=$current_file'.podspec'
SpecPath=$Root/$SpecName
spec_sources='https://cdn.cocoapods.org/'
# 自增,滿10進(jìn)1
increment_version () {
declare -a part=( ${1//\./ } )
declare new
declare -i carry=1
for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
len=${#part[CNTR]}
new=$((part[CNTR]+carry))
[ ${#new} -gt $len ] && carry=1 || carry=0
[ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new}
done
new="${part[*]}"
echo "${new// /.}"
}
#驗(yàn)證版本號(hào)格式
verify_input_tag_format()
{
local tag_version=$1
if [ -z $tag_version ]; then
echo ${red}"請(qǐng)輸入正確的tag 版本號(hào),例如:0.0.1 / 1.0.0 / 1.0.0-bate1"${reset}
exit 1
else
# release的正則
reg='^[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}.*$'
# reg='^(0|[1-9][1-9]*)\.(0|[1-9][1-9]*)\.(0|[1-9][1-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
if [[ "$tag_version" =~ $reg ]]; then
echo ${blue}"tag版本號(hào)正確,格式驗(yàn)證通過(guò)"${reset}
else
echo ${red}"請(qǐng)輸入正確的tag 版本號(hào),例如:0.0.1 / 1.0.0 / 1.0.0-bate1"${reset}
exit 1
fi
fi
}
#驗(yàn)證版本號(hào)是否存在
verify_input_tag_exist()
{
local tag_version=$1
touch .tag_list_file
tempListFilePath=$Root/.tag_list_file
echo ${blue}"1========================================================="${tempListFilePath}
git fetch --tags
git tag -l |sort -r > $tempListFilePath
exist="0"
while read line
do
tag_number=$line
echo ${blue}"2========================================================="${tag_number}
echo ${yellow}"3========================================================="${tempListFilePath}
if [[ $tag_number == $tag_version ]]; then
exist="1"
break
fi
done < $tempListFilePath
if [[ $exist == "1" ]]; then
echo ${red}"tag版本號(hào) ${tag_version} 已經(jīng)存在,請(qǐng)重新執(zhí)行!"${reset}
exit 1
else
echo ${blue}"tag版本號(hào)正確,格式驗(yàn)證通過(guò)"${reset}
fi
rm $tempListFilePath 2> /dev/null
}
# #修改podspec文件版本號(hào)
# change_spec_version()
# {
# local tag_version=$1
# while read line
# do
# reg="^s.version"
# if [[ "$line" =~ $reg ]]; then
# sed -i "" "s/${line}/s.version = \'$tag_version\'/g" $SpecPath
# fi
# done < $SpecPath
# cat $SpecPath
# }
#修改podspec文件版本號(hào)
function change_spec_version()
{
local tag_version=$1
local specPath=$2
echo_log "podspec路徑$specPath"
echo_log "要修改的版本號(hào)$tag_version"
while read line
do
reg="^s.version"
if [[ "$line" =~ $reg ]]; then
sed -i "" "s/${line}/s.version = \'$tag_version\'/g" $specPath
fi
done < $specPath
cat $specPath
}
#提交git及打tag
function update_git_tags(){
RunCommand cd $1
if [ -n "$(git status -s)" ];then
RunCommand git add -A && git commit -m "$new_tag_version"
if [[ $? -ne 0 ]]; then
echo_error "'git add -A && git commit -m' failed,請(qǐng)檢查錯(cuò)誤信息 $1"
exit 1
fi
RunCommand git tag $new_tag_version
RunCommand git push origin --tags && git push origin master
RunCommand git fetch --all
if [[ $? -ne 0 ]]; then
echo_error "'git push origin' failed,請(qǐng)檢查錯(cuò)誤信息 $1"
exit 1
fi
else
echo_error "似乎有異常,Git沒(méi)有變更的內(nèi)容 $1"
exit 1
fi
}
#拷貝打包完的frameworke文件到指定路徑下
copy_framework_to_target_file()
{
echo ${green}"====================== 開(kāi)始拷貝文件 ===================="${reset}
local tag_version=$1
#目標(biāo)地址
target_file_Path=$Root'/Frameworks'
#拷貝地址
file_Path=${Root}'/'$current_file'-'${tag_version}
copy_Path=${file_Path}'/ios'
if [ ! -d "$target_file_Path" ]; then
mkdir $target_file_Path
echo ${green}"========================創(chuàng)建文件夾成功================================="${reset}
else
echo ${green}"========================文件夾已經(jīng)存在================================="${reset}
fi
echo ${green}"====================== 生成的文件路徑$copy_Path ===================="${reset}
echo ${green}"====================== 要拷貝的目標(biāo)文件路徑 $target_file_Path===================="${reset}
# 拷貝生成的framework文件到目標(biāo)文件
cp -rf $copy_Path/* $target_file_Path/
#刪除自動(dòng)生成的framework文件目錄
rm -rf $file_Path
}
#------------------- 腳本開(kāi)始 --------------------
echo ${green}"========================================================="${reset}
echo ${green}"====================== 開(kāi)始發(fā)布組件 ======================="${reset}
echo ${green}"========================================================="${reset}
echo $current_file
## >-------------------------開(kāi)始編譯處理-------------------------------------------<
echo_log ">>>打印路徑"
RunCommand cd $Root
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
last_commit=$(git log --pretty=format:'%h %s (%an, %ar)' -n 5 | tr '\n' ' ';)
echo_log "最近一次提交記錄:$last_commit"
## >----------------------------------更新Git Tag版本號(hào) spec索引庫(kù)處理----------------------------------
log_separator
echo_log "更新git tags版本號(hào) spec索引庫(kù)處理"
old_tag_version="1.0.0"
RunCommand git describe --tags `git rev-list --tags --max-count=1`
#TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
#
# echo $TAG
if [[ $? -ne 0 ]]; then
echo_log "當(dāng)前倉(cāng)庫(kù)沒(méi)有g(shù)it --tags,就使用默認(rèn)的初始版本號(hào) 1.0.0"
else
old_tag_version="$(git describe --tags `git rev-list --tags --max-count=1`)"
fi
echo_log "當(dāng)前版最新tag為:$old_tag_version"
prebetal=`echo $old_tag_version | cut -d '-' -f 1`
new_tag_version="$(increment_version $prebetal)"
echo_log "當(dāng)前組件版本號(hào):${old_tag_version} 新的版本號(hào):${new_tag_version}"
change_spec_version $new_tag_version $SpecPath
RunCommand update_git_tags $Root
#------------------- 版本號(hào)輸入和校驗(yàn) --------------------
#verify_input_tag_format $TagVersion
#verify_input_tag_exist $TagVersion
#change_spec_version $TagVersion
TagVersion=$new_tag_version
echo ${green}"RepoNameRepoName===========${RepoName}=============================================="${reset}
echo ${green}"SpecPathSpecPath===========${RepoName}===================${SpecPath}==========================="${reset}
pod lib lint --sources=${spec_sources} --use-libraries --allow-warnings --no-clean
if [[ $? != 0 ]]; then
git push origin --delete $TagVersion
git tag -d $TagVersion
echo ${red}"本地校驗(yàn)失敗"${reset}
exit 1
else
echo ${red}"本地校驗(yàn)成功"${reset}
fi
pod lib lint --sources=${spec_sources} --use-libraries --allow-warnings --no-clean
if [[ $? != 0 ]]; then
git push origin --delete $TagVersion
git tag -d $TagVersion
echo ${red}"遠(yuǎn)程校驗(yàn)失敗"${reset}
exit 2
else
echo ${red}"遠(yuǎn)程校驗(yàn)成功"${reset}
fi
# pod repo push ${RepoName} 'CommonUtil.podspec' --sources=${spec_sources} --allow-warnings --verbose --use-libraries --skip-import-validation --skip-tests
# if [[ $? != 0 ]]; then
# git push origin --delete $TagVersion
# git tag -d $TagVersion
# echo ${red}"repo push 失敗"${reset}
# exit 3
# else
# echo ${red}"repo push成功"${reset}
# fi
echo ${green}"========================================================="${reset}
echo ${green}"====================== 開(kāi)始打包framework ===================="${reset}
echo ${green}"========================================================="${reset}
pod package ${SpecName} --force --exclude-deps --no-mangle --spec-sources=${spec_sources}
copy_framework_to_target_file $TagVersion
echo ${green}"========================================================="${reset}
echo ${green}"====================== 打包framework完成 ===================="${reset}
echo ${green}"========================================================="${reset}
git add .
git commit -m "發(fā)布組件打包framework:$TagVersion"
git push
git tag -d "$TagVersion"
git push origin --delete tag "$TagVersion"
git tag -a $TagVersion -m "$TagVersion"
git push origin $TagVersion
echo ${green}"========================================================="${reset}
echo ${green}"====================== 組件發(fā)布完成 ======================="${reset}
echo ${green}"========================================================="${reset}
#pod package AliMobileOneLogin.podspec --force –embedded --exclude-deps --no-mangle --spec-sources=https://cdn.cocoapods.org/
第六步 點(diǎn)擊立即構(gòu)建 在構(gòu)建歷史里面查看構(gòu)建日志
第六步 查看編譯狀態(tài)和產(chǎn)物
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-861602.html
到這里,jenkins 配置shell腳本打包組件的完整配置流程就已經(jīng)完成文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-861602.html
到了這里,關(guān)于Jenkins CI/CD 持續(xù)集成專題三 Jenkins 使用shell腳本打包組件配置流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!