一、jenkins構(gòu)建腳本
dsp_cicd.sh
#! /bin/bash
tag=`date +%Y%m%d%H%M`
user="llsuser"
data_dir="/opt"
ip=$1
port=$2
branch=`echo $GIT_BRANCH | awk -F'/' '{print $2}'`
proj=`echo $JOB_NAME |awk -F - '{print $1}'`
app_type=`echo $JOB_NAME|awk -F - '{print $NF}'`
## 前端應(yīng)用CI
front_ci(){
tar -cf ${JOB_NAME}_${branch}_${tag}.tar.gz dist
ssh -i /root/.ssh/id_rsa_dsp -p ${port} ${user}@${ip} "if [ ! -d ${data_dir}/${proj}/${JOB_NAME} ];then mkdir ${data_dir}/${proj}/${JOB_NAME} -p; fi"
scp -i /root/.ssh/id_rsa_dsp -P ${port} ${JOB_NAME}_${branch}_${tag}.tar.gz ${user}@${ip}:${data_dir}/${proj}/${JOB_NAME}/
}
## java應(yīng)用CI
java_ci(){
jar_name=`find * -name *${app_type}*.jar|grep -Ev 'job-core|executor-samples|api|original|SNAPSHOT|facility|sources|-ec'`
if [ `echo $jar_name|grep -c "jar"` -gt 0 ];then
echo 部署的jar包名為:$jar_name
else
echo "jar包不存在 or jar包名不正確剔交,請核查肆饶!"
exit 1
fi
ssh -i /root/.ssh/id_rsa_dsp -p ${port} ${user}@${ip} "if [ ! -d ${data_dir}/${proj}/${JOB_NAME} ];then mkdir ${data_dir}/${proj}/${JOB_NAME} -p; fi"
scp -i /root/.ssh/id_rsa_dsp -P ${port} $jar_name ${user}@${ip}:${data_dir}/${proj}/${JOB_NAME}/${JOB_NAME}_${branch}_${tag}.jar
}
## 前端應(yīng)用發(fā)布
front_cd(){
echo "--------發(fā)布應(yīng)用--------"
ssh -i /root/.ssh/id_rsa_dsp -p ${port} ${user}@${ip} "bash -x ${data_dir}/${proj}/dsp-deploy.sh ${JOB_NAME} ${data_dir}/${proj}/${JOB_NAME}/${JOB_NAME}_${branch}_${tag}.tar.gz"
}
## java應(yīng)用發(fā)布
java_cd(){
echo "--------發(fā)布應(yīng)用--------"
ssh -i /root/.ssh/id_rsa_dsp -p ${port} ${user}@${ip} "bash -x ${data_dir}/${proj}/dsp-deploy.sh ${JOB_NAME} ${data_dir}/${proj}/${JOB_NAME}/${JOB_NAME}_${branch}_${tag}.jar"
}
main(){
if [ "$app_type" = "front" ]; then
front_ci
front_cd
elif [ "$app_type" = "web" ] || [ "$app_type" = "service" ] || [ "$app_type" = "job" ];then
java_ci
java_cd
fi
}
main
二、發(fā)布腳本
dsp-deploy.sh
#!/bin/bash
source /etc/profile
PROJECT_DIR=$(cd $(dirname $BASH_SOURCE) && pwd)
service_name=$1
deploy_package=$2
proj=`echo $service_name |awk -F - '{print $1}'`
app_type=`echo $service_name|awk -F - '{print $NF}'`
## 前端發(fā)布
deploy_front(){
if test -d ${PROJECT_DIR}/front/${service_name}
then
echo exist
rm -rf ${PROJECT_DIR}/front/${service_name}/*
tar -xf ${deploy_package} -C ${PROJECT_DIR}/front/${service_name}
else
echo "front_workdir ${PROJECT_DIR}/front/${service_name} does't exist."
mkdir -p ${PROJECT_DIR}/front/${service_name}
tar -xf ${deploy_package} -C ${PROJECT_DIR}/front/${service_name}
fi
}
## java應(yīng)用發(fā)布
deploy_java(){
bash /opt/dsp/start.sh $service_name stop
rm -f ${PROJECT_DIR}/jar/${service_name}.jar
ln -s $deploy_package ${PROJECT_DIR}/jar/${service_name}.jar
bash /opt/dsp/start.sh $service_name start
}
main(){
if [ "$app_type" = "front" ]; then
deploy_front
elif [ "$app_type" = "web" ] || [ "$app_type" = "service" ] || [ "$app_type" = "job" ];then
deploy_java
fi
}
main
三岖常、服務(wù)啟動腳本
start.sh
#!/bin/bash
#參數(shù)1:服務(wù)名 參數(shù)2:start stop restart status
source /etc/profile
## 項目目錄
PROJECT_DIR=$(cd $(dirname $BASH_SOURCE) && pwd)
#jar包存放的總目錄,不限制
PROJECT_JAR_ROOT=$PROJECT_DIR/jar
PROJECT_LOG_ROOT=$PROJECT_DIR/logs
##應(yīng)用名
SERVICE_NAME=$1
cd $PROJECT_JAR_ROOT
## 應(yīng)用jar包和日志絕對路徑
JARPATH=$PROJECT_JAR_ROOT/${SERVICE_NAME}.jar
LOGPATH=$PROJECT_LOG_ROOT/${SERVICE_NAME}/${SERVICE_NAME}.log
## jar包啟動相關(guān)參數(shù)
parameter="-Xms512M -Xmx1024M --add-opens java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED -Dio.netty.tryReflectionSetAccessible=true -Ddubbo.reference.check=false -Dfile.encoding=utf-8"
JAR_PARAMS="-Dspring.config.location=${PROJECT_DIR}/config/application-${SERVICE_NAME}.properties -Dlog.path=${PROJECT_DIR}/logs -Dserver.tomcat.basedir=/opt/dsp"
if [ ! -f $JARPATH ];then
echo "不存在此${SERVICE_NAME}應(yīng)用服務(wù)驯镊,請核查!"
echo "參數(shù)1:服務(wù)名 參數(shù)2:start stop restart status"
echo "example:./start.sh exchange-web status"
ls $PROJECT_JAR_ROOT
exit
fi
function start()
{
pidlist=`ps -ef | grep ${JARPATH} | grep -v "grep" | awk '{print $2}'`
if [ "$pidlist" = "" ]
then
nohup /usr/bin/java $parameter ${JAR_PARAMS} -jar ${JARPATH} > /dev/null 2>&1 &
while [ "$pidlist" = "" ];do
pidlist=`ps -ef | grep ${JARPATH} | grep -v "grep" | awk '{print $2}'`
echo "wait......"
sleep 1
done;
echo -e "Starting ${SERVICE_NAME} : \033[32;49;1m [ OK ] \033[39;49;0m"
sleep 1
else
echo -e "${SERVICE_NAME} is already \033[32;49;1m RUNNING... \033[39;49;0m"
fi
}
function stop()
{
pidlist=`ps -ef | grep ${JARPATH} | grep -v "grep" | awk '{print $2}'`
if [ "$pidlist" = "" ]
then
echo -e "${SERVICE_NAME} is already done before... \033[31;49;1m [ DIED ] \033[39;49;0m"
else
kill -9 $pidlist
while [ "$pidlist" != "" ];do
pidlist=`ps -ef | grep ${JARPATH} | grep -v "grep" | awk '{print $2}'`
echo "wait......"
sleep 1
done;
echo -e "Stopping ${SERVICE_NAME}: \033[32;49;1m [ OK ] \033[39;49;0m"
fi
}
function status()
{
pidlist=`ps -ef | grep ${JARPATH} | grep -v "grep" | awk '{print $2}'`
if [ "$pidlist" = "" ]
then
echo -e "${SERVICE_NAME} is \033[31;49;1m NOT RUNNING... \033[39;49;0m"
else
echo -e "${SERVICE_NAME} is \033[32;49;1m RUNNING \033[39;49;0m"
fi
}
case "$2" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage : start | stop | restart | status"
;;
esac