码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本实现git快速提交代码与快速回滚

时间:2018-10-17 12:21:31      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:readme.md   mail   ast   png   dep   app   git add   git push   mail -s   

shell脚本实现git快速提交代码与快速回滚

创建一个commit库:

[root@centos-3 cml]# echo "check github" > index.html

[root@centos-3 cml]# cat index.html

check github

[root@centos-3 cml]# git add index.html

[root@centos-3 cml]# git commit -m "check github"

[master b357825] check github

 1 files changed, 1 insertions(+), 1 deletions(-)

[root@centos-3 cml]# git push

Counting objects: 5, done.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 263 bytes, done.

Total 3 (delta 1), reused 0 (delta 0)

remote: Resolving deltas: 100% (1/1), completed with 1 local object.

To git@github.com:legehappy/cml.git

   1a81b88..b357825  master -> master

添加几个标签(这个标签是完好无事的):

[root@centos-3 cml]# git tag

v1.0

v2.0

v3.0

编写一个快速提交代码与快速回滚的shell脚本:

#!/bin/bash
#author=cml
#mail=406552227@qq.com
#phone=*********
##################################
mail_user="406552227@qq.com"
git_dir=/github
REPOLY="cml"

send_mail_push(){
    echo "push version is $1" | mail -s "deploy push" $mail_user
}

send_mail_roll(){
    echo "roll version is $1" | mail -s "deploy roll" $mail_user
}

git_pull(){
    if [ ! -d $git_dir ];then
        mkdir $git_dir
    fi
        cd $git_dir
    if [ ! -d $REPOLY ];then
        git clone git@github.com:legehappy/cml.git >> /dev/null
    fi
        cd $REPOLY
        git pull
}

git_tag_list(){
    cd $git_dir/$REPOLY
    git_pull
    count=`git tag | wc -l`
    if [ $count -eq 0 ];then
        echo "please take your tag"
    else
        git tag
    fi
}

git_add_deploy(){
    cd $git_dir/$REPOLY
    git_pull
    git_num=$(($(git tag | wc -l)+1))
    git_deloy="v$git_num.0"
    git tag -a $git_deloy -m "$git_deloy"
    git push
    git push --tag
}

delopy(){
    rsync -vzrtopg --progress $git_dir/$REPOLY/* 192.168.5.128:/data/
    cd $git_dir/$REPOLY
    tag_status=$(git tag | tail -n 1)
    echo "$tag_status" 
    send_mail_push $tag_status
    #ssh 192.168.5.128 ln -s /data/ /web
}

check_web(){
    check=`curl -I -m 10 -o /dev/null -s -w %{http_code} 192.168.5.128/index.html`
    if [ $check -eq 200 -o $check -eq 301 -o $check -eq 302 -o $check -eq 304];then
        echo "the web is up"
    else
        echo "please check index.html"
    fi
}

git_set(){
    cd $git_dir/$REPOLY
    select x in "git_reset_HEAD" "git_reset_tag";do
        case $x in
            git_reset_HEAD)
                cd /github/cml
                git reset --hard HEAD^
                git push -f
                git push --tags
                rsync -vzrtopg --progress $git_dir/$REPOLY/* 192.168.5.128:/data/
                cd $git_dir/$REPOLY
                tag_status=$i
                echo "$i" 
                send_mail_roll $i
            ;;
            git_reset_tag)
                cd $git_dir/$REPOLY
                a=`git tag`
                worry_tag=$(git tag | tail -n 1)
                echo "please select which:"
                select i in $a; do
                    if [ $i == "$worry_tag" ];then
                        echo "try again"
                    else
                        echo "you select $i"
                        git reset --hard $i
                        break
                    fi
                done
                git push -f
                git push --tags
                rsync -vzrtopg --progress $git_dir/$REPOLY/* 192.168.5.128:/data/
                cd $git_dir/$REPOLY
                tag_status=$i
                echo "$i" 
                send_mail_roll $i
            ;;
        esac
        break
    done
}

main(){
    git_add_deploy
    delopy
    check_web
}

reset_one(){
    git_reset_HEAD
    check_web
}

reset_second(){
    git_reset_tag
    check_web
}
$1

测试:发布一个代码使用脚本快速上传和增加标签:

[root@centos-3 github]# cd cml/

[root@centos-3 cml]# ls

cml.txt  index.html  README.md  test.txt

[root@centos-3 cml]# echo "worry index.html" > index.html

[root@centos-3 cml]# cat index.html

worry index.html

[root@centos-3 cml]# git add index.html

[root@centos-3 cml]# git commit -m "worry index.html"

[master 224e010] worry index.html

 1 files changed, 1 insertions(+), 1 deletions(-)

[root@centos-3 cml]# git push

Counting objects: 5, done.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 272 bytes, done.

Total 3 (delta 1), reused 0 (delta 0)

remote: Resolving deltas: 100% (1/1), completed with 1 local object.

To git@github.com:legehappy/cml.git

   b357825..224e010  master -> master

[root@centos-3 github]# bash deploy.sh main

Already up-to-date.

Everything up-to-date

Counting objects: 1, done.

Writing objects: 100% (1/1), 151 bytes, done.

Total 1 (delta 0), reused 0 (delta 0)

To git@github.com:legehappy/cml.git

 * [new tag]         v4.0 -> v4.0

sending incremental file list

README.md

           6 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/4)

cml.txt

          10 100%    9.77kB/s    0:00:00 (xfer#2, to-check=2/4)

index.html

          17 100%   16.60kB/s    0:00:00 (xfer#3, to-check=1/4)

test.txt

          11 100%   10.74kB/s    0:00:00 (xfer#4, to-check=0/4)

sent 286 bytes  received 88 bytes  748.00 bytes/sec

total size is 44  speedup is 0.12

v4.0

the web is up

在开发者的服务器上已经同步新的代码:

[root@centos-4 data]# ls

cml.txt index.html README.md test.txt

[root@centos-4 data]# cat index.html

worry index.html

而且邮箱收到上传的最新标签:

技术分享图片

Reset一:此时发现新的代码存在bug,先回退到上一个版本:

[root@centos-3 github]# bash deploy.sh reset_one

HEAD is now at b357825 check github

Total 0 (delta 0), reused 0 (delta 0)

To git@github.com:legehappy/cml.git

 + 224e010...b357825 master -> master (forced update)

sending incremental file list

index.html

          13 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)

sent 135 bytes  received 37 bytes  344.00 bytes/sec

total size is 40  speedup is 0.23

v4.0

the web is up

[root@centos-3 github]# cat cml/index.html

check github

技术分享图片

这样就实现了git快速上传代码:出现故障时回滚代码的效果:

Reset二:有时候可能会出现最近上传的多个代码都出现bug想要回滚到一个没问题的代码的时候,我们选择第二层回滚:

[root@centos-3 github]# bash deploy.sh reset_second

please select which:

1) v1.0

2) v2.0

3) v3.0

4) v4.0

#? 3

you select v3.0

Everything up-to-date

sending incremental file list

sent 82 bytes  received 12 bytes  188.00 bytes/sec

total size is 40  speedup is 0.43

v3.0

the web is up

[root@centos-3 github]# cat cml/index.html

check github

而且邮件收到你回滚到的版本的标签:

技术分享图片

shell脚本实现git快速提交代码与快速回滚

标签:readme.md   mail   ast   png   dep   app   git add   git push   mail -s   

原文地址:http://blog.51cto.com/legehappy/2301031

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!