deployJar.groovy 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. ///usr/bin/env groovy
  2. def call(Map args = [:]) {
  3. String jarName = args.jarName ?: env.JAR_NAME
  4. String jarPath = args.jarPath ?: env.JAR_PATH
  5. String remoteHost = args.remoteHost ?: env.REMOTE_HOST
  6. String remotePort = args.remotePort ?: env.REMOTE_PORT
  7. String remotePath = args.remotePath ?: env.REMOTE_PATH
  8. String profile = args.profile ?: env.profile
  9. String remoteJar = "${remotePath}/${jarName}"
  10. String jvmArgs = args.jvmArgs ?: env.JVM_ARGS ?: ''
  11. PrintMes("开始发布 ${jarName} 到 ${remoteHost}:${remotePort}", 'yellow')
  12. try {
  13. /* 1. 停旧服务 */
  14. sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'lsof -ti:${remotePort} | xargs -r kill -9 || true'"
  15. /* 2. 备份旧包 */
  16. sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'test -f ${remoteJar} && mv ${remoteJar} ${remoteJar}-\$(date +%Y%m%d%H%M) || true'"
  17. /* 3. 传新包 */
  18. sh "scp -o StrictHostKeyChecking=no ${jarPath} ${remoteHost}:${remoteJar}"
  19. /* 4. 启动新服务 */
  20. sh """ssh -o StrictHostKeyChecking=no ${remoteHost} "sh -c 'cd ${remotePath} && nohup java ${jvmArgs} -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 &'" """
  21. PrintMes("发布成功 ${jarName}", 'green')
  22. } catch (Exception e) {
  23. PrintMes("发布失败 ${jarName} : ${e.message}", 'red')
  24. error('deploySpringBoot failed')
  25. }
  26. }