创建自动化部署流水线
选择示例代码
使用默认的Java构建上传模块
新建主机
添加自有主机
复制信息到自有主机运行
复制执行在Linux系统上
设置部署主机的包的位置和执行位置
以下是 deploy.sh文件信息,新建一个文件放到服务器与包相同的目录下
#!/bin/bash
# 修改APP_NAME为云效上的应用名
APP_NAME=application
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20 # 等待应用启动的时间
APP_PORT=8080 # 应用端口
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT} # 应用健康检查URL
HEALTH_CHECK_FILE_DIR=/home/admin/app/status # 脚本会在这个目录下生成nginx-status文件
APP_HOME=/home/admin/app # 从package.tgz中解压出来的jar包放到这个目录下
JAR_NAME=${APP_HOME}/${APP_NAME}.jar # jar包的名字
JAVA_OUT=${APP_HOME}/logs/start.log #应用的启动日志
# 创建出相关目录
mkdir -p ${HEALTH_CHECK_FILE_DIR}
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
echo "Usage: $PROG_NAME {start|stop|restart}"
exit 2
}
health_check() {
exptime=0
echo "checking ${HEALTH_CHECK_URL}"
while true
do
status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code} ${HEALTH_CHECK_URL}`
if [ "$?" != "0" ]; then
echo -n -e "
application not started"
else
echo "code is $status_code"
if [ "$status_code" == "200" ];then
break
fi
fi
sleep 1
((exptime++))
echo -e "
Wait app to pass health check: $exptime..."
if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
echo 'app start failed'
exit 1
fi
done
echo "check ${HEALTH_CHECK_URL} success"
}
start_application() {
echo "starting java process"
nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 &
echo "started java process"
}
stop_application() {
checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
if [[ ! $checkjavapid ]];then
echo -e "
no java process"
return
fi
echo "stop java process"
times=60
for e in $(seq 60)
do
sleep 1
COSTTIME=$(($times - $e ))
checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
if [[ $checkjavapid ]];then
kill -9 $checkjavapid
echo -e "
-- stopping java lasts `expr $COSTTIME` seconds."
else
echo -e "
java process has exited"
break;
fi
done
echo ""
}
start() {
start_application
health_check
}
stop() {
stop_application
}
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
;;
esac
执行运行,查看部署情况
查看服务器状况
多主机部署
新增一台主机
编辑主机
添加了第二台主机
点击运行查看部署情况
查看部署情况