众所周知,前端部署代码要么通过ci/cd部署,要么通过手动部署,但是一个人的工程有必要cicd嘛,
于是就想用ssh部署代码,写一段脚本,部署到服务器,然后解压删除文件就行了,
当然还可以弄个加日期的备份也是可以的,以下脚本还支持多服务器部署
jsconst SSH = require('ssh.js');
const servers = [
{ host: 'server1.example.com', username: 'user1', password: 'password1' },
{ host: 'server2.example.com', username: 'user2', password: 'password2' },
{ host: 'server3.example.com', username: 'user3', password: 'password3' }
];
async function compressAndUploadFile(serverIndex, localFilePath, remoteFilePath) {
const server = servers[serverIndex];
const ssh = new SSH({
host: server.host,
username: server.username,
password: server.password
});
await ssh.connect();
await ssh.execCommand(`tar -czvf ${remoteFilePath}.tar.gz ${localFilePath}`);
await ssh.putFile(`${remoteFilePath}.tar.gz`, `${remoteFilePath}.tar.gz`);
await ssh.execCommand(`tar -xzvf ${remoteFilePath}.tar.gz`);
await ssh.execCommand(`rm ${remoteFilePath}.tar.gz`);
await ssh.dispose();
}
// Example usage:
compressAndUploadFile(0, '/path/to/local/file.txt', '/path/to/remote/file.txt');
好方便,以上结果由chatgpt生成