小团队轻量发布脚本,一键部署静态资源。
迭代了好几个发布流程,webhook的自动发布,但部署起来麻烦。机器上执行脚本,需要登机器执行。利用Xftp手动上传打包文件,先打包后拖拽也麻烦。计划部署个jenkins,又存在维护成本。
目前这个版本相对来说更轻量一点。
配置流程如下,先加载依赖,然后配置好机器密码,静态资源存放路径。
建议在.gitignore上添加此文件,因为有机器密码不要提交到码仓。
命名为deploy.js, node deploy.js 就OK啦。
/**
* 自动发布脚本 node deploy.js
*
* 自行修改config配置文件 host username passwork staticPath:静态资源存放路径
* 预先加载依赖 npm install chalk sftpcopy --save -dev
*/
const config = {
host: '100.93.2.89',
port: 22,
username: 'root',
password: '*********',
staticPath: '/root/web/static', //静态资源存放路径
};
const { exec } = require('child_process');
const chalk = require('chalk');
const { resolve } = require("path");
const localDir = resolve(__dirname, "dist");
const sftpcopy = require("sftpcopy");
class Deploy {
constructor(config) {
this.config = config;
this.build();
}
build() {
console.log(chalk.green('开始执行构建...'));
console.log(chalk.green('构建参数: ' + JSON.stringify(this.config)));
exec("npm run build", (err, stdout, stderr) => {
if (err) {
console.log(chalk.red('构建失败,错误输出: ' + stderr));
} else {
console.log(chalk.green('构建成功,开始上传文件到目标文件夹:' + this.config.host));
this.push();
}
});
}
push() {
let { host, port, username, password, staticPath } = this.config;
const config = {
host,
port,
username,
password,
copys: [
{
type: "dir",
from: resolve(localDir),
to: staticPath
},
]
};
sftpcopy(config).then(res => {
console.log(chalk.green('success!'));
}).catch(err => {
console.log(chalk.green('error!', err));
})
}
}
new Deploy(config);