当前位置:网站首页>使用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();
边栏推荐
- 各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
- 2022 Google CTF SEGFAULT LABYRINTH wp
- NEON优化:矩阵转置的指令优化案例
- 第三方跳转网站 出现 405 Method Not Allowed
- [signal and system]
- C语言实例_5
- 免费白嫖的图床对比
- Yunna | work order management software, work order management software app
- C language instance_ three
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
猜你喜欢

Js逆向——捅了【马蜂窝】的ob混淆与加速乐

The MySQL database in Alibaba cloud was attacked, and finally the data was found

2022 Google CTF SEGFAULT LABYRINTH wp

移植DAC芯片MCP4725驱动到NUC980

Analysis of mutex principle in golang

从底层结构开始学习FPGA----FIFO IP的定制与测试

Yunna - work order management system and process, work order management specification
![[signal and system]](/img/aa/a65d6da1d1d9410254ca7b775e24a6.png)
[signal and system]

How to manage distributed teams?

MySQL script batch queries all tables containing specified field types in the database
随机推荐
2022 Google CTF SEGFAULT LABYRINTH wp
WCF基金会
移植DAC芯片MCP4725驱动到NUC980
Typical problems of subnet division and super network construction
7.6 simulation summary
Boot - Prometheus push gateway use
Docker method to install MySQL
Yunna - work order management system and process, work order management specification
Analysis of mutex principle in golang
身体质量指数程序,入门写死的小程序项目
修改px4飞控的系统时间
hdu 4661 Message Passing(木DP&amp;组合数学)
Instructions for using the domain analysis tool bloodhound
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
[signal and system]
1123. The nearest common ancestor of the deepest leaf node
【C语言进阶篇】指针的8道笔试题
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
swiper组件中使用video导致全屏错位