分享一个自动化部署jar的sh脚本,包括了拉去源码,切换分支,构建项目,停止和运行

#!/bin/bash

启动方式 bash run.sh




filePath="./jars"
mvnPath="/opt/maven/apache-maven-3.9.3/bin/mvn"
jar_array=($(find "$filePath" -type f -name "*.jar" -exec basename {} \;))
 


echo "-----jar列表-----"
for i in "${!jar_array[@]}"; do
  printf "%s. %s\n" "$i" "${jar_array[$i]}"
done
read -p "请选择app num > " app_num

jar=${jar_array[$app_num]}
app=$(echo "$jar" | sed 's/.jar//')

cd "$app"
# 选分支
branches=($(git branch -a --format='%(refname:short)' | sed -n '/origin*/p'   | sed 's/origin\///'))
echo "----分支列表----"
for i in "${!branches[@]}"; do
  printf "%s. %s\n" "$i" "${branches[$i]}"
done
git fetch --all
read -p "请选择分支 num > " branch_num
branch=${branches[$branch_num]}
git checkout ${branch}

read -p "是否获取最新代码: $app [y/n] > " isorigin
if [[ $isorigin == "y" ]]; then
  git pull origin ${branch}
fi

#编译并移动jar包
read -p "是否编译项目: $app [y/n] > " compile
if [[ $compile == "y" ]]; then
  $mvnPath clean install -DskipTests
  cp "./target/$jar" "../jars" 
fi
cd ".."


# 选择环境
echo "-----环境列表-----"
profile_array=('dev' 'prod' 'test')
for i in "${!profile_array[@]}"; do
  printf "%s. %s\n" "$i" "${profile_array[$i]}"
done

read -p "请选择环境 num > " p_num
profile=${profile_array[$p_num]}

echo "开始启动[$profile]环境项目: [$app] "

# 服务重启
LOG_FILE=${app}.log

pid=$(ps -ef | grep ${app} | grep "java" | awk '{print $2}')
if [[ -n "$pid" ]]; then
  kill -9 ${pid}
  echo "关闭项目: ${app}成功!"
fi

nohup java -jar "$filePath/$jar" --spring.profiles.active=$profile >${LOG_FILE} &
echo "[${profile}] 环境项目 [${app}] 启动成功!"
tail -100f ${LOG_FILE}

你可能感兴趣的:(自动化,jar,运维)