Jenkins CI/CD 持续集成专题三 Jenkins 使用shell脚本打包组件配置流程

第一步 新建任务

第二步 输入项目名称和选择自由风格的软件项目点击确定

第三步 配置下项目地址和账号密码

第四步 配置 build steps 选择 shell 脚本

第五步 shell 配置 (注意shell 必须以#!/bin/sh开头,否则会报 找不到shell 命令的错)

bash 复制代码
#!/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 "----------------------------执行新的任务------------------------------"
}

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进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// /.}"
 }
 
#验证版本号格式
    verify_input_tag_format()
    {
        local tag_version=$1
        if [ -z $tag_version ]; then
            echo ${red}"请输入正确的tag 版本号,例如: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版本号正确,格式验证通过"${reset}
            else
                echo ${red}"请输入正确的tag 版本号,例如:0.0.1 / 1.0.0 / 1.0.0-bate1"${reset}
                exit 1
            fi
        fi
    }

#验证版本号是否存在
    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版本号 ${tag_version} 已经存在,请重新执行!"${reset}
            exit 1
        else
            echo ${blue}"tag版本号正确,格式验证通过"${reset}
        fi
        rm $tempListFilePath 2> /dev/null
    }

#    #修改podspec文件版本号
#    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文件版本号
function change_spec_version()
{
    local tag_version=$1
    local specPath=$2
        echo_log "podspec路径$specPath"
        echo_log "要修改的版本号$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,请检查错误信息 $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,请检查错误信息 $1"
        exit 1
    fi
else
    echo_error "似乎有异常,Git没有变更的内容  $1"
    exit 1
fi

}
#拷贝打包完的frameworke文件到指定路径下
    copy_framework_to_target_file()
    {
    echo ${green}"====================== 开始拷贝文件 ===================="${reset}
         local tag_version=$1
    #目标地址
     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}"========================创建文件夹成功================================="${reset}
           else
            echo ${green}"========================文件夹已经存在================================="${reset}
             fi
              echo ${green}"====================== 生成的文件路径$copy_Path ===================="${reset}
                  echo ${green}"====================== 要拷贝的目标文件路径 $target_file_Path===================="${reset}
           # 拷贝生成的framework文件到目标文件
            cp  -rf  $copy_Path/*  $target_file_Path/
            #删除自动生成的framework文件目录
            rm -rf $file_Path
        }

     #------------------- 脚本开始 --------------------
    echo ${green}"========================================================="${reset}
    echo ${green}"====================== 开始发布组件 ======================="${reset}
    echo ${green}"========================================================="${reset}

    echo $current_file


## >-------------------------开始编译处理-------------------------------------------<
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版本号 spec索引库处理----------------------------------

log_separator
echo_log "更新git tags版本号 spec索引库处理"

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 "当前仓库没有git --tags,就使用默认的初始版本号 1.0.0"
else
    old_tag_version="$(git describe --tags `git rev-list --tags --max-count=1`)"
fi
echo_log "当前版最新tag为:$old_tag_version"


prebetal=`echo $old_tag_version | cut -d '-' -f 1`
new_tag_version="$(increment_version $prebetal)"
echo_log "当前组件版本号:${old_tag_version} 新的版本号:${new_tag_version}"

change_spec_version $new_tag_version $SpecPath
 RunCommand update_git_tags $Root
 
     #------------------- 版本号输入和校验 --------------------
#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}"本地校验失败"${reset}
         exit 1
    else
         echo ${red}"本地校验成功"${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}"远程校验失败"${reset}
         exit 2
    else
          echo ${red}"远程校验成功"${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}"====================== 开始打包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 "发布组件打包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}"====================== 组件发布完成 ======================="${reset}
echo ${green}"========================================================="${reset}


#pod package AliMobileOneLogin.podspec  --force --embedded  --exclude-deps --no-mangle --spec-sources=https://cdn.cocoapods.org/

第六步 点击立即构建 在构建历史里面查看构建日志

第六步 查看编译状态和产物

到这里,jenkins 配置shell脚本打包组件的完整配置流程就已经完成

相关推荐
error:(1 小时前
【保姆级】讯飞ROS智能车 Debian系统 U盘克隆/恢复教程
运维·人工智能·debian
@donshu@2 小时前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible
bryant_meng2 小时前
【linux】Linux vs Android
android·linux·运维
IT成长日记2 小时前
【Docker基础】Docker核心概念:联合文件系统(Union FS)详解
运维·docker·容器·联合文件系统·union fs
eybk2 小时前
Flask设计网页截屏远程电脑桌面及切换运行程序界面
运维·服务器·python
kcarly4 小时前
如何科学测算AI业务场景所需算力服务器?——以Qwen3 32B模型与海光K100为例
运维·服务器·人工智能
青芒亭4 小时前
泰山派RK3566编译命令
linux·运维·服务器
kaikaile19954 小时前
windows配置supervisor实现nginx自启
运维·windows·nginx
zz9602264 小时前
DELL R730XD服务器调整风扇转速
运维·服务器
宇钶宇夕5 小时前
工业 AI Agent:智能化转型的核心驱动力
运维·人工智能·物联网·算法·自动化