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

本文介绍了一种利用Shell脚本实现Git代码快速部署及在遇到bug时进行快速回滚的方法。通过创建并调用特定的脚本命令,开发者可以轻松地在本地和远程仓库间同步代码,并在必要时回退到之前的稳定版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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

这样就实现了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快速提交代码与快速回滚

转载于:https://blog.51cto.com/legehappy/2301031

Git指令的Shell脚本,能够快速便捷地管理Git库,包括添加修改、提交修改、显示库状态、推送到远程库、从远程库更新到本地、版本恢复等操作。 使用方法: 1. 在Linux系统中,将本文件放在Git库目录下,利用Shell运行本文件; 2.在windows系统中,需下载安装操作系统相对应的Git软件,并将本文件放在Git库目录下,双击即可运行。 运行示例: Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? h A: Add all changes to repository. C: Commit all changes to repository. D: Show differences between current version with HEAD->. E: Exit shell script. F: Fetch origin/master and merge. L: Show latest two-weeks logs of repository. P: Push commissions to origin/master. U: User command mode(Press ‘Enter’ to exit). R: Reset current version according version_id. S: Show status of repository. Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? s On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: Git.sh modified: PyNote/PyNote_2.md no changes added to commit (use "git add" and/or "git commit -a") Please choose the first letter of options. [Add|Commit|Diff|Fetch|Exit|Help|Log|Push|User|Reset|Status]? a On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD ..." to unstage) modified: Git.sh modified: PyNote/PyNote_2.md
1. 上线前的备份工作: 按日期生成上线记录文件夹 文件结构目录和正式环境保持一致 备份你所涉及到的修改文件 2. 上线前的准备工作: 把你修改到的patch文件 到 PATCH_ROOT 目录中 文件结构目录和正式环境保持一致 3. 上线工作: diff -r -b 比较 备份环境 和 发布环境 文件差异; 再次确定修改是否正确 PATCH_ROOT 下的文件夹和文件 发布到正式环境 即可; 完成上线后,要检查系统功能是否正确。 4. 回滚工作: BAK_ROOT 下的文件夹和文件 回滚到正式环境 即可 ; 完成回滚后 要检查系统功能是否正确回滚。 5. shell脚本功能: === 参数配置 ===: #指定正式环境的路径 正式环境的文件 WEB_ROOT=/var/www/html echo ${WEB_ROOT} #指定发布环境的路径 发布环境的文件 PAT_ROOT=/home/chenlong/blog echo ${PAT_ROOT} #指定发布管理日志的路径 记录发布过程,完成文件备份和文件patch PUB_ROOT=/home/chenlong/Publish === 使用方法 ===: #./apply.sh blog /home/chenlong/patch_sh/list20100520.txt 例如 -bash-3.2$ ./apply.sh blog /home/chenlong/patch_sh/list20100520.txt /var/www/html /home/chenlong/blog /home/chenlong/Publish blog /home/chenlong/patch_sh/list20100520.txt /home/chenlong/patch_sh === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/wp-includes/js/common.js === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/upload_editor.php /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/upload_editor.php /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/upload_editor.php === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/BM/Tpl/default/Admin/Img/index.php /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/BM/Tpl/default/Admin/Img/index.php /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/BM/Tpl/default/Admin/Img/index.php === cmd === :/home/chenlong/patch_sh/apply_cp.sh /home/chenlong/blog/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/patch /bin/cp /home/chenlong/blog/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/patch/home/chenlong/blog/wp-includes/js/common.js === cmd === :/home/chenlong/patch_sh/apply_cp.sh /home/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值