进了新项目,需要对版本号进行批量更新,因此写了个自动化更新脚本,脚本内容如下
#!/bin/bash
WORKSPACE_DIR="/usr/local/workspace/app";
#接收跟新分支
new_branch=$1
#接收更新版本
new_version=$2
if [[ -z $new_branch ]] || [[ -z $new_version ]]; then
echo "更新分支名称(param1:$new_branch)或更新版本(param2:$new_version)为空"
exit 1
fi
declare -A app_arr
app_arr["app1"]="git@192.168.0.1:XXXXX"
old_version=""
#切换到父级工程目录
if [ ! -d $WORKSPACE_DIR ]; then
echo "目录不存在,创建新目录:$WORKSPACE_DIR"
mkdir $WORKSPACE_DIR
fi
cd $$WORKSPACE_DIR
if [ "$(pwd)" != "$WORKSPACE_DIR" ]; then
echo "工程目录切换失败:$WORKSPACE_DIR"
exit 1
fi
#git clone
for key in ${!app_arr[@]}
do
temp_dir="$WORKSPACE_DIR/$key"
if [ ! -d $temp_dir ]; then
echo "$temp_dir目录不存在,开始执行git克隆"
echo "执行命令:git clone ${app_arr[$key]}"
/usr/bin/git clone ${app_arr[$key]}
if [ $? -ne 0 ]; then
echo "git clone ${app_arr[$key]}命令执行失败,当前目录:$(pwd)"
exit 1
fi
fi
done
#切换指定分支,修改对应版本号
for key in ${!app_arr[@]}
do
#切换工程目录
temp_dir="$WORKSPACE_DIR/$key"
cd $temp_dir
if [ "$(pwd)" != "$temp_dir" ]; then
echo "目录切换失败,当前目录:$(pwd),目标目录:$temp_dir"
exit 1
fi
echo "已切换到新工程目录:$(pwd)"
#判断当前分支是不是目标分支,不是则切换分支
current_branch=$(/usr/bin/git branch | grep \* | cut -d ' ' -f2)
if [ "$current_branch" != "$new_branch" ]; then
echo "当前分支:$current_branch,目标分支:$new_branch,需要切换分支"
#判断本地是否存在目标分支
local_branch=$(/usr/bin/git branch | grep $new_branch)
if [[ -z $local_branch ]]; then
#从远程加载分支到本地
echo "本地没有分支,开始从远程加载到本地"
/usr/bin/git checkout -b "$new_branch" "remote/$new_branch" >/dev/null 2>&1
current_branch=$(/usr/bin/git | grep \* | cut -d ' ' f2)
if [ "$current_branch" != "$new_branch" ]; then
echo "加载远程分支到本地失败,/usr/bin/git checkout -b $new_branch remote/$new_branch"
echo "当前分支为:$current_branch,目标分支为:$new_branch"
exit 2
else
echo "加载远程分支到本地成功,/usr/bin/git checkout -b $new_branch remote/$new_branch"
echo "当前分支为:$current_branch,目标分支为:$new_branch"
fi
else
#切换本地分支
echo "本地有目标分支,切换到目标分支"
/usr/bin/git checkout $new_branch >/dev/null 2>&1
current_branch=$(/usr/bin/git | grep \* | cut -d ' ' f2)
if [ "$current_branch" != "$new_branch" ]; then
echo "切换本地分支失败,/usr/bin/git checkout -b $new_branch remote/$new_branch"
echo "当前分支为:$current_branch,目标分支为:$new_branch"
exit 2
else
echo "切换本地分支成功,/usr/bin/git checkout -b $new_branch remote/$new_branch"
echo "当前分支为:$current_branch,目标分支为:$new_branch"
fi
fi
else
echo "当前分支:$current_branch,目标分支:$new_branch,不需要切换分支"
fi
#查找pom.xml文件路径
pom_path=$(find $temp_dir -name "pom.xml")
#查找旧分支名称
if [[ -z $old_version ]]; then
for file in $pom_path
do
res=$(cat $file|grep "</version>")
for version_no in $res
do
version_no=$(echo $version_no | sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g' | sed -e 's/<[\/]*version>//g')
read -t 10 -p "被替换版本号是否为:$version_no(yes/no/quit):" input
if [ "yes" == $input ];then
old_version=$version_no
break
fi
if [ "quit" == $input ]; then
echo "程序执行结束"
exit 1
fi
done
if [[ -n $old_version ]]; then
echo "将被替换版本号为:$old_version"
break
fi
done
fi
if [[ -n $old_version ]]; then
for file in $pom_path
do
sed -i 's:'$old_version':'$new_version':g' $file
echo "$file => 文件版本已替换$old_version->$new_version"
done
fi
echo -e "------------------------$key--------------------------\n"
done
使用demo
./version_replace.sh ${branch} ${version}