jenkins启动springboot脚本

6821人浏览 / 0人评论

注意:Jenkins项目名字和代码项目名字一致

需要提前定义好的变量:

key value
APP_JAR   es1-1.0-SNAPSHOT.jar
DEST /release
APP_NAME eureka-server1
PORT 18761
ENV test
MEM 2g
IPS 192.168.31.244,192.168.31.243

 

 

Jenkins目录:/template

目标服务器目录:/scripts  /release

部署文件:start.yml

---
- hosts: "{{host}}"
  remote_user: root
  tasks:
    - name: 复制程序包
      copy:
        src: "{{jarsrc}}"
        dest: "{{jardest}}"
    - name: 复制脚本
      copy:
        src: "{{scriptsrc}}"
        dest: "{{scriptdest}}"
    - name: 执行脚本
      shell: sh /scripts/start.sh {{jar}} {{mem}} {{env}}

部署脚本:deploy.yml

#!/bin/bash
IPS=$1
APP_JAR=$2
DEST=$3
MEM=$4
ENV=$5
WORKSPACE=$6
APP_NAME=$7
PORT=$8
array=(${IPS//,/ })
for IP in ${array[@]}
do
   echo '开始部署服务器:'$IP
   ansible-playbook /template/start.yml -e "host="$IP" name="$APP_JAR" jar="$DEST"/"$APP_JAR" mem="$MEM" env="$ENV" jarsrc="${WORKSPACE}"/"$APP_NAME"/target/"$APP_JAR" jardest="$DEST"/"$APP_JAR" scriptsrc=/template/start.sh scriptdest=/scripts/start.sh"
   sh /template/check.sh $IP $PORT $APP_JAR
done

启动脚本:start.sh

#!/bin/bash

APP_JAR=$1
MEM=$2
ENV=$3


PIDS=`ps -ef |grep "$APP_JAR" |grep java |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
kill -9 $PIDS 
echo "$APP_JAR 已被杀死"
else
echo '程序未运行'
fi

JAVA_MEM_OPTS=" -server -Xms"${MEM}" -Xmx"${MEM}" -XX:+UseParallelGC "
nohup java   $JAVA_MEM_OPTS   -jar $APP_JAR $1 --spring.profiles.active=$ENV &
echo "程序启动中。。。"

检查脚本:check.sh

#!/bin/bash
IP=$1
PORT=$2
APP_JAR=$3

for(( i=1;i<=100;i=i+1))
do
if [ `curl -I -m 10 -o /dev/null -s -w %{http_code} "$IP":"$PORT"/info` = 200 ]; then
	echo "项目"${APP_JAR}"在服务器"${IP}"启动成功"
    exit 0
fi
echo "==================访问http://"${IP}":"${PORT}"第$i次======================================="
sleep 1
done
echo "======================启动失败============================================="
exit 1

 

编译命令:

mvn clean package -Dmaven.test.skip=true
echo '编译完成'

启动命令:

sh /template/deploy.sh $IPS $APP_JAR $DEST $MEM $ENV ${WORKSPACE} $APP_NAME $PORT

 

全部评论