#!/usr/bin/env groovy def call(Map args = [:]) { 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}" PrintMes("开始发布 ${jarName} 到 ${remoteHost}:${remotePort}", 'yellow') try { /* 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} "sh -c 'cd ${remotePath} && nohup java -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &'" ''' PrintMes("发布成功 ${jarName}", 'green') } catch (Exception e) { PrintMes("发布失败 ${jarName} : ${e.message}", 'red') error('deploySpringBoot failed') } }