前端自动化部署,基于scp2,ssh2

前端自动化部署,基于scp2,ssh2

配置文件

deploy/products.js

/*
 * @author: linzl
 * @method: 
 * @param: 
 * @Date: 2021-05-11 14:32:22
 * @return: 
 */
/*
 *读取env环境变量
 */
 const SERVER_ID = process.env.NODE_ENV === "prod" ? 0 : 1; 
 /*
  *定义多个服务器账号 及 根据 SERVER_ID 导出当前环境服务器账号
  * 使用的是ssh的密钥
  */
 const path = "C:\\Users\\{用户名}\\.ssh\\id_rsa" 
 let privateKey = require('fs').readFileSync(path).toString() 
 const SERVER_LIST = [
   {
     id: 0,
     name: "A-测试环境",
     domain: "www.XXX.com",
     host: "101.200.181.XX",
     port: 108,
     username: "root",
     password: "",
     privateKey:privateKey,
     indexpath: "/XXXX/XXXX/smartBigScreen/",
     assetspath: "/XXXX/XXXX/smartBigScreen/assets"
   },
   {
     id: 1,
     name: "B-测试环境",
     domain: "www.XXX.com",
     host: "101.200.181.XX",
     port: 108,
     username: "root",
     password: "",
     privateKey: privateKey,
     indexpath: "/XXXX/XXXX/smartBigScreen/",
     assetspath: "/XXXX/XXXX/smartBigScreen/assets"
   }
 ];
 
 module.exports = SERVER_LIST[SERVER_ID];

执行脚本文件

deploy/index.js

/*
 * @author: linzl
 * @method: 
 * @param: 
 * @Date: 2021-05-11 14:32:44
 * @return: 
 */
//  deploy/index.js里面
const scpClient = require("scp2");
const ora = require("ora");
const chalk = require("chalk");
const server = require("./products");
const spinner = ora(
  "正在发布到" +
    (process.env.NODE_ENV === "prod" ? "生产" : "测试") +
    "服务器..."
);

var Client = require("ssh2").Client;
var config = {
    host: server.host,
    port: server.port,
    username: server.username,
    password: server.password,
    privateKey: server.privateKey, 
} 
var conn = new Client();
conn
  .on("ready", function() {
      console.log("enter");
    // rm 删除dist文件,n 是换行 换行执行 重启nginx命令 我这里是用docker重启nginx
    conn.exec(
      "rm -rf /XXXX/smartBigScreen/index.htmln rm -rf /XXXX/smartBigScreen/assets", 
      function(err, stream) {
        if (err) throw err;
        stream
          .on("close", function(code, signal) {
            // 在执行shell命令后,把开始上传部署项目代码放到这里面
            spinner.start();
            scpClient.scp(
              "dist/index.html",
              {
                ...config,
                path: server.indexpath
              },
              function(err) {
                if (err) {
                  console.log(chalk.red("发布失败.n"));
                  throw err;
                } else {
                  scpClient.scp(
                    "dist/assets/",
                    {
                      ...config,
                      path: server.assetspath
                    },
                    function(err) {
                      spinner.stop();
                      if (err) {
                        console.log(chalk.red("发布失败.n"));
                        throw err;
                      } else {
                        console.log(
                          chalk.green(
                            "Success! 成功发布到" +
                              (process.env.NODE_ENV === "prod"
                                ? "生产"
                                : "测试") +
                              "服务器! n"
                          )
                        );
                      }
                    }
                  );
                }
              }
            );

            conn.end();
          })
          .on("data", function(data) {
            console.log("STDOUT: " + data);
          })
          .stderr.on("data", function(data) {
            console.log("STDERR: " + data);
          });
      }
    );
  })
  .connect(config);

package.json

{
  "name": "smart-bigscreen-react",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build:test": "tsc && vite build --mode staging",
    "build": "tsc && vite build --mode production",
    "serve": "vite preview",
    "css": "node ./tool/index.js",
    "deploy:dev": "npm run build:test && cross-env NODE_ENV=dev  node ./deploy ", 
    "deploy:prod": "npm run build && cross-env NODE_ENV=prod node ./deploy", 
    "commit": "git-cz" 
  },
 }

效果图

前端自动化部署,基于scp2,ssh2
前端自动化部署,基于scp2,ssh2

上一篇:LeetCode 91. Decode Ways


下一篇:Leetcode 题解 - 分治