首先我们要从 上申请一个GitHub仓库。(过程就不介绍了)
现在我们来写个git的自动回滚脚本-通过版本号来回滚代码。
cat /usr/local/src/git.sh#!/bin/bash#author:xiaozhagnmail_user="1179570cab@qq.com"WORK_DIR=/learngit/REPLOY="zrh"SRC_DIR=/data #已存在IP=172.25.0.31GITHUB=git@github.com:xiaozhagn/zrh.gitsend_mail () { echo "$1" | mail -s "Gits current version" $mail_user}git_pull(){ if [ ! -d $WORK_DIR ];then mkdir $WORK_DIR fi}git_add_tag(){ git_pull tag_num=$(($(git tag | wc -l)+1)) tag_deploy="v$tag_num.0" git tag -a $tag_deploy -m "$tag_deploy" git push -f git push --tag}git_deploy(){ rsync -vzrtopg --progress $WORK_DIR/$REPLOY/* $IP:$SRC_DIR cd $WORK_DIR/$REPLOY tag_status=$(git tag | tail -n 1) echo "$tag_status" send_mail $tag_status ssh 172.25.0.31 ln -s $SRC_DIR /web/}git_reset(){ PS3="Select a number: " cd $WORK_DIR/$REPLOY while true ; do read -p "Does reset your version yes/no": input if [ $input == yes ]; then select rollback in "quick rollback" "rollback you need version"; do case $rollback in "quick rollback") git reset --hard HEAD^ git push -f tag1_status=$(git tag | tail -n 1) send_mail $tag1_status echo $tag1_status quit ;; "rollback you need version" ) list_version=$(git tag) select version in $list_version ; do gti reset --hart $version git push -f tag2_status=$(git tag | tail -n 1) send_mail $tag2_status echo $tag2_status break done ;; esac done else if [ $input == no ]; then exit fi if [ $input == * ] ; then echo "Please yes or on" fi fidone}line(){ echo "Please echo \"main\"" echo "If you check your script file please input \"git_add_tag/git_deploy/git_reset again\" " }linemain(){ git_add_tag git_deploy git_reset}$1