deployDist.groovy 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env groovy
  2. def call(Map args = [:]) {
  3. String distName = args.distName ?: env.DIST_NAME ?: 'dist'
  4. String distPath = args.distPath ?: env.DIST_PATH ?: "${env.WORKSPACE}/dist"
  5. String remoteHost = args.remoteHost ?: env.REMOTE_HOST ?: error('REMOTE_HOST 未配置')
  6. String remotePath = args.remotePath ?: env.REMOTE_PATH ?: error('REMOTE_PATH 未配置')
  7. String remoteDist = "${remotePath}/${distName}"
  8. String backupName = "${distName}-\$(date +%Y%m%d%H%M)"
  9. PrintMes("开始发布 ${distName} 到 ${remoteHost}", 'yellow')
  10. try {
  11. /* 1. 判断远程是否存在同名目录,存在则备份 */
  12. sh """
  13. ssh -o StrictHostKeyChecking=no ${remoteHost} \
  14. 'if [ -d ${remoteDist} ]; then
  15. mv ${remoteDist} ${remotePath}/${backupName} &&
  16. echo "备份完成:${backupName}"
  17. fi'
  18. """
  19. /* 2. 上传新包 */
  20. sh """
  21. scp -o StrictHostKeyChecking=no -r ${distPath} ${remoteHost}:${remoteDist}
  22. """
  23. /* 3. 刷新服务 */
  24. sh """
  25. ssh -o StrictHostKeyChecking=no ${remoteHost} \
  26. 'chmod -R 755 ${remoteDist} &&
  27. chown -R www-data:www-data ${remoteDist} &&
  28. nginx -t && nginx -s reload'
  29. """
  30. PrintMes("发布成功 ${distName}", 'green')
  31. } catch (Exception e) {
  32. PrintMes("发布失败 ${distName} : ${e.message}", 'red')
  33. error('deployDist failed')
  34. }
  35. }