創建一個commit褲先:
1、[root@centos-3 cml]# echo "check github" > index.html
[root@centos-3 cml]# cat index.html
check github
2、[root@centos-3 cml]# git add index.html
3、[root@centos-3 cml]# git commit -m "check github"
[master b357825] check github
1 files changed, 1 insertions(+), 1 deletions(-)
4、[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 [email protected]:legehappy/cml.git
1a81b88..b357825 master -> master
添加幾個標籤(這個標籤是完好無事的):
[root@centos-3 cml]# git tag
v1.0
v2.0
v3.0
編寫一個快速發送代碼與快速回滾的shell腳本:
[root@centos-3 github]# cat deploy.sh
#!/bin/bash #author=cml #mail=*******@qq.com #phone=********* ################################## mail_user="********@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 [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
測試:發佈一個代碼使用腳本快速上傳和增加標籤:
[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 [email protected]: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 [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
在開發者的服務器上已經同步新的代碼:
[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 [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
[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
而且郵件收到你回滾到的版本的標籤: