luhaowen 3 долоо хоног өмнө
parent
commit
7949c915b6
1 өөрчлөгдсөн 22 нэмэгдсэн , 45 устгасан
  1. 22 45
      vars/deployJar.groovy

+ 22 - 45
vars/deployJar.groovy

@@ -1,54 +1,31 @@
 #!/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 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}"
 
-    String remoteJar = "${remotePath}/${jarName}"
+    PrintMes("开始发布 ${jarName} 到 ${remoteHost}:${remotePort}", 'yellow')
 
-    echo ">>> 开始发布 ${jarName} 到 ${remoteHost}:${remotePort}"
+    try {
+        /* 1. 停旧服务 */
+        sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'lsof -ti:${remotePort} | xargs -r kill -9 || true'"
 
-    /* 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'"
 
-    /* 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}"
 
-    /* 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 &'"
 
-    /* 4. 启动新服务 */
-    sh """
-       ssh -o StrictHostKeyChecking=no ${remoteHost} \
-          'cd ${remotePath} && nohup java -jar ${jarName} \
-           --spring.profiles.active=${profile} > server.log 2>&1 &'
-    """
-
-    echo "<<< 发布完成"
+        PrintMes("发布成功 ${jarName}", 'green')
+    } catch (Exception e) {
+        PrintMes("发布失败 ${jarName} : ${e.message}", 'red')
+        error('deploySpringBoot failed')   // 让流水线整体失败
+    }
 }