当前位置:网站首页>使用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();
边栏推荐
- Neon Optimization: an instruction optimization case of matrix transpose
- C language instance_ two
- Comparison of picture beds of free white whoring
- Wood extraction in Halcon
- C语言实例_4
- AcWing 1140. 最短网络 (最小生成树)
- Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
- 736. Lisp 语法解析 : DFS 模拟题
- The cost of returning tables in MySQL
- 接收用户输入,身高BMI体重指数检测小业务入门案例
猜你喜欢
云呐-工单管理制度及流程,工单管理规范
JTAG debugging experience of arm bare board debugging
Typical problems of subnet division and super network construction
Let's see through the network i/o model from beginning to end
2022 Google CTF SEGFAULT LABYRINTH wp
Yunna | work order management software, work order management software app
2022 Google CTF segfault Labyrinth WP
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
域分析工具BloodHound的使用说明
AI automatically generates annotation documents from code
随机推荐
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Send template message via wechat official account
域分析工具BloodHound的使用说明
How to manage distributed teams?
Lldp compatible CDP function configuration
Installation of gazebo & connection with ROS
[case sharing] basic function configuration of network loop detection
Yunna - work order management system and process, work order management specification
The difference between spin and sleep
taro3.*中使用 dva 入门级别的哦
Make Jar, Not War
curl 命令
Force buckle 1037 Effective boomerang
免费白嫖的图床对比
mysqlbackup 还原特定的表
Docker method to install MySQL
[advanced C language] 8 written questions of pointer
编译命令行终端 swift
Add the applet "lazycodeloading": "requiredcomponents" in taro,
C language instance_ three