Parcourir la source

feat: 增加项目构建后推送

sunxiao il y a 8 mois
Parent
commit
32881910b3

+ 26 - 0
deploy/config.js

@@ -0,0 +1,26 @@
+export const config = [
+  {
+    id: 0,
+    nodeEnv: "test",
+    name: "测试环境",
+    domain: "",
+    host: "10.0.0.28",
+    port: "22",
+    username: "root",
+    password: "hsmysql",
+    path: "/data/node_web/modelAdmin",
+    removepath: "/data/node_web/modelAdmin"
+  },
+  {
+    id: 1,
+    nodeEnv: "prod",
+    name: "生产环境",
+    domain: "",
+    host: "192.168.9.54",
+    port: "22",
+    username: "root",
+    password: "admin1,xxh",
+    path: "/data/node_web/modelAdmin",
+    removepath: "/data/node_web/modelAdmin"
+  },
+];

+ 103 - 0
deploy/index.js

@@ -0,0 +1,103 @@
+import Client from 'ssh2-sftp-client';
+import ssh2 from 'ssh2';
+import chalk from 'chalk';
+import ora from 'ora';
+import shell from 'shelljs';
+import path from 'path';
+import { fileURLToPath } from 'url';
+import { config } from './config.js';
+
+const rawArgv = process.argv.slice(2);
+const filterStage = rawArgv.includes("--prod") ? "prod" : "test";
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+// 构建
+const compileDist = async () => {
+  console.log(chalk.blue("项目开始构建"));
+  if (shell.exec(`npm run build:${filterStage}`).code === 0) {
+    console.log(chalk.blue("项目构建成功"));
+  }
+}
+
+// 获取配置
+const getConfig = () => {
+  const [result] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
+  return result;
+}
+
+const execRemoteCommand = async () => {
+  const config = getConfig();
+  const conn = new ssh2.Client();
+  conn.on("ready", function () {
+    console.log(chalk.blue("ssh2 链接成功 - 执行 pm2 restart bigModelAdmin"));
+    conn.exec("pm2 restart bigModelAdmin", function (err, stream) {
+      if (err) throw err;
+      console.log(
+        chalk.blue(``) + chalk.green(`---执行成功---`),
+      );
+      console.log(
+        chalk.blue(`✅`) + chalk.green(`---部署完成---`),
+      );
+      conn.end();
+    });
+  }).connect({ ...config });
+}
+
+
+// 部署
+const connectShell = async () => {
+  const sftp = new Client();
+  const item = getConfig();
+  let spinner = null;
+
+  // 打印信息
+  const printMsg = ({ color, text }) => {
+    console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
+  }
+
+  printMsg({ color: 'green', text: '服务器连接中' });
+
+  sftp.connect({
+    host: item.host,
+    port: item.port,
+    username: item.username,
+    password: item.password,
+  })
+    .then(() => {
+      printMsg({ color: 'green', text: '服务器连接成功' });
+      printMsg({ color: 'yellow', text: '执行删除文件中' });
+      return sftp.rmdir(item.path, true);
+    })
+    .then(() => {
+      printMsg({ color: 'green', text: '执行删除文件成功' });
+      printMsg({ color: 'green', text: '即将开始上传' });
+      spinner = ora().start();
+      spinner.text = '文件上传中,请等待'
+      return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
+    })
+    .then(() => {
+      spinner.info('文件上传结束')
+      spinner.stop();
+
+      if (filterStage === 'prod') {
+        execRemoteCommand()
+      }
+
+      printMsg({ color: 'green', text: '上传完成,部署成功' });
+      sftp.end();
+    })
+    .catch((err) => {
+      console.error(
+        err,
+        chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
+      );
+      sftp.end();
+    });
+}
+
+async function runStart() {
+  await compileDist();
+  await connectShell();
+}
+
+runStart();

+ 6 - 1
package.json

@@ -10,7 +10,9 @@
     "build:prod": "vite build",
     "build:test": "vite build --mode test",
     "build:stage": "vite build --mode staging",
-    "preview": "vite preview"
+    "preview": "vite preview",
+    "deploy:prod": "node deploy/index.js --prod",
+    "deploy:test": "node deploy/index.js --test"
   },
   "repository": {
     "type": "git",
@@ -34,7 +36,9 @@
     "markdown-it-math": "^4.1.1",
     "markdown-it-texmath": "^1.0.0",
     "nprogress": "0.2.0",
+    "ora": "^8.0.1",
     "pinia": "2.1.7",
+    "ssh2-sftp-client": "^10.0.3",
     "vue": "3.3.9",
     "vue-cropper": "1.1.1",
     "vue-router": "4.2.5"
@@ -45,6 +49,7 @@
     "autoprefixer": "^10.4.19",
     "postcss": "^8.4.38",
     "sass": "1.69.5",
+    "ssh2": "^1.15.0",
     "tailwindcss": "^3.4.4",
     "unplugin-auto-import": "0.17.1",
     "unplugin-vue-setup-extend-plus": "1.0.0",

+ 12 - 0
public/ecosystem.config.js

@@ -0,0 +1,12 @@
+module.exports = {
+  apps: [{
+    name: 'bigModelAdmin',
+    port: '8899',
+    output: './pm2-log/nitro-back-out.log',
+    error: './pm2-log/nitro-back-error.log',
+    ignore_watch: ["node_modules", "logs"],
+    script: './index.html'
+  }]
+}
+
+

Fichier diff supprimé car celui-ci est trop grand
+ 135 - 0
vite.config.js.timestamp-1720755814744-8a4ee5866c34d.mjs


Fichier diff supprimé car celui-ci est trop grand
+ 135 - 0
vite.config.js.timestamp-1720755825503-17bae81946011.mjs


Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff