赞
踩
本文介绍使用Jenkins一键将SpringBoot应用打包并上传到生产环境服务器运行,这里使用的是直接在服务器上运行FagJar的方式,首先确保服务器环境配置好,安装必须的JDK运行时环境,运行目录,日志存放目录等。
关于JDK的选择,自1.8版本收费之后,我们大多数选择openjdk作为生产环境的运行时环境,和大多数软件安装包一样,在CentOS服务器上,我们可以选择yum方式和下载压缩包的方式进行安装。大多数情况下使用yum方式比较简单方便。也有特殊情况,一些安全要求高单位或系统,只允许在局域网内运行,此时就需要选择压缩包的方式进行安装了。
yum search java | grep -i --color openjdk
[root@localhost ~]# yum search java | grep -i --color openjdk
openjdk-asmtools-javadoc.noarch : Javadoc for openjdk-asmtools
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk.x86_64 : OpenJDK 8 Runtime Environment
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK 8 Demos
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK 8 Development Environment
java-1.8.0-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 8
java-1.8.0-openjdk-headless.x86_64 : OpenJDK 8 Headless Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed
java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-src.x86_64 : OpenJDK 8 Source Bundle
java-11-openjdk.i686 : OpenJDK Runtime Environment 11
java-11-openjdk.x86_64 : OpenJDK 11 Runtime Environment
java-11-openjdk-demo.i686 : OpenJDK Demos 11
java-11-openjdk-demo.x86_64 : OpenJDK 11 Demos
java-11-openjdk-devel.i686 : OpenJDK Development Environment 11
java-11-openjdk-devel.x86_64 : OpenJDK 11 Development Environment
java-11-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 11
java-11-openjdk-headless.x86_64 : OpenJDK 11 Headless Runtime Environment
java-11-openjdk-javadoc.i686 : OpenJDK 11 API documentation
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-11-openjdk-javadoc-zip.i686 : OpenJDK 11 API documentation compressed in a
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in
java-11-openjdk-jmods.i686 : JMods for OpenJDK 11
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.i686 : OpenJDK Source Bundle 11
java-11-openjdk-src.x86_64 : OpenJDK 11 Source Bundle
java-11-openjdk-static-libs.i686 : OpenJDK libraries for static linking 11
java-11-openjdk-static-libs.x86_64 : OpenJDK 11 libraries for static linking
openjdk-asmtools.noarch : To develop tools create proper & improper Java
openprops.noarch : An improved java.util.Properties from OpenJDK
sudo yum install java-11-openjdk.x86_64 -y
[root@localhost /]# java -version
openjdk version "11.0.20" 2023-07-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.20.0.8-1.el7_9) (build 11.0.20+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.20.0.8-1.el7_9) (build 11.0.20+8-LTS, mixed mode, sharing)
[root@localhost /]# which java
/usr/bin/java
[root@localhost /]# dirname $(readlink $(readlink $(which java)))
/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64/bin
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME
[root@localhost /]# source /etc/profile
[root@localhost /]# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
执行完以上操作之后,Java运行时环境就安装配置好了,但是一定要注意:PATH在配置的时候,一定要把$JAVA_HOME/bin放在前面,不然系统中安装有多个版本的JDK,那么在使用java命令时,系统会找到其他版本的Java。还有以前很多教程配置中的CLASSPATH和tools.jar在JDK1.5以后就不需要配置了。
mkdir -p /opt/tmp /opt/bak /opt/script /data/container/server
chmod -R 777 /opt/tmp /opt/bak /opt/script /data/container/server
定义入参,可以通过Jenkins任务将参数传入脚本中,我们定义了下面3个参数:
project_name=gitegg-server : 服务名称
server_port=8080 : 服务端口号
server_path=“/data/container/server” : 服务运行路径
对参数进行检查,是否未传入参数,这里根据自己的实际情况判断,比如必须传入哪些参数,就设置参数的个数不能小于几。
echo "param validate"
if [ $# -lt 1 ]; then
echo "you must use like this : ./publish_gitegg_server.sh [projectname] [server port] [server home dir]"
exit
fi
if [ "$1" != "" ]; then
project_name="$1"
fi
if [ "$2" != "" ]; then
server_port="$2"
fi
if [ "$3" != "" ]; then
server_path="$3"
fi
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
#check server process
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "current :" $server_pid
#shutdown server
kill -9 $server_pid
echo "server shutting down"
while [ -n "$server_pid" ]
do
sleep 5
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "scan server pid :" $server_pid
done
echo "server shutt down"
#publish project
echo "scan no server pid,$project_name publishing"
rm -rf "$server_path"/$project_name*.jar
cp /opt/tmp/$project_name*.jar "$server_path"/$project_name.jar
#bak project
BAK_DIR=/opt/bak/$project_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "$server_path"/$project_name.jar "$BAK_DIR"/"$project_name"_`date +%H%M%S`.jar
#remove tmp
rm -rf /opt/tmp/$project_name*.jar
cd "$server_path"
source /etc/profile
nohup java -jar -Xms4096m -Xmx4096m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m "$project_name".jar --server.port="$server_port" > log.file 2>&1 &
echo "Server is starting,please try to access $container_name conslone url"
project_name=gitegg-server
server_port=8080
server_path="/data/container/server"
#param validate
if [ $# -lt 1 ]; then
echo "you must use like this : ./publish_qxq_server.sh [projectname] [server port] [server home dir]"
exit
fi
if [ "$1" != "" ]; then
project_name="$1"
fi
if [ "$2" != "" ]; then
server_port="$2"
fi
if [ "$3" != "" ]; then
server_path="$3"
fi
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el7_9.x86_64
#check server process
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "current :" $server_pid
#shutdown server
kill -9 $server_pid
echo "server shutting down"
while [ -n "$server_pid" ]
do
sleep 5
server_pid=`/usr/sbin/lsof -n -P -t -i :$server_port`
echo "scan server pid :" $server_pid
done
echo "server shutt down"
#publish project
echo "scan no server pid,$project_name publishing"
rm -rf "$server_path"/$project_name*.jar
cp /opt/tmp/$project_name*.jar "$server_path"/$project_name.jar
#bak project
BAK_DIR=/opt/bak/$project_name/`date +%Y%m%d`
mkdir -p "$BAK_DIR"
cp "$server_path"/$project_name.jar "$BAK_DIR"/"$project_name"_`date +%H%M%S`.jar
#remove tmp
rm -rf /opt/tmp/$project_name*.jar
#start server
cd "$server_path"
source /etc/profile
nohup java -jar -Xms4096m -Xmx4096m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m "$project_name".jar --server.port="$server_port" > log.file 2>&1 &
echo "Server is starting,please try to access $project_name conslone url"
chmod 755 publish_server.sh
/opt/script/publish_server.sh gitegg-application 8182 /data/container/server
立即构建
查看构建日志:点击立即构建之后,下方会出现进度条,点击进度条就可以进入构建日志界面。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。