前端自动化部署

在这里插入图片描述

在这里插入代码片

require("dotenv").config();
const { NodeSSH } = require("node-ssh");
const path = require("path");

const ssh = new NodeSSH();

async function deploy() {
  try {
    console.log("开始部署...");

    // 连接服务器
    await ssh.connect({
      host: process.env.DEPLOY_HOST,
      username: process.env.DEPLOY_USERNAME,
      password: process.env.DEPLOY_PASSWORD,
      port: process.env.DEPLOY_PORT || 22,
    });

    console.log("已连接到服务器");

    // 获取项目根目录
    const projectRoot = path.resolve(__dirname);
    const remotePath = process.env.DEPLOY_PATH || "/var/www/nodeRed";

    // 清理远程目录(保留node_modules和.env文件)
    console.log("清理远程目录...");
    const cleanCommand = `
            cd ${remotePath} && \
            find . -maxdepth 1 ! -name '.' ! -name '..' ! -name 'node_modules' ! -name '.env' -exec rm -rf {} +
        `;
    await ssh.execCommand(cleanCommand);
    console.log("远程目录清理完成");

    // 上传项目文件
    console.log(`正在上传文件到 ${remotePath}...`);
    await ssh.putDirectory(projectRoot, remotePath, {
      recursive: true,
      concurrency: 10,
      validate: function (itemPath) {
        const baseName = path.basename(itemPath);
        return (
          baseName.charAt(0) !== "." && // 不上传隐藏文件
          baseName !== "node_modules"
        ); // 不上传node_modules
        //    baseName !== '.env';   不上传.env文件
      },
    });

    // 安装依赖(如果package.json有更新)
    console.log("检查并更新项目依赖...");
    await ssh.execCommand("npm install", {
      cwd: remotePath,
    });

    // 检查PM2是否已安装
    console.log("检查PM2...");
    const pmResult = await ssh.execCommand("pm2 -v");
    if (pmResult.code !== 0) {
      console.log("PM2未安装,正在安装...");
      await ssh.execCommand("npm install pm2 -g");
    } else {
      console.log("PM2已安装,版本:" + pmResult.stdout);
    }

    // 使用PM2启动应用
    console.log("使用PM2重启应用...");
    await ssh.execCommand("pm2 delete nodeRed || true", { cwd: remotePath });
    await ssh.execCommand("pm2 restart ecosystem.config.js --env production", {
      cwd: remotePath,
    });

    console.log("部署完成!");
    process.exit(0);
  } catch (error) {
    console.error("部署失败:", error);
    process.exit(1);
  } finally {
    ssh.dispose(); // 确保关闭连接
  }
}

deploy();

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值