微服務專案Git倉庫自動化指令碼

易墨發表於2022-12-20

說明

基於微服務專案,產生的的多專案倉庫管理指令碼。可直接儲存 shell 指令碼後酌情修改後試用

目錄結構

  • xxxx
    • Xxx1Api/
    • Xxx2Api/
    • git_clone_api.sh
    • git_branch_dev.sh
    • git_pull_all.sh
    • git_status.sh
    • api-build-tag.sh
    • api-commit-push.sh
    • api-dev-release-merage.sh
    • api-update-nuget-pack.sh

指令碼放置在外層目錄,將操作 Api 字尾目錄下 git 倉庫

批次克隆倉庫

#!/bin/bash
# 不要放在中文路徑下
# 介面
git clone http://xxxxx.com/XXX.XXXApi.git
read pause

切換分支到 dev

#!/bin/bash
function getdir(){
    for element in `ls $1 | grep /$`
    do
        dir_or_file=$1"\\"$element
		echo $dir_or_file
		cd $dir_or_file
		git checkout $2
		git pull
		cd ..
    done
}
root_dir="./"
branch=dev
getdir $root_dir $branch

拉取分支最新程式碼

#!/bin/bash
function getdir(){
    for element in `ls -F $1 | grep /$`
    do
        dir_or_file=$1"\\"$element
		echo $dir_or_file
        if [ $element = "docs" ]
        then
            echo $dir_or_file skip
        else
           cd $dir_or_file
		   git pull
		   cd ..
        fi
    done
}
root_dir="./"
getdir $root_dir

檢視倉庫狀態

#!/bin/bash
function getdir(){
    for element in `ls $1 | grep /$`
    do
        dir_or_file=$1"\\"$element
		echo $dir_or_file
		cd $dir_or_file
		git status
		cd ..
    done
}
root_dir="./"
getdir $root_dir
read pause

自動升級 dev 的 nuget 包

  • 專案: 匹配的文字
  • *Api:Api 字尾的目錄
  • XXX: 需要更新的指定包,匹配包名
  • xxxxx.com:nuget 源
#!/bin/bash
#set -x
echo '自動升級nuget包-Need Setting shell to GBK Encoding'
function upgradePack(){
	tempFile=./temp.txt
	tempPackFile=./tempPack.txt
	echo 當前目錄:$1
	cd $1
	cd src
	pwd
	#read pause
	dotnet restore
	dotnet list package --source xxxxxxx.com   --include-prerelease  --outdated>$tempFile
	tempProjectMatch="專案"
	projectName=''
	cat $tempFile | while read line
	do
		#if  [[ "$line" == *XXX* ]];then
			if  [[ $line =~ $tempProjectMatch ]];then
				echo $line | grep -Eo "XXX.((\w)+(\.?))+">$tempPackFile
				projectName=$(cat $tempPackFile)
				echo 檢測專案:$projectName
			else
				if [[ "$line" == *XXX* ]];then
					echo $line | grep -Eo "XXX.((\w)+(\.?))+">$tempPackFile
					packageName=$(cat $tempPackFile)
					echo 升級包:$packageName
					dotnet add $projectName/$projectName.csproj package $packageName
				fi
			fi

		#fi
	done
	rm $tempFile
	rm $tempPackFile
}
function getdir(){
	branchName=$2
    for element in `ls $1 | grep /*Api`
    do
        dir_or_file=$1/$element
		cd $dir_or_file
		if([ "$branchName" != "" ]);then
			git checkout $branchName
			git pull
		fi
		upgradePack $dir_or_file
    done
}

branch=dev
root_dir=$(cd `dirname $0`;pwd)
#echo 指令碼目錄:$root_dir
getdir $root_dir $branch
read pause

將 dev 分支打 tag:vyyyyMMdd 並推送到 origin

#!/bin/bash
#set -x
echo '重新命名分支-Need Setting shell to GBK Encoding'
function pushTag(){
	echo 拉取dev分支
	git checkout dev
	echo 建立tag
	time_span=v`date +%Y%m%d`
	git tag -l $time_span
	git tag -a -f -m relrease $time_span
	echo 推送tag
	git push --set-upstream origin $time_span -f
	echo 推送完畢
}
function getdir(){
    # 資料夾名匹配
    for element in `ls $1 | grep -E 'XXXApi|YYYYApi'`
    do
        dir_or_file=$1/$element
		cd $dir_or_file
		pushTag $dir_or_file
    done
}

root_dir=$(cd `dirname $0`;pwd)
#echo 指令碼目錄:$root_dir
getdir $root_dir
read pause

提交 dev 分支並推送

#!/bin/bash
#set -x
echo '自動提交-Need Setting shell to GBK Encoding'
function pushCode(){
	echo 當前目錄:$1
	git add *
	git commit -m 更新包
	git pull
	git push
}
function getdir(){
	branchName=$2
    for element in `ls $1 | grep /*Api`
    do
        dir_or_file=$1/$element
		cd $dir_or_file
		pushCode $dir_or_file
    done
}

branch=dev
root_dir=$(cd `dirname $0`;pwd)
#echo 指令碼目錄:$root_dir
getdir $root_dir $branch
read pause

合併 dev 分支到 release

#!/bin/bash
#set -x
echo '重新命名分支-Need Setting shell to GBK Encoding'
function pushTag(){
	echo 當前目錄:$1 $branch
	echo 拉取dev分支
	git checkout dev
	echo 建立tag
	time_span=v`date +%Y%m%d`
	git tag -l $time_span
	git tag -a -f -m relrease $time_span
	echo 推送tag
	git push --set-upstream origin $time_span -f
	echo 推送完畢
}
function pushCode(){
	echo 當前目錄:$1 $branch
	echo 切換到dev,開始合併
	git checkout dev
	git pull
	echo 刪除release分支
	git branch -d release
	echo 新建release分支
	git checkout -b release
	echo 推送新的release分支
	git push --set-upstream origin release -f
}
function getdir(){
    for element in `ls $1 | grep /*Api`
    do
        dir_or_file=$1/$element
		cd $dir_or_file
		pushTag $dir_or_file
		pushCode $dir_or_file
		echo 睡眠30秒
		sleep 30s
    done
}

root_dir=$(cd `dirname $0`;pwd)
#echo 指令碼目錄:$root_dir
getdir $root_dir
read pause

相關文章