deployJar.groovy 1.3 KB

12345678910111213141516171819202122232425262728293031
  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. PrintMes("开始发布 ${jarName} 到 ${remoteHost}:${remotePort}", 'yellow')
  11. try {
  12. /* 1. 停旧服务 */
  13. sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'lsof -ti:${remotePort} | xargs -r kill -9 || true'"
  14. /* 2. 备份旧包 */
  15. sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'test -f ${remoteJar} && mv ${remoteJar} ${remoteJar}-\$(date +%Y%m%d%H%M) || true'"
  16. /* 3. 传新包 */
  17. sh "scp -o StrictHostKeyChecking=no ${jarPath} ${remoteHost}:${remoteJar}"
  18. /* 4. 启动新服务 */
  19. sh "ssh -o StrictHostKeyChecking=no ${remoteHost} 'cd ${remotePath} && nohup java -jar ${jarName} --spring.profiles.active=${profile} > server.log 2>&1 </dev/null &'"
  20. PrintMes("发布成功 ${jarName}", 'green')
  21. } catch (Exception e) {
  22. PrintMes("发布失败 ${jarName} : ${e.message}", 'red')
  23. error('deploySpringBoot failed')
  24. }
  25. }