当前位置:网站首页>使用nodejs完成判断哪些项目打包+发版
使用nodejs完成判断哪些项目打包+发版
2022-07-06 18:02:00 【阿六啊】
因为我的多个系统的代码都是在一个文件夹下,可以只修改一个其中一个系统的代码,但是却四个都发版
所以利用nodejs 完成这个需求
git fetch 从远程获取代码库 ci合并请求的⽬标分⽀名称。
git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
git diff 判断有哪些不同
git diff --name-only $CI_COMMIT_BEFORE_SHA origin/master | awk -F/ '{print $1}' | sort -u
子进程运行shell脚本
https://blog.csdn.net/cindy647/article/details/108830841
child_process:node的一个子进程api,可创建一个子进程用于执行命令
// 调用util.promisify方法,返回一个promise,如const { stdout, stderr } = await exec(‘rm -rf build’)
const runShell = async (command, exitShellByMsg = false) => {
const exec = util.promisify(child_process.exec); //
let shell = await exec(command);
let data = '';
return new Promise((resolve, reject) => {
data = shell.stdout;
if ((exitShellByMsg && shell.stdout.indexOf('shell exit') > 0) || !exitShellByMsg) {
resolve(data);
}
});
};
获取命令行参数
process.argv[2]
完整代码如下
const util = require('util');
const child_process = require('child_process'); // 用于运行shell命令
const projects = ['ishmael','isaac','hagar','sarai'];
const env = process.env;
const runShell = async (command, exitShellByMsg = false) => {
const exec = util.promisify(child_process.exec);
let shell = await exec(command);
let data = '';
return new Promise((resolve, reject) => {
data = shell.stdout;
if ((exitShellByMsg && shell.stdout.indexOf('shell exit') > 0) || !exitShellByMsg) {
resolve(data);
}
});
};
const diffChange = async () => {
await runShell("git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME"); // git fetch 从远程获取代码库 ci合并请求的⽬标分⽀名称。
const data = await runShell("git diff --name-only $CI_COMMIT_BEFORE_SHA origin/master | awk -F/ '{print $1}' | sort -u");
const diffs = data.trim().replace(/\n/g, ',').split(',');
const isNeedBuildAll = diffs.some(name => name && projects.indexOf(name) < 0); // 如果文件夹名称不在projects里,证明为共用文件夹,需要全部build
console.info('change files', diffs);
return isNeedBuildAll ? projects : projects.filter(name => {
return name && diffs.indexOf(name) > -1;
});
};
const buildChanges = async (names) => {
if (!names.length) {
console.info('no changes to build');
return;
}
let index = 0;
const build = async () => {
const name = names[index];
console.log("project--------name-------building",name)
// runShell 得到了一个Promise对象
await runShell(`./build.sh ${
name} ${
env.CI_COMMIT_SHORT_SHA}`, true)
console.log("project--------name-------buildEnd",name)
index++;
if (index <= names.length - 1) {
await build();
}
};
return build();
};
const deployChanges = async (names) => {
if (!names.length) {
console.info('no changes to deploy');
return;
}
let index = 0;
const deploy = async () => {
const name = names[index];
const envName = process.argv[2] || 'TEST'; // 项目名称
const prefix = `${
envName}_${
name.toUpperCase()}_SERVER`; // TEST_项目名_SERVER_xx || PRODUCT_项目名_SERVER_xxx
const server = env[`${
prefix}_NAME`]; // 服务器名称
const serverPWD = env[`${
prefix}_PWD`]; // 服务器密码
const serverPath = env[`${
prefix}_PATH`]; // 服务器路径
if (server) {
await runShell(`./script/ci-deploy.sh ${
name} ${
server} ${
serverPWD} ${
serverPath}`, true);
}
index++;
if (index <= names.length - 1) {
await deploy();
}
};
return deploy();
};
const main = async () => {
const buildProjectNames = process.argv[2] === 'TEST' ? await diffChange() : projects;
const projectNames = buildProjectNames.join('、');
console.info(`${
projectNames} prepare build`);
await buildChanges(buildProjectNames);
console.info(`${
projectNames} build done`);
await deployChanges(buildProjectNames);
console.info(`${
projectNames} deploy done`);
};
main();
边栏推荐
- swiper组件中使用video导致全屏错位
- 分享一个通用的so动态库的编译方法
- 交叉验证如何防止过拟合
- Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
- AcWing 1142. 繁忙的都市 题解(最小生成树)
- 云呐|工单管理软件,工单管理软件APP
- The cost of returning tables in MySQL
- 【信号与系统】
- 今日问题-2022/7/4 lambda体中修改String引用类型变量
- 736. LISP syntax parsing: DFS simulation questions
猜你喜欢
Transformation transformation operator
1123. The nearest common ancestor of the deepest leaf node
对C语言数组的再认识
今日问题-2022/7/4 lambda体中修改String引用类型变量
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Instructions for using the domain analysis tool bloodhound
1123. 最深叶节点的最近公共祖先
[case sharing] basic function configuration of network loop detection
从底层结构开始学习FPGA----FIFO IP的定制与测试
随机推荐
云呐|工单管理软件,工单管理软件APP
uva 1401 dp+Trie
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
【C语言进阶篇】指针的8道笔试题
736. Lisp 语法解析 : DFS 模拟题
云呐-工单管理制度及流程,工单管理规范
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Boot - Prometheus push gateway use
Make Jar, Not War
c语言—数组
接收用户输入,身高BMI体重指数检测小业务入门案例
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
Receive user input, height BMI, BMI detection small business entry case
【芯片方案设计】脉搏血氧仪
swiper组件中使用video导致全屏错位
Gazebo的安装&与ROS的连接
Table table setting fillet
Let's see through the network i/o model from beginning to end
Yunna | work order management software, work order management software app
NEON优化:矩阵转置的指令优化案例