luhaowen 3 недель назад
Родитель
Сommit
9dfcbcc76f
1 измененных файлов с 42 добавлено и 0 удалено
  1. 42 0
      vars/deployDist.groovy

+ 42 - 0
vars/deployDist.groovy

@@ -0,0 +1,42 @@
+#!/usr/bin/env groovy
+
+def call(Map args = [:]) {
+    String distName    = args.distName    ?: env.DIST_NAME    ?: 'dist'
+    String distPath    = args.distPath    ?: env.DIST_PATH    ?: "${env.WORKSPACE}/dist"
+    String remoteHost  = args.remoteHost  ?: env.REMOTE_HOST  ?: error('REMOTE_HOST 未配置')
+    String remotePath  = args.remotePath  ?: env.REMOTE_PATH  ?: error('REMOTE_PATH 未配置')
+
+    String remoteDist  = "${remotePath}/${distName}"
+    String backupName  = "${distName}-$(date +%Y%m%d%H%M)"
+
+    PrintMes("开始发布 ${distName} 到 ${remoteHost}", 'yellow')
+
+    try {
+        /* 1. 判断远程是否存在同名目录,存在则备份 */
+        sh """
+            ssh -o StrictHostKeyChecking=no ${remoteHost} \
+                'if [ -d ${remoteDist} ]; then
+                     mv ${remoteDist} ${remotePath}/${backupName} &&
+                     echo "备份完成:${backupName}"
+                 fi'
+        """
+
+        /* 2. 上传新包 */
+        sh """
+            scp -o StrictHostKeyChecking=no -r ${distPath} ${remoteHost}:${remoteDist}
+        """
+
+        /* 3. 刷新服务 */
+        sh """
+            ssh -o StrictHostKeyChecking=no ${remoteHost} \
+                'chmod -R 755 ${remoteDist} &&
+                 chown -R www-data:www-data ${remoteDist} &&
+                 nginx -t && nginx -s reload'
+        """
+
+        PrintMes("发布成功 ${distName}", 'green')
+    } catch (Exception e) {
+        PrintMes("发布失败 ${distName} : ${e.message}", 'red')
+        error('deployDist failed')
+    }
+}