该版本为2.121.1,其他版本会稍有不同,仅做参考
1.登录jenkins后,点击左上角“新建任务”。
2.填写任务名称,然后选择构建类型(一般是选择构建一个maven项目)
3.填写General模块下信息
(1).填写描述
(2).选择 丢弃旧的构建
4.填写源码管理模块下信息
(1).选择git或Subversion,看自己项目用的git或者svn (下面是svn的配置)
(2).Repository URL : 项目svn的路径
Credentials : 如果有就随便选一个,没有就创建一个
其他的不用管,默认的就可以了
5.填写Build模块下信息
Root POM :pom.xml
Goals and options :clean install
6.填写Post Steps模块下信息
(1).选择 Run regardless of build result选项。
(2). 点击add post-build step,选择send files execute commands over SSH
Name : 发布的项目所在的服务器地址
Source files :shop-web/target/*.jar
Remove prefix :shop-web/target
Remote directory :/new-shop/
Exec command :chmod 777 /opt/new-shop/*.jar
(3).点击add server
Name : 发布的项目所在的服务器地址
Source files: sh/node-*.sh
Remove prefix : sh
Remote directory : /new-shop/
Exec command : chmod 777 /opt/new-shop/*.sh
(4). 点击 add post-build step,选择 execute shell script on remote host using ssh
SSH site : root@服务器地址:22
Command :/opt/new-shop/node-app.sh stop
cd /opt/new-shop/
BUILD_ID=dontKillMe ;
nohup /opt/new-shop/node-app.sh start>nohup.out &
7.点击保存
然后再服务器里面new-shop(这个目录是存放项目jar包的目录)目录下创建node-app.sh文件,文件内容如下:
#!/bin/sh
# 函数名
f=$1
# get basepath in parament
current=$(dirname $0)
cd ${current}
# 获取应用全路径
function get_full_app_path(){
# statememnt app deployment path
app_deploy_path=$1
max_time=0
max_time_file=""
for FILE in $app_deploy_path/*.jar
do
modify_date=`date +%s -r ${FILE}`
if test $[$modify_date] -gt $[$max_time]
then
max_time=$modify_date
max_time_file=$FILE
fi
done
echo "$max_time_file"
}
# 获取应用名称
function get_app(){
# get app path
path=$1
app_path=$(get_full_app_path ${path})
echo "${app_path##*/}"
}
function stoped(){
# incoming directory
app_directoy=$1
# get app path
app=$(get_app $app_directoy)
if [ -z "${app}" ]; then
echo "no find app $app"
else
pid=`ps -ef | grep ${app} | grep -v grep | awk '{print $2}'`
if [ -n "${pid}" ]; then
kill -9 $pid
echo "stop OK"
else
echo "haven't app run"
fi
fi
}
function started(){
. /etc/profile
# incoming direcory
app_directoy=$1
#get app path
app_path=$(get_full_app_path ${app_directoy})
echo ${app_path}
chmod 777 ${app_path}
## JAVA_HOME/java ...
nohup /usr/local/services/jdk1.8.0_171/bin/java -Xms512m -Xmx512m -jar ${app_path} --spring.profiles.active=stest start &
}
case $f in
start)
echo "starting app"
started ${current}
;;
stop)
echo "stoping app"
stoped ${current}
;;
restart)
stoped ${current}
started ${current}
echo "restart"
;;
*)
echo "start [Release directory],stop [Release directory],restart [Release directory], deploy [Release directory] [project]"
;;
esac
需要修改 nohup /usr/local/services/jdk1.8.0_171/bin/java 改成自己服务器的jdk路径