shell脚本实现git快速提交代码与快速回滚
创建一个commit库:
[[email protected] cml]# echo "check github" > index.html
[[email protected] cml]# cat index.html
check github
[[email protected] cml]# git add index.html
[[email protected] cml]# git commit -m "check github"
[master b357825] check github
1 files changed, 1 insertions(+), 1 deletions(-)
[[email protected] 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 [email protected]:legehappy/cml.git
1a81b88..b357825 master -> master
添加几个标签(这个标签是完好无事的):
[[email protected] cml]# git tag
v1.0
v2.0
v3.0
编写一个快速提交代码与快速回滚的shell脚本:
#!/bin/bash
#author=cml
#[email protected]
#phone=*********
##################################
mail_user="[email protected]"
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 [email protected]: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
测试:发布一个代码使用脚本快速上传和增加标签:
[[email protected] github]# cd cml/
[[email protected] cml]# ls
cml.txt index.html README.md test.txt
[[email protected] cml]# echo "worry index.html" > index.html
[[email protected] cml]# cat index.html
worry index.html
[[email protected] cml]# git add index.html
[[email protected] cml]# git commit -m "worry index.html"
[master 224e010] worry index.html
1 files changed, 1 insertions(+), 1 deletions(-)
[[email protected] 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 [email protected]:legehappy/cml.git
b357825..224e010 master -> master
[[email protected] 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 [email protected]: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
在开发者的服务器上已经同步新的代码:
[[email protected] data]# ls
cml.txt index.html README.md test.txt
[[email protected] data]# cat index.html
worry index.html
而且邮箱收到上传的最新标签:
Reset一:此时发现新的代码存在bug,先回退到上一个版本:
[[email protected] github]# bash deploy.sh reset_one
HEAD is now at b357825 check github
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]: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
[[email protected] github]# cat cml/index.html
check github
这样就实现了git快速上传代码:出现故障时回滚代码的效果:
Reset二:有时候可能会出现最近上传的多个代码都出现bug想要回滚到一个没问题的代码的时候,我们选择第二层回滚:
[[email protected] 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
[[email protected] github]# cat cml/index.html
check github
而且邮件收到你回滚到的版本的标签:
原文地址:http://blog.51cto.com/legehappy/2301031
时间: 2024-10-12 01:07:35