luhaowen 5 日 前
コミット
5f323a3dde
1 ファイル変更30 行追加24 行削除
  1. 30 24
      vars/deployJar.groovy

+ 30 - 24
vars/deployJar.groovy

@@ -1,58 +1,64 @@
 #!/usr/bin/env groovy
 /**
- * 强验证发布 SpringBoot  fat-jar
- *  - 按端口杀进程
- *  - 按 profile 做所有进程级校验
+ * 强验证发布 SpringBoot  fat-jar
+ *  - 按端口杀进程
+ *  - 按 profile 做所有进程级校验
  */
 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 jvmArgs    = args.jvmArgs    ?: env.JVM_ARGS ?: ''
-    String remoteJar  = "${remotePath}/${jarName}"
+    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 jvmArgs      = args.jvmArgs      ?: env.JVM_ARGS ?: ''
+    String remoteJar    = "${remotePath}/${jarName}"
 
     PrintMes("开始发布 ${jarName} [profile=${profile}] 到 ${remoteHost}:${remotePort}", 'yellow')
 
     try {
-/* 1. 按端口杀旧进程,并确认端口已释放 */
+        /* 1. 按端口杀旧进程,并确认端口已释放 (已修复) */
         sh """
             ssh -o StrictHostKeyChecking=no ${remoteHost} '
                 OLD_PID=\$(lsof -ti:${remotePort} || true)
                 if [ -n "\$OLD_PID" ]; then
                     echo "杀掉占用端口 ${remotePort} 的进程 \$OLD_PID"
-                   kill -9 \$OLD_PID
-                   sleep 3
+                    kill -9 \$OLD_PID
+                    sleep 3
                 fi
-               if lsof -ti:${remotePort}; then
-                   echo "端口 ${remotePort} 仍未释放,杀进程失败"
+                if lsof -ti:${remotePort}; then
+                    echo "端口 ${remotePort} 仍未释放,杀进程失败"
                     exit 1
                 fi
             '
         """
 
-        /* 2. 备份旧包 */
+        /* 2. 备份旧包 (已修复 Groovy 编译错误) */
         sh """
-            ssh -o StrictHostKeyChecking=no ${remoteHost} \\
-              'test -f ${remoteJar} && mv ${remoteJar} ${remoteJar}-\\$(date +%Y%m%d%H%M) || true'
+            ssh -o StrictHostKeyChecking=no ${remoteHost} '
+                test -f ${remoteJar} && mv ${remoteJar} ${remoteJar}-\$(date +%Y%m%d%H%M) || true
+            '
         """
 
         /* 3. 传包 + MD5 强校验 */
+        // 注意:这里的 \S1 是 Groovy GString 的转义,与 Shell 远程命令无关,因此保持不变。
         String localMd5 = sh(script: "md5sum ${jarPath} | awk '{print \$1}'", returnStdout: true).trim()
         sh "scp -o StrictHostKeyChecking=no ${jarPath} ${remoteHost}:${remoteJar}"
+        
+        // 这里的 awk 转义复杂,但 Groovy 编译时没问题,保持不变。
         String remoteMd5 = sh(script: "ssh -o StrictHostKeyChecking=no ${remoteHost} 'md5sum ${remoteJar} | awk \"{print \\$1}\"'", returnStdout: true).trim()
+        
         if (localMd5 != remoteMd5) {
             error("MD5 校验失败:本地 ${localMd5} != 远程 ${remoteMd5}")
         }
 
-        /* 4. 启动新进程 */
+        /* 4. 启动新进程 (已修复 Groovy 编译错误) */
         sh """
-            ssh -o StrictHostKeyChecking=no ${remoteHost} \\
-              'cd ${remotePath} &&
-               nohup java ${jvmArgs} -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &
-               sleep 5'
+            ssh -o StrictHostKeyChecking=no ${remoteHost} '
+                cd ${remotePath} &&
+                nohup java ${jvmArgs} -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &
+                sleep 5
+            '
         """
 
         /* 5. 按 profile 精确校验:有且仅有 1 个进程 */