首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

handazao

养家糊口
首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Docker使用

  • 数据库相关

  • Java相关

  • Linux学习

    • Linux常用命令
    • Ubuntu定时任务
    • 定时备份mysql数据库
    • Ubuntu开启bbr
    • 密钥连接服务器,发布项目
      • 1.发布项目到服务器
  • 工具

  • vue3

  • Git

  • 技术
  • Linux学习
handazao
2023-01-12
目录

密钥连接服务器,发布项目

# 1.发布项目到服务器

// 服务器配置信息
const server = {
    host: 'ip', // 服务器ip
    port: '22', // 端口一般默认22
    username: 'ubuntu', // 用户名
    //password: '', // 密码
    passphrase: 'password', //私钥密码
    privateKey: '',//使用 私钥密钥登录
    pathNmae: '/home/ubuntu/vuepress/dist', // 上传到服务器的位置
    locaPath:'./docs/.vuepress/dist/' // 本地打包文件的位置
};


//引入scp2
const client = require('scp2');
const ora = require('ora');
const fs = require('fs');
const spinner = ora('正在发布到服务器...');

const Client = require('ssh2').Client; // 创建shell脚本
const conn = new Client();

server.privateKey = fs.readFileSync("C:\\Users\\admin\\.ssh\\id_rsa")

console.log('正在建立连接');
conn.on('ready', function () {
    console.log('已连接');
    if(!server.pathNmae){
        console.log('连接已关闭');
        conn.end();
        return false;
    }
    // 这里我拼接了放置服务器资源目录的位置 ,首选通过rm -rf删除了这个目录下的文件
    conn.exec('sudo rm -rf ' + server.pathNmae + '/*', function (err, stream) {
        console.log('删除文件');
        if (err) {
            console.log('发布失败.\n');
            throw err;
        }
        stream.on('close', function (code, signal) {
            console.log('开始上传');
            spinner.start();
            client.scp(server.locaPath, {
                "host": server.host,
                "port": server.port,
                "username": server.username,
                "passphrase": server.passphrase,
                "privateKey": server.privateKey,
                "path": server.pathNmae
            }, err => {
                spinner.stop();
                if (!err) {
                    console.log('项目发布完毕');
                } else {
                    console.log("err", err)
                }
                conn.end() // 结束命令
            })
        }).on('data', function(data) {
            console.log(data)
        });
    })
}).connect({
    host: server.host,
    port: server.port,
    username: server.username,
    //password: server.password,
    passphrase: server.passphrase, //私钥密码
    privateKey: server.privateKey //使用 私钥密钥登录
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#Ubuntu
上次更新: 2023/01/12, 08:55:15
Ubuntu开启bbr
ja-netfilter配置

← Ubuntu开启bbr ja-netfilter配置→

最近更新
01
pre-push
08-07
02
commit-msg
08-07
03
pre-commit
08-07
更多文章>
Theme by Vdoing | Copyright © 2020-2024 handazao | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式