jenkins发布项目
一 前提
1 首先要保证你的jenkins服务器可以正常连接你要发布的服务器,所以可以设置下免密钥登录,并且注意你的jenkins用户,到对方的机器是否有执行权限,比如重启服务,备份等。
2 设置hosts,如果用主机名通信的话。
二 jenkins设置
1 选择要发布的项目
2 选择要发布的主机
3 选择父目录
一个项目,可能里面包括很多子项目,如果你要发布的子项目,就需要先选择父母录
4 执行发布脚本
三 脚本编写
1 拷贝新包,和备份旧包
sendPort.sh脚本
#!/bin/bash
function sendToRemote(){
currentTime=`date "+%Y%m%d_%H%M%S"`
remote_shell_path=/usr/local/tiantian_dubbo #远端服务器项目目录
remote_path=/usr/local/tiantian_dubbo/${project}
local_package=${local_dir}/${parentDirectory}/${project}/target/${package_name}
echo "local_package::"${local_package}
remote_wait_package=${remote_path}/wait/${currentTime}${package_name}
remote_backup_path=${remote_path}/backup
echo "发送到远程服务器"
scp -P${remote_port} ${local_package} ${remote_user}@${remote_ip}:${remote_wait_package}
echo "开始关闭服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh stop ${currentTime}${package_name}" && sleep 2
echo "关闭完成"
echo "开始执行备份"
ssh ${remote_user}@${remote_ip} -p${remote_port} "mv ${remote_path}/*${package_name} ${remote_backup_path}/"
echo "备份完成"
echo "将等待执行文件mv到执行目录"
ssh ${remote_user}@${remote_ip} -p${remote_port} "mv ${remote_wait_package} ${remote_path}/"
echo "mv完成"
echo "开始启动服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh start ${currentTime}${package_name}"
echo "启动完成"
}
function remote(){
project=$1
package_name=$2
parentDirectory=$4
remote_port=51212
remote_user=jenkins
remote_ip=$3
echo "remote_ip:${remote_ip}"
local_dir=/xs/git_project/tiantian_dubbo
sendToRemote
}
case "$@" in
*)
packageName="$1"
parentDirectory="$3"
node="$2"
remote ${packageName%.*} ${packageName} ${node} ${parentDirectory}
;;
esac
echo "$@脚本执行完毕"
2 启动服务脚本
server.sh
# description: app开机启动脚本
# 接收入参带.jar的待启动包名称
packageName="$2"
RETVAL=0
APP_NAME=${packageName%.*}
APP_NAME=${APP_NAME:15}
# description: app开机启动脚本
# 接收入参带.jar的待启动包名称
packageName="$2"
RETVAL=0
APP_NAME=${packageName%.*}
APP_NAME=${APP_NAME:15}
export JAVA_HOME=/usr/local/java/jdk1.8.0_111
export JRE_HOME=/usr/local/java/jdk1.8.0_111/jre
APP_DIR=/usr/local/tiantian_dubbo/${APP_NAME}
JAR_NAME=${packageName}
PID=$APP_NAME\.pid
# 自定义
logPath=/datalog/service/$APP_NAME/$server_name
if [ ! -x "$logPath" ]; then
mkdir -p "${logPath}"
fi
# 运行环境------------------------------必须要指定
envName=prod
cd $APP_DIR
case "$1" in
start)
##-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC
## -Xms1024m -Xmx1024m
java_opts='-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC'
nohup $JRE_HOME/bin/java -Dfile.encoding=utf-8 ${java_opts} -jar $APP_DIR/$JAR_NAME --spring.profiles.active=$envName --logging.path=$logPath >$logPath/nohup.out 2>&1 &
echo $! > $APP_DIR/$PID
echo "start $APP_NAME success!"
;;
stop)
kill -9 `cat $APP_DIR/$PID`
echo "begin stop $APP_NAME"
sleep 5
APP_PID=`ps -ef|grep $$APP_NAME|grep $APP_DIR|grep -v 'grep'|awk '{print $2}'`
if [ "$APP_PID" == "" ];
then
echo "stop $APP_NAME success !"
else
echo "$APP_NAME process pid is:$APP_PID"
echo "begin kill $APP_NAME process, pid is:$APP_PID"
kill -9 $APP_PID
echo "stop $APP_NAME success!"
fi
;;
restart)
echo "restarting $APP_NAME"
/usr/local/tiantian_dubbo/server.sh stop $2
echo "stoped"
sleep 2
echo "starting"
/usr/local/tiantian_dubbo/server.sh start $2
;;
*)
echo "start|restart|stop"
;;
esac
exit 0