deployJar.groovy 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env groovy
  2. /**
  3. * 强验证发布 SpringBoot fat-jar
  4. * - 按端口杀进程
  5. * - 按 profile 做所有进程级校验
  6. */
  7. def call(Map args = [:]) {
  8. String jarName = args.jarName ?: env.JAR_NAME
  9. String jarPath = args.jarPath ?: env.JAR_PATH
  10. String remoteHost = args.remoteHost ?: env.REMOTE_HOST
  11. String remotePort = args.remotePort ?: env.REMOTE_PORT
  12. String remotePath = args.remotePath ?: env.REMOTE_PATH
  13. String profile = args.profile ?: env.PROFILE
  14. String jvmArgs = args.jvmArgs ?: env.JVM_ARGS ?: ''
  15. String remoteJar = "${remotePath}/${jarName}"
  16. PrintMes("开始发布 ${jarName} [profile=${profile}] 到 ${remoteHost}:${remotePort}", 'yellow')
  17. try {
  18. /* 1. 按端口杀旧进程,并确认端口已释放 */
  19. PrintMes("→ 1. 开始清理旧进程,确保端口 ${remotePort} 释放", 'yellow')
  20. sh """
  21. ssh -o StrictHostKeyChecking=no ${remoteHost} '
  22. OLD_PID=\$(lsof -ti:${remotePort} || true)
  23. if [ -n "\$OLD_PID" ]; then
  24. echo "杀掉占用端口 ${remotePort} 的进程 \$OLD_PID"
  25. kill -9 \$OLD_PID
  26. sleep 3
  27. fi
  28. if lsof -ti:${remotePort}; then
  29. echo "端口 ${remotePort} 仍未释放,杀进程失败"
  30. exit 1
  31. fi
  32. '
  33. """
  34. PrintMes("√ 1. 旧进程清理完成,端口 ${remotePort} 已释放", 'yellow')
  35. /* 2. 备份旧包 */
  36. PrintMes("→ 2. 开始备份旧 JAR 包 ${remoteJar}", 'yellow')
  37. sh """
  38. ssh -o StrictHostKeyChecking=no ${remoteHost} '
  39. test -f ${remoteJar} && mv ${remoteJar} ${remoteJar}-\$(date +%Y%m%d%H%M) || true
  40. '
  41. """
  42. PrintMes("√ 2. 旧 JAR 包备份完成", 'yellow')
  43. /* 3. 传包 + MD5 强校验 */
  44. PrintMes("→ 3. 开始传输 JAR 包并进行 MD5 强校验", 'yellow')
  45. // 1. 在本地计算 MD5
  46. String localMd5 = sh(script: "md5sum ${jarPath} | awk '{print \$1}'", returnStdout: true).trim()
  47. PrintMes(" 本地 MD5: ${localMd5}", 'yellow')
  48. // 2. 传输 JAR 包
  49. sh "scp -o StrictHostKeyChecking=no ${jarPath} ${remoteHost}:${remoteJar}"
  50. // 3. 远程计算 MD5 并返回
  51. String remoteMd5Output = sh(
  52. script: "ssh -o StrictHostKeyChecking=no ${remoteHost} 'md5sum ${remoteJar}'",
  53. returnStdout: true
  54. ).trim()
  55. // 4. 在 Jenkins Groovy 侧处理输出,提取 MD5 值
  56. String remoteMd5 = remoteMd5Output.split('\\s+')[0]
  57. PrintMes(" 远程 MD5: ${remoteMd5}", 'yellow')
  58. if (localMd5 != remoteMd5) {
  59. PrintMes("× 3. MD5 校验失败:本地 ${localMd5} != 远程 ${remoteMd5}", 'red')
  60. error("MD5 校验失败:本地 ${localMd5} != 远程 ${remoteMd5}")
  61. }
  62. PrintMes("√ 3. JAR 包传输和 MD5 校验成功", 'yellow')
  63. /* 4. 启动新进程 */
  64. PrintMes("→ 4. 启动新进程 (profile=${profile})", 'yellow')
  65. sh """ssh -o StrictHostKeyChecking=no ${remoteHost} "sh -c 'cd ${remotePath} && nohup java ${jvmArgs} -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &'" """
  66. PrintMes("√ 4. 新进程启动命令已发送", 'yellow')
  67. /* 5. 按 profile 精确校验:有且仅有 1 个进程 */
  68. PrintMes("→ 5. 校验进程数量 (期望 1 个 ${profile} 进程)", 'yellow')
  69. String procCount = sh(
  70. script: "ssh -o StrictHostKeyChecking=no ${remoteHost} 'ps -ef | grep \"java.*${jarName}.*--spring.profiles.active=${profile}\" | grep -v grep | wc -l'",
  71. returnStdout: true
  72. ).trim()
  73. PrintMes(" 实际找到 ${procCount} 个进程", 'yellow')
  74. if (procCount != "1") {
  75. PrintMes("× 5. 启动校验失败:期望 1 个 ${profile} 进程,实际 ${procCount} 个", 'red')
  76. error("启动校验失败:期望 1 个 ${profile} 进程,实际 ${procCount} 个")
  77. }
  78. PrintMes("√ 5. 进程数量校验成功", 'yellow')
  79. /* 6. 端口已被新进程占用 (加入等待重试机制) */
  80. def maxRetries = 12 // 最多重试 12 次
  81. def waitTimeSec = 5 // 每次间隔 5 秒
  82. String newPid = ""
  83. PrintMes("→ 6. 校验端口 ${remotePort} 是否已被新进程监听 (最大等待 ${maxRetries * waitTimeSec} 秒)", 'yellow')
  84. for (int i = 0; i < maxRetries; i++) {
  85. newPid = sh(script: "ssh -o StrictHostKeyChecking=no ${remoteHost} 'lsof -ti:${remotePort} || true'", returnStdout: true).trim()
  86. if (newPid) {
  87. PrintMes(" 端口已监听,PID: ${newPid}", 'yellow')
  88. break // 找到 PID,跳出循环
  89. }
  90. if (i < maxRetries - 1) {
  91. PrintMes(" 未监听,等待 ${waitTimeSec} 秒后重试... (${i + 1}/${maxRetries})", 'yellow')
  92. sleep(waitTimeSec) // Jenkins Pipeline 内置步骤
  93. }
  94. }
  95. if (!newPid) {
  96. PrintMes("× 6. 端口 ${remotePort} 在 ${maxRetries * waitTimeSec} 秒内未被新进程监听,启动可能失败", 'red')
  97. error("端口 ${remotePort} 未被新进程监听,启动超时失败")
  98. }
  99. PrintMes("√ 6. 端口监听校验成功", 'yellow')
  100. // 最终成功提示
  101. PrintMes("发布成功 ${jarName} [profile=${profile}, pid=${newPid}]", 'green')
  102. } catch (Exception e) {
  103. // 捕获异常,打印红色失败信息
  104. PrintMes("发布失败 ${jarName} : ${e.message}", 'red')
  105. error('deploySpringBoot failed')
  106. }
  107. }