当前位置:网站首页>使用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();

原网站

版权声明
本文为[阿六啊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43957384/article/details/125432366