批量部署前端项目到多个不同的服务器

const config = require("./config.js");

const shell = require("shelljs");

const chalk = require("chalk");

const fs = require("fs");

const path = require("path");

const Client = require("ssh2-sftp-client");

const rawArgv = process.argv.slice(2);

const dayjs = require("dayjs");

// const args = rawArgv.join(" ");

// // const ora = require("ora");

const filterStage = rawArgv.includes("--prod") ? "prod" : "stage";

console.log(chalk.blue("当前环境:", filterStage));

const ClientOrg = require("ssh2").Client;



const compileDist = async () => {

  if (shell.exec(`npm run build:${filterStage}`).code === 0) {

    console.log(chalk.green("打包成功"));

  }

};



async function connectShell(params) {

  config

    .filter((item) => item.nodeEnv == filterStage)

    .map((item, index) => {

      const sftp = new Client();

      sftp

        .connect({

          host: item.host,

          port: item.port,

          username: item.username,

          password: item.password,

        })

        .then(() => {

          if (index > 0) {

            return "ok";

          }

          console.log(

            chalk.red(`${item.host}`) + chalk.blue(`---执行下拉文件备份---`)

          );

          console.log(chalk.blue(`---创建备份文件夹中···---`));

          let newFile = `/${item.host}/${dayjs().format(

            "YYYY-MM-DD"

          )}/dist-${dayjs().format("HH_mm_ss")}`;

          fs.mkdir(

            path.resolve(__dirname, `../distbak${newFile}`),

            { recursive: true },

            (err) => {

              if (err) throw err;

              console.log(chalk.green("---创建备份文件夹成功---"));

            }

          );



          return sftp.downloadDir(

            item.path,

            path.resolve(__dirname, `../distbak${newFile}`)

          );

        })

        .then(() => {

          if (index === 0) {

            console.log(

              chalk.red(`${item.host}`) + chalk.blue(`---备份完成---`)

            );

          }

          console.log(

            chalk.yellow(`${item.host}`) + chalk.red(`---执行删除文件中···---`)

          );

          return sftp.rmdir(item.path, true);

        })

        .then(() => {

          console.log(

            chalk.red(`${item.host}`) + chalk.green(`执行删除文件成功---`)

          );

          console.log(

            chalk.hex("#DEADED").bold(`---${item.host}执行上传文件中···---`)

          );

          return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);

        })

        .then(() => {

          console.log(

            chalk.yellow(`${item.host}`) + chalk.green(`上传完成,部署成功---`)

          );

          sftp.end();

          chmodDir(item); //赋值

        })

        .catch((err) => {

          console.error(err, chalk.red(`${item.host}`) + chalk.red(`上传失败`));

          sftp.end();

        });

    });

}

async function chmodDir(item) {

  if (!item) {

    throw "获取ip配置失败";

  }



  const conn = new ClientOrg();

  conn

    .on("ready", function () {

      conn.exec("chmod  755  /etc/nginx/html -R", function (err, stream) {

        if (err) throw err;

        console.log(

          chalk.red(`${item.host}`) + chalk.green(`---添加目录权限成功---`)

        );

        conn.end();

      });

    })

    .connect({

      host: item.host,

      port: item.port,

      username: item.username,

      password: item.password,

    });

}

async function runStart() {

  await compileDist();

  await connectShell();

}

runStart();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值