1.NODEJS项目流水线配置(vue举例)
nodejs构建配置
官方教程
注意:下图的dist是vue项目打包目录名称,根据实际名称配置
# input your command here
cnpm cache clean --force
cnpm install
cnpm run build
主机部署配置
rm -rf /home/vipcardmall/frontend/
mkdir -p /home/vipcardmall/frontend
#解压构建的dist压缩包到指定目录
tar zxvf /home/flowapp/vip_card_mall_user_frontend_v2.tgz -C /home/vipcardmall/frontend
#删除dist压缩包
rm -rf /home/flowapp/vip_card_mall_user_frontend_v2.tgz
下图红框的是webhook触发地址,通过post/json触发,触发后会自动触发流水线,请求参数{},触发成功返回
{"object": true,"successful": true
}
2.springboot项目部署
deploy.sh
#!/bin/bash#---------------------------配置开始----------------------------------
# 修改APP_NAME为云效上的应用名
APP_NAME=openapi-service-0.0.1-SNAPSHOT
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT_SECONDS=70 # 等待应用启动的时间(秒)
APP_PORT=6677 # 应用端口
BASE_URL=http://127.0.0.1:${APP_PORT}/openapi-service # 应用的基础URL
HEALTH_CHECK_URL=${BASE_URL}/common/heartCheck # 应用健康检查URL
APP_HOME=/home/backend/openapi # 从package.tgz中解压出来的jar包放到这个目录下
JAR_NAME=${APP_HOME}/${APP_NAME}.jar # jar包的名字
JAVA_OUT=${APP_HOME}/prod.log #应用的启动日志
PACKAGE_PATH=/home/flowapp/mall-openapi.tgz #制品路径
SHUTDOWN_URL=${BASE_URL}/common/actuator/shutdown #优雅关闭地址
SHUTDOWN_TIMEOUT_SECONDS=70 #优雅停机超时时间(秒)
SPRING_PROFILE="prod" # The Spring profile to activate
#---------------------------配置结束----------------------------------# 创建出相关目录
mkdir -p ${APP_HOME}
usage() {echo "Usage: $PROG_NAME {start|stop|restart}"exit 2
}health_check() {exptime=0echo "checking ${HEALTH_CHECK_URL}"while truedostatus_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code} ${HEALTH_CHECK_URL}`if [ "$?" != "0" ]; thenecho -n -e "\rapplication not started"elseecho "code is $status_code"if [ "$status_code" == "200" ];thenbreakfifisleep 1((exptime++))echo -e "\rWait app to pass health check: $exptime..."if [ $exptime -gt ${APP_START_TIMEOUT_SECONDS} ]; thenecho 'app start failed'exit 1fidoneecho "check ${HEALTH_CHECK_URL} success"
}
start_application() {echo "starting java process"nohup java -jar -Xmx512m -Xms256m -Dspring.profiles.active=${SPRING_PROFILE} -Dserver.port=${APP_PORT} ${JAR_NAME} >${JAVA_OUT} 2>&1 &echo "started java process"
}stop_application() {# 查找Java进程IDcheckjavapid=$(ps -ef | grep java | grep ${APP_NAME} | grep ${APP_PORT} | grep -v grep | awk '{print $2}')# 如果没有发现Java进程,则直接返回if [[ ! $checkjavapid ]]; thenecho "No java process to stop (process not found)"returnfi# 向Java进程发送SIGTERM请求echo "Sending SIGTERM to Java process with PID ${checkjavapid}."kill -15 ${checkjavapid}# 等待Java进程停止for ((i=0; i<$SHUTDOWN_TIMEOUT_SECONDS; i++)); do# 使用curl检查心跳接口http_status=$(curl -o /dev/null -s -w "%{http_code}\n" ${HEALTH_CHECK_URL})# 如果状态码不是200,认为进程已经停止if [ "$http_status" != "200" ]; thenecho "Java process stopped successfully."returnfisleep 1echo "Waiting for Java process to stop..."done# 如果Java进程在等待时间后仍然存在,则发送SIGKILLecho "Java process did not stop after $SHUTDOWN_TIMEOUT_SECONDS seconds, sending SIGKILL"kill -9 $checkjavapidecho "Java process killed forcefully"
}start() {start_applicationhealth_check
}
stop() {stop_application
}
deploy() {# 停止应用程序stop# 解压PACKAGE_PATH文件到APP_HOME目录echo "Unpacking $PACKAGE_PATH to $APP_HOME"if tar zxvf $PACKAGE_PATH -C $APP_HOME; thenecho "Unpack finished successfully."# 删除原始压缩包echo "Removing the package $PACKAGE_PATH"rm -f $PACKAGE_PATHif [ $? -eq 0 ]; thenecho "Package removed successfully."elseecho "Failed to remove the package."fielseecho "Error occurred during unpacking. Exiting."exit 1fi# 启动应用程序start
}case "$ACTION" instart)start;;stop)stop;;restart)stopstart;;deploy)deploy;;*)usage;;
esac