jenkins 如何发布用java -jar来启动的java项目

对于一些jenkins无法管理的项目,该如何做自动发布?

最近遇到了一些使用jenkins无法发布的java项目,比如用nohup java -jar xxx.jar & 启动的项目,在用jenkins发布时会遇到进程夯住的情况。
所以我们需要把对应的项目做成linux下的服务,可以做成systemd管理的服务。这样我们在发布完项目之后只需执行systemctl restart xxx即可。

如xxl-job项目

  1. 首先我们需要写好脚本
    cat /opt/shell/manage-service/restart-jar-service.sh
#!/bin/bash
# 专门用来管理老的用java -jar的方式启动服务,解决无法通过jenkins 服务重启该服务的问题
# author: Yangning
# datetime: 20210816
# 如
# jarName="xxl-job-admin-2.3.0-SNAPSHOT.jar"
# workDir="/data/server/dubbo-service/xxl-job/"
if [[ "$#" -ne 3 ]];then
	echo "total required three parameters"
	echo "Usage: $0 service/work/directory jarname start|stop|status)"
	exit 1
fi
if [[ ! -d "$1" ]] || [[ ! -f "$1/$2" ]];then
	echo "$1 is not directory or $1$2 is not file"
	exit 2
fi
if [[ "$3" != "start" ]] && [[ "$3" != "stop" ]] && [[ "$3" != "status" ]];then
	echo "Third parameter is error, $3 must be start|stop|status"
	exit 3
fi
source /etc/profile
workDir=$1
jarName=$2
action=$3



set -x
function start (){
    start_check_pid=$(ps -ef|grep -v grep|egrep "java\s+-jar\s+$jarName"|awk '{print $2}')
	if [[ "$start_check_pid" == "" ]];then
		echo $(which java)
		cd $workDir && java -jar "$jarName" > /dev/null 2>&1 &
		start_pid=$(ps -ef|grep -v grep|egrep "java\s+-jar\s+$jarName"|awk '{print $2}')
		if [[ "$start_pid" != "" ]];then
			echo "$jarName start success, runing... PID $start_pid"
		else
			echo "$jarName start failed"
		fi
	else
		echo "$jarName is Already start, running PID $start_check_pid"
	fi
}

function stop (){
    stop_check_pid=$(ps -ef|grep -v grep|egrep "java\s+-jar\s+$jarName"|awk '{print $2}')
	if [[ "$stop_check_pid" == "" ]];then
		echo "$jarName Already stoped"
	else
		kill -9 $stop_check_pid
		stop_pid=$(ps -ef|grep -v grep|egrep "java\s+-jar\s+$jarName"|awk '{print $2}')
		if [[ "$stop_pid" == "" ]];then
			echo "$jarName stoped success, PID $stop_check_pid is gone "
		else
			echo "$jarName stop failed"
		fi
	fi
}

# 这个函数在systemd中没有用到
function status (){
	server_status=$(ps -ef|grep -v grep|egrep "java\s+-jar\s+$jarName")
	echo $server_status
	if [[ "$server_status" == "" ]];then
		echo "$jarName died..."
	else
		echo "$jarName running..."
	fi
}

case $action in
  start)
  	start
  	;;
  stop)
  	stop
    ;;
  restart)
  	stop
	start
    ;;
  status)
  	status
    ;;
  *)
  exit
      ;;
esac
  1. 做成systemd管理的服务
    cat /usr/lib/systemd/system/xxl-job.service
[Unit]
Description=xxl-job server
Wants=network-online.target
After=network.target

[Service]
Type=forking
WorkingDirectory=/opt/shell/manage-service/
ExecStart=/bin/bash restart-jar-service.sh /data/server/dubbo-service/xxl-job/ xxl-job-admin-2.3.0-SNAPSHOT.jar start
ExecStop=/bin/bash restart-jar-service.sh /data/server/dubbo-service/xxl-job/ xxl-job-admin-2.3.0-SNAPSHOT.jar stop
ExecReload=/bin/bash restart-jar-service.sh /data/server/dubbo-service/xxl-job/ xxl-job-admin-2.3.0-SNAPSHOT.jar restart
Restart=on-failure

[Install]
WantedBy=multi-user.target
posted @ 2021-08-16 16:29  早晨我在雨中采花  阅读(720)  评论(0编辑  收藏  举报