102 lines
3.0 KiB
JavaScript
102 lines
3.0 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
const { execSync } = require('child_process');
|
|
|
|
const config = {
|
|
host: 'hs.yifenbao.cn',
|
|
port: 22,
|
|
username: 'root',
|
|
password: 'Torch1112'
|
|
};
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', async () => {
|
|
console.log('SSH连接成功');
|
|
|
|
try {
|
|
console.log('\n=== 1. 创建tar.gz压缩包 ===');
|
|
const tarPath = './h5-build.tar.gz';
|
|
|
|
execSync(`cd dist\\build\\h5 && tar -czf ../../../h5-build.tar.gz assets static index.html`, { windowsHide: true });
|
|
|
|
const stats = fs.statSync(tarPath);
|
|
console.log(`压缩包大小: ${(stats.size / 1024 / 1024).toFixed(2)} MB`);
|
|
|
|
console.log('\n=== 2. 上传压缩包到服务器 ===');
|
|
await new Promise((resolve) => {
|
|
conn.sftp((err, sftp) => {
|
|
if (err) {
|
|
console.error('SFTP错误:', err.message);
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const readStream = fs.createReadStream(tarPath);
|
|
const writeStream = sftp.createWriteStream('/www/huishou/h5-build.tar.gz');
|
|
|
|
writeStream.on('close', () => {
|
|
console.log('压缩包上传完成');
|
|
sftp.end();
|
|
resolve();
|
|
});
|
|
|
|
readStream.pipe(writeStream);
|
|
});
|
|
});
|
|
|
|
console.log('\n=== 3. 在服务器上解压 ===');
|
|
await executeCommand('cd /www/huishou && rm -rf dist/build/h5/* && tar -xzf h5-build.tar.gz -C dist/build/h5/');
|
|
|
|
console.log('\n=== 4. 检查解压后的文件 ===');
|
|
await executeCommand('ls -la /www/huishou/dist/build/h5/assets/index-CFIPh5xV.js');
|
|
|
|
console.log('\n=== 5. 重启容器 ===');
|
|
await executeCommand('cd /www/huishou && docker-compose down && docker-compose up -d');
|
|
|
|
console.log('\n=== 6. 等待服务启动 ===');
|
|
await sleep(10000);
|
|
|
|
console.log('\n=== 7. 测试H5访问 ===');
|
|
await executeCommand('curl -s http://localhost:4001/h5/');
|
|
|
|
console.log('\n=== 8. 测试JS资源 ===');
|
|
await executeCommand('curl -s -o /dev/null -w "JS: %{http_code}, Size: %{size_download} bytes\n" http://localhost:4001/h5/assets/index-CFIPh5xV.js');
|
|
|
|
console.log('\n=== 清理临时文件 ===');
|
|
fs.unlinkSync(tarPath);
|
|
await executeCommand('rm /www/huishou/h5-build.tar.gz');
|
|
|
|
} catch (error) {
|
|
console.error('部署失败:', error.message);
|
|
} finally {
|
|
conn.end();
|
|
}
|
|
});
|
|
|
|
function executeCommand(cmd) {
|
|
return new Promise((resolve) => {
|
|
conn.exec(cmd, (err, stream) => {
|
|
if (err) {
|
|
console.error('命令执行错误:', err.message);
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
stream.stdout.on('data', (data) => process.stdout.write(data));
|
|
stream.stderr.on('data', (data) => process.stdout.write(data));
|
|
|
|
stream.on('close', () => resolve());
|
|
});
|
|
});
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
conn.on('error', (err) => {
|
|
console.error('SSH连接错误:', err.message);
|
|
});
|
|
|
|
conn.connect(config); |