Centos 7.2 Jenkins+Ansible+Gitlab 部署maven项目

项目结构

多模块

 

 

 单模块

 

 

 

jenkins效果

  • 构建
    •  

       

  • 发布

基础配置

mkdir -p /data/dubbo/{app,logs,cache} 
&& useradd tomcat
&& chown tomcat:tomcat -R /data/dubbo/
mkdir /data/scripts
vim jar_init
#!/bin/bash
#
# ucservice     start ucservice

. /etc/rc.d/init.d/functions
. /etc/profile

PROJ_NAME=$3
PKG=${PROJ_NAME}.jar
PORT=$2
USER="tomcat"

[ -n "$PORT" ] && JMX_PORT=$((PORT+10000))
HEAP="2048m"

APP_PATH="/data/dubbo/app/${PROJ_NAME}_${PORT}"
LOG_PATH="/data/dubbo/logs/${PROJ_NAME}_${PORT}"
TMP_PATH="/data/dubbo/cache/${PROJ_NAME}_${PORT}"
PID="${LOG_PATH}/${PROJ_NAME}_${PORT}.pid"
LOG_LEVEL="info" 
STDOUT=0
START_PARS="-Xms${HEAP} -Xmx${HEAP} -Xmn512m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heapdump.hprof"
LOG_PARS="--log.home=${LOG_PATH} --log.level=${LOG_LEVEL} --log.stdout=${STDOUT}"
TMP_PARS="--server.tomcat.basedir=${TMP_PATH}"
[ -n "$JMX_PORT" ] && JMX_PARS="--jmx.rmi.port=${JMX_PORT}"

function start()
{
    if [ -s $PID ];then
        echo "${PROJ_NAME} on port ${PORT} is already running...."
        exit 0
    else
        [ -d $APP_PATH ] && chown -R $USER:$USER $APP_PATH
        [ -d $LOG_PATH ] || mkdir -p $LOG_PATH && chown -R $USER:$USER $LOG_PATH
        [ -d $TMP_PATH ] || mkdir -p $TMP_PATH && chown -R $USER:$USER $TMP_PATH
        [ ! -f ${APP_PATH}/${PKG} ] && echo "file ${APP_PATH}/${PKG} not found!" && exit 4
        su - $USER -c "nohup java -jar ${START_PARS} ${APP_PATH}/${PKG} --server.port=${PORT} ${TMP_PARS} ${LOG_PARS} ${JMX_PARS} >> /dev/null 2>&1 &"
        pid=`ps -ef|grep java|grep "server.port=${PORT}"|grep -v grep|awk '{print $2}'`
        #pid=$!
        #count=`ps -ef|grep java|grep $pid |grep -v grep|wc -l`
        sleep 1
        if [ -d /proc/$pid ];then
            su - $USER -c "echo $pid > $PID"
            echo -n "Start ${PROJ_NAME} on port ${PORT}"
            success
            echo
        else
            echo -n "Start ${PROJ_NAME} on port ${PORT}"
            failure
            echo
        fi
    fi
}

function stop()
{
    if [ -s $PID ];then
        pid=`cat ${PID}`
        killproc -p $PID
        if [ ! -d /proc/$pid ];then
            echo -n "Stop ${PROJ_NAME} on port ${PORT}"
            success
            echo
        else
            echo -n "Stop ${PROJ_NAME} on port ${PORT}"
            failure
            echo
        fi
    else
        echo "${PROJ_NAME} on port ${PORT} is not running...."
    fi
}

function status()
{
    [ -s $PID ] && echo "${PROJ_NAME} on port ${PORT} is running!" || echo "${PROJ_NAME} on port ${PORT} is stopped!"
}

function restart()
{
    stop
    start
}

case $1 in
    start)
        start;;
    stop)
        stop;;
    restart)
        restart;;
    status)
        status;;
    *)
        echo "USAGE:$0 {start|stop|restart|status}"
esac

 

构建通用配置

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

单模块

# 说明: 此脚本用于将打包好的jar包存放到归档文件夹下

# ************************定义参数************************************* #
## 时间
timestamp=`date +%F-%H%M%S`

## jar包归档路径 归档文件夹/项目名称/时间戳_版本号
ARC_FOLDER_DETAIL=${ARC_FOLDER}/${APP_NAME}/${timestamp}_V${BUILD_ID}

## jenkins工作区中的项目路径
MAV_JAR_FILE=${WORKSPACE}

# *************************处理服务jar包************************* #
JAR_NAME=$MAV_JAR_FILE/target/${APP_NAME}.jar

  echo "正在复制$JAR_NAME到$ARC_FOLDER_DETAIL"

  #创建归档子文件夹
  mkdir $ARC_FOLDER_DETAIL -p

  #复制文件夹到归档子文件夹
  cp $JAR_NAME $ARC_FOLDER_DETAIL/

多模块

# 说明: 此脚本用于将打包好的jar包存放到归档文件夹下

# ************************定义参数************************************* #
## 时间
timestamp=`date +%F-%H%M%S`

## jar包归档路径 归档文件夹/项目名称/时间戳_版本号
ARC_FOLDER_DETAIL=${ARC_FOLDER}/${APP_NAME}/${timestamp}_V${BUILD_ID}

## jenkins工作区中的项目路径
MAV_JAR_FILE=${WORKSPACE}



# *************************循环处理各个服务jar包************************* #
#只处理根目录下的文件夹,以及文件夹名字与jar包名字相同的jar包

for file in  $MAV_JAR_FILE/*
do

   if [ -d "$file" ]
   then
       echo  "正在处理服务文件夹:" $file 
       
       #拼接需要的jar文件
       basename=`basename $file`
       JAR_NAME=$file/target/${basename}.jar
       
       #只复制需要的jar文件
       if [ -a "$JAR_NAME" ]
       then
           echo "正在复制$JAR_NAME到$ARC_FOLDER_DETAIL/$basename"
           
           #创建归档子文件夹
           mkdir $ARC_FOLDER_DETAIL/$basename -p
           
           #复制文件夹到归档子文件夹
           cp $JAR_NAME $ARC_FOLDER_DETAIL/$basename/
       fi
   fi
done

发布基本配置

 

 

 

 

 

 

 

 

 

def cmd='cd ' + ARC_FOLDER + '/'+ PROJ_NAME +  '; ls -r  | head -n 10 '

def ver_keys = [ 'bash', '-c', cmd ]

ver_keys.execute().text.tokenize('\n')

 

 

 

 开始推送

 

 

多模块

delete=true group=no owner=no 
src=${ARC_FOLDER}${PROJ_NAME}/${Release_Version}/${APP_NAME}/${APP_NAME}.jar  dest=${target_dir}/${APP_NAME}_${port}/
单模块

delete=true group=no owner=no 
src=${ARC_FOLDER}${APP_NAME}/${Release_Version}/${APP_NAME}.jar  dest=${target_dir}/${APP_NAME}_${port}/

 

posted @ 2020-01-19 16:26  mickey007  阅读(328)  评论(0编辑  收藏  举报