当前位置:网站首页>使用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();
边栏推荐
- 一起看看matlab工具箱内部是如何实现BP神经网络的
- Docker method to install MySQL
- 云呐|工单管理软件,工单管理软件APP
- Body mass index program, entry to write dead applet project
- 1123. 最深叶节点的最近公共祖先
- Comparison of picture beds of free white whoring
- 图片打水印 缩放 和一个输入流的转换
- C language instance_ five
- Using the entry level of DVA in taro3.*
- Implementation principle of waitgroup in golang
猜你喜欢

一起看看matlab工具箱内部是如何实现BP神经网络的

AI 从代码中自动生成注释文档

Boot - Prometheus push gateway use

1123. 最深叶节点的最近公共祖先

AcWing 361. 观光奶牛 题解(spfa求正环)

1123. The nearest common ancestor of the deepest leaf node

Let's see through the network i/o model from beginning to end

Yunna | work order management software, work order management software app

Send template message via wechat official account

The MySQL database in Alibaba cloud was attacked, and finally the data was found
随机推荐
MySQL script batch queries all tables containing specified field types in the database
405 method not allowed appears when the third party jumps to the website
Oracle: Practice of CDB restricting PDB resources
C# 计算农历日期方法 2022
增加 pdf 标题浮窗
7.6模拟赛总结
Vocabulary in Data Book
Table table setting fillet
Install Firefox browser on raspberry pie /arm device
hdu 4661 Message Passing(木DP&amp;组合数学)
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
[case sharing] basic function configuration of network loop detection
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Taro2.* applet configuration sharing wechat circle of friends
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
HMM 笔记
2022 Google CTF segfault Labyrinth WP
Failed to successfully launch or connect to a child MSBuild. exe process. Verify that the MSBuild. exe
2022 Google CTF SEGFAULT LABYRINTH wp
微信公众号发送模板消息