- 多年前的一段简简单单的shell脚本代码却承载着一段成长历程
-
## ## git命令半自动化脚本 ## ## 代码冲突时,脚本不能帮忙解决冲突,将停止执行## ## 适用场景:在开发分支提交代码## 最佳适用场景:提测后有些小问题修改后立马提测## 此文件放在项目根目录## run: ./.sh## 运行脚本的命令 ./.sh 可以在bash配置文件里配置成 简写 gp ( alias gp='./.sh')set -e #一旦出现了返回值非零,整个脚本就会立即退出devBranch='zulin1.3.8.2' #初始化变量的值testBranch=develop #初始化变量的值currBranch=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3` #获取当前分支名字 echo $branchName; input1='' #设置 input1 变量值为空 传给commit -minputPush='' #设置 inputPush 变量值为空 inputDevelop='' #设置 inputPush 变量值为空if [ $currBranch != $testBranch ] thendevBranch=$currBranch #初始化变量的值 echo -e "\e[32m current branch:$currBranch \e[0m" git add . echo -e "\e[36m $currBranch : add. over \e[0m" echo '...' echo '检查状态:' git status echo '...' #until 循环,当 input1 变量的值为 exit 时退出该循环 until [ "$input1" != '' ] do #echo -e "\e[33m Please enter commit message: \e[0m" echo -e "\e[33m 请输入commit message: \e[0m" #读取键盘输入的数据 read input1 #输入的不是 '' 时把用户输入的数据指向commit message if [ "$input1" != '' ] then #输出变量 input1 的值 git commit -m "$input1" echo '...' echo -e "\e[36m $currBranch : commit over \e[0m" echo '检查状态:' echo '...' git status else echo 'Exit the script.' fi done echo "Pulling code " git pull --rebase #与远程代码同步,同步过程会检查冲突 git fetch + git rebase FETCH_HEAD echo "..." if [ $? -eq 0 ]; then echo -e "\e[36m $currBranch : pull over \e[0m" #read -r -p "Do you need to push to the remote repository [Y/n]: " inputPush read -r -p "是否需要push到$devBranch [Y/n]: " inputPush case $inputPush in [yY][eE][sS]|[yY]) echo "Pushing code " echo "..." git push if [ $? -eq 0 ]; then echo "..." echo -e "\e[36m $currBranch : push over \e[0m" #read -r -p "Do you need to merge into the develop-branch [Y/n]: " inputDev read -r -p "当前开发分支$devBranch的commit未提测, 是否提测? [Y/n]: " inputDev case $inputDev in [yY][eE][sS]|[yY]) git checkout $testBranch currentBr=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3` echo -e "\e[30;46m current branch $currentBr \e[0m" git pull echo "..." echo -e "\e[36m $currentBr : pull over \e[0m" echo -e "\e[36m 即将执行 git merge $devBranch, 如有代码冲突脚本自动停止,请自行解决冲突 \e[0m" git merge $devBranch echo "..." echo -e "\e[36m $currentBr : merge over \e[0m" echo -e "\e[36m 代码已经合并到 $currentBr 分支,即将执行git push \e[0m" echo "Pushing code " echo "..." git push echo "..." echo -e "\e[36m $currentBr : push over \e[0m" echo "检查状态:" git status echo "..." git checkout $devBranch echo -e "\e[36m $currBranch : 提测完毕,已经切换开发分支 \e[0m" exit 0 ;; [nN][oO]|[nN]) echo -e "\e[36m $currBranch : 当前代码未提测 \e[0m" echo "检查状态:" git status ;; *) echo "..." exit 0 ;; esac else exit 1 echo -e "\e[41m Push code failed: Please check the network ( run: git status) \e[0m" fi ;; [nN][oO]|[nN]) echo "code not push" git status ;; *) echo "..." exit 0 ;; esac else echo -e "\e[31m $currBranch : Pull code failed: Please check the network or resolve code conflicts \e[0m" fielse echo -e "\e[41m script execution error : Please check the current branch (请检查分支,此程序只建议在开发分支执行) \e[0m" echo -e "\e[37m End of script execution \e[0m"fi$?
3. 执行构建等一系列git命令、然后推到远端让服务端利用此构建包进行服务部署