rollbackJar.groovy 1.3 KB

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