luhaowen vor 2 Wochen
Ursprung
Commit
6e0c7a7da9
1 geänderte Dateien mit 27 neuen und 0 gelöschten Zeilen
  1. 27 0
      vars/rollbackJar.groovy

+ 27 - 0
vars/rollbackJar.groovy

@@ -0,0 +1,27 @@
+def call(Map args = [:]) {
+  String remoteHost = args.remoteHost ?: env.REMOTE_HOST
+  String remotePort = args.remotePort ?: env.REMOTE_PORT
+  String remotePath = args.remotePath ?: env.REMOTE_PATH
+  String jarName    = args.jarName    ?: env.JAR_NAME
+  String profile    = args.profile    ?: env.profile
+  String jvmArgs    = args.jvmArgs    ?: env.JVM_ARGS ?: ''
+  String backupTime = args.backupTime ?: params.BACKUP_TIME
+
+  // 1. 找到要回滚到的备份包
+  String findCmd = """ssh -o StrictHostKeyChecking=no ${remoteHost} \
+      "cd ${remotePath} && ls ${jarName}-${backupTime ?: '*'} | tail -1" """
+  String backupJar = sh(script: findCmd, returnStdout: true).trim()
+  if (!backupJar) error("未找到备份包,回滚终止")
+
+  // 2. 停当前服务
+  sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'lsof -ti:${remotePort} | xargs -r kill -9 || true'"
+
+  // 3. 还原包
+  sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'cd ${remotePath} && cp -f ${backupJar} ${jarName}'"
+
+  // 4. 重启
+  sh """ssh -o StrictHostKeyChecking=no ${remoteHost} \
+      "sh -c 'cd ${remotePath} && nohup java ${jvmArgs} -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &'" """
+
+  PrintMes("回滚完成:${backupJar} 已部署到 ${remoteHost}:${remotePort}", 'green')
+}