Implementation of git Quick Submitting Code and Quick Rollback by shell script

Keywords: Linux git CentOS github rsync

Create a commit library:

[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

Add a few tags (this tag is perfect):

[root@centos-3 cml]# git tag

v1.0

v2.0

v3.0

Write a shell script that quickly submits code and rolls back:

#!/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

Test: Publish a code to upload and add tags quickly using scripts:

[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

New code has been synchronized on the developer's server:

[root@centos-4 data]# ls

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

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

worry index.html

And the mailbox receives the latest tag uploaded:

Reset 1: When a bug is found in the new code, go back to the previous version.

[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

This enables git to quickly upload code: rollback code in case of failure:

Reset 2: Sometimes there may be bug s in many recently uploaded codes. When we want to roll back to a good code, we choose the second rollback:

[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

And the email receives the label of the version you rolled back to:

Posted by radhoo on Fri, 01 Feb 2019 08:39:15 -0800