通过GIT_COMMIT进行代码回滚

时间:2023-11-28 23:05:44

首先需要安装插件:conditional-buildstep

A buildstep wrapping any number of other buildsteps, controlling their execution based on a defined condition (e.g. BuildParameter).
1、构建代码时,需要记录每次构建的GIT_COMMIT,保存到一个文本中,以构建编号进行命名:
通过GIT_COMMIT进行代码回滚
#!/bin/sh

#$:send branch
#$:jenkins build id
#$:git commit

branch=$
version=$
commit=$

case $branch in
    Pre)
        echo "branch: ${branch}"
        echo "version: ${version}"
        echo "commit: ${commit}"
        #同步代码到发布目录
        #to do

        #备份版本git commit
        echo ${commit}>/home/wos/test2/${version}.txt

        ;;
    Real)
        echo "branch: ${branch}"
        echo "version: ${version}"
        echo "commit: ${commit}"
        #同步代码到发布目录
        #to do

        #备份版本git commit
        echo ${commit}>/home/wos/test2/${version}.txt

        ;;
    Rollback_Pre)
        echo "branch: ${branch}"
        echo "version: ${version}"
        echo "commit: ${commit}"
        #同步代码到发布目录
        #to do
        ;;
    Rollback_Real)
        echo "branch: ${branch}"
        echo "version: ${version}"
        echo "commit: ${commit}"
        #同步代码到发布目录
        #to do
        ;;
    *)
        exit
        ;;
esac

2、进行回滚

通过GIT_COMMIT进行代码回滚

case $deploy_evn in
    Rollback_Pre)
        url="$pre_version"
        ;;
    Rollback_Real)
        url="$real_version"
        ;;
    *)
        exit
        ;;
esac

model="$deploy_evn"
projectName=`echo "$url"|awk -F '/' '{print $6}'`
rollbackID=`echo "$url"|awk -F '/' '{print $7}'`

#获取回滚GIT_COMMIT
commit=$(cat /home/wos/test2/${rollbackID}.txt)
#进入备份目录
cd /home/wos/test3/WOS_Portal/
#下载最新代码
git clone http://username:password@git.wondershare.cn/CPStudio/WOS_Portal.git
#本地回滚代码
git reset --hard ${commit}
#回滚代码到WEB服务器
/bin/sh /usr/local/rsync/portal/portal.sh $model $rollbackID