#!/usr/bin/env groovy /** * 一键发布 SpringBoot jar 到远程 Linux 主机 * 依赖环境变量(Jenkinsfile 的 environment {} 已定义即可): * JAR_NAME 例:jeecg-system-start-3.8.2.jar * JAR_PATH 例:sohoyw-som/jeecg-module-system/jeecg-system-start/target/${JAR_NAME} * REMOTE_HOST 例:root@52.83.132.95 * REMOTE_PORT 例:8080 * REMOTE_PATH 例:/srv/server * profile 例:test * * 用法: * deploySpringBoot() * 或 * deploySpringBoot(jarName: 'xxx.jar', profile: 'prod') */ def call(Map args = [:]) { /* 0. 读取参数(优先使用传参,其次 fallback 到环境变量) */ String jarName = args.jarName ?: env.JAR_NAME String jarPath = args.jarPath ?: env.JAR_PATH String remoteHost= args.remoteHost?: env.REMOTE_HOST String remotePort= args.remotePort?: env.REMOTE_PORT String remotePath= args.remotePath?: env.REMOTE_PATH String profile = args.profile ?: env.profile String remoteJar = "${remotePath}/${jarName}" echo ">>> 开始发布 ${jarName} 到 ${remoteHost}:${remotePort}" /* 1. 停旧服务(端口进程) */ sh """ ssh -o StrictHostKeyChecking=no ${remoteHost} \ 'lsof -ti:${remotePort} | xargs -r kill -9 || true' """ /* 2. 备份旧包(如果存在) */ sh """ ssh -o StrictHostKeyChecking=no ${remoteHost} \ 'test -f ${remoteJar} && \\ mv ${remoteJar} ${remoteJar}-\$(date +%Y%m%d%H%M) || true' """ /* 3. 传新包 */ sh "scp -o StrictHostKeyChecking=no ${jarPath} ${remoteHost}:${remoteJar}" /* 4. 启动新服务 */ sh """ ssh -o StrictHostKeyChecking=no ${remoteHost} \ 'cd ${remotePath} && nohup java -jar ${jarName} \ --spring.profiles.active=${profile} > server.log 2>&1 &' """ echo "<<< 发布完成" }