64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
import { Client } from 'ssh2';
|
|
import { spawn } from 'child_process';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const conn = new Client();
|
|
|
|
const buildDir = path.join(path.dirname(import.meta.url).replace('file:///', ''), 'dist/build/h5');
|
|
const tarFile = path.join(path.dirname(import.meta.url).replace('file:///', ''), 'h5.tar.gz');
|
|
|
|
console.log('=== 部署H5代码 ===\n');
|
|
|
|
const tar = spawn('tar', ['-czf', tarFile, '-C', buildDir, '.']);
|
|
|
|
tar.on('close', (code) => {
|
|
if (code !== 0) {
|
|
console.log('压缩失败');
|
|
return;
|
|
}
|
|
console.log('压缩完成');
|
|
|
|
conn.on('ready', () => {
|
|
console.log('连接成功');
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) { console.log('SFTP错误:', err); conn.end(); return; }
|
|
|
|
const readStream = fs.createReadStream(tarFile);
|
|
const writeStream = sftp.createWriteStream('/tmp/h5.tar.gz');
|
|
|
|
writeStream.on('close', () => {
|
|
console.log('上传完成');
|
|
|
|
conn.exec('rm -rf /www/huishou/dist/build/h5/* && tar -xzf /tmp/h5.tar.gz -C /www/huishou/dist/build/h5/ && docker restart huishou-backend', (err, stream) => {
|
|
if (err) { console.log('执行命令错误:', err); conn.end(); return; }
|
|
|
|
let output = '';
|
|
stream.on('data', (d) => {
|
|
output += d.toString();
|
|
console.log('输出:', d.toString().trim());
|
|
});
|
|
|
|
stream.on('close', (code) => {
|
|
console.log('部署完成, 退出码:', code);
|
|
conn.end();
|
|
});
|
|
});
|
|
});
|
|
|
|
readStream.pipe(writeStream);
|
|
});
|
|
});
|
|
|
|
conn.on('error', (err) => {
|
|
console.log('连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect({
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
});
|
|
}); |