Skip to content

Refactor/add check build status cli #1220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cli/bin/erda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import init from '../lib/init';
import i18n from '../lib/i18n';
import generateService from '../lib/service-generator';
import checkCliVersion from '../lib/check-cli-version';
import comparisonBuild from '../lib/comparison-build';
import checkBuildStatus from '../lib/check-build-status';

const program = new Command();

Expand Down Expand Up @@ -119,15 +119,14 @@ program
});

program
.command('comparison-build')
.command('check-build-status')
.description(
'bundle all erda ui modules to public directory, compare git commit sha with previous build, if match it will skip build and reuse last built files. Only used in pipeline build',
'compare git commit sha with previous build, if match it will skip build and reuse last built files. Only used in pipeline build',
)
.option('-s, --skip', 'skip the cli version check')
.option('-m, --module', 'module name to build')
.action(async (options) => {
await checkCliVersion(options);
comparisonBuild(options);
checkBuildStatus();
});

program.parse(process.argv);
133 changes: 133 additions & 0 deletions cli/lib/check-build-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2021 Terminus, Inc.
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* eslint-disable no-await-in-loop */
import { promisify } from 'util';
import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import { EOL } from 'os';
import { logInfo, logSuccess, logWarn, logError } from './util/log';

const asyncExec = promisify(child_process.exec);

const externalModules = ['fdp', 'admin'];
const subModules = ['market', 'uc'];
const allModules = ['shell', 'core', 'admin', 'fdp', 'market', 'uc'];

const checkBuildStatus = async () => {
const cwd = process.cwd();
const enterprisePath = path.resolve(cwd, 'erda-ui-enterprise');
const requireBuildMap = allModules.reduce<{ [k: string]: string }>((acc, module) => {
acc[`skip-${module}-build`] = 'true';
return acc;
}, {});

for (const moduleName of allModules) {
const staticPath = path.join(cwd, `public/static/${moduleName}`);
logInfo('-----------------------------------------');
logInfo('Looking for build cache at:', staticPath);
logInfo(`Start compare diff for ${moduleName}`);
const gitCwd = externalModules.includes(moduleName) ? enterprisePath : cwd;
let { stdout: headSha } = await asyncExec('git rev-parse --short HEAD', { cwd: gitCwd });
headSha = headSha.replace(/\n/, '');
let { stdout: externalHeadSha } = await asyncExec('git rev-parse --short HEAD', {
cwd: enterprisePath,
});
externalHeadSha = externalHeadSha.replace(/\n/, '');
const skipKey = `skip-${moduleName}-build`;

try {
const gitVersionPath = path.resolve(staticPath, '.git-version');
let prevGitSha = null;
let skipBuild = true;
let nextSha = headSha;
if (fs.existsSync(gitVersionPath)) {
prevGitSha = fs.readFileSync(gitVersionPath, { encoding: 'utf8' }).replace('\n', '');
logInfo('Found previous git sha:', prevGitSha);
const [prevSha, prevExternalSha] = prevGitSha.split('/');
const { stdout: diff } = await asyncExec(`git diff --name-only ${prevSha} ${headSha}`, { cwd: gitCwd });

const fileRegex = new RegExp(
`(^${subModules.includes(moduleName) ? `modules/${moduleName}` : moduleName}/.*)`,
'gm',
);
let match = fileRegex.exec(diff);
if (match) {
logInfo('File diff:');
skipBuild = false;
}
while (match) {
logInfo(match[1]);
match = fileRegex.exec(diff);
}

if (moduleName === 'shell') {
logWarn('In case shell has part of code maintained under enterprise, need to check enterprise code base');
nextSha = `${headSha}/${externalHeadSha}`;
const { stdout: externalDiff } = await asyncExec(
`git diff --name-only ${prevExternalSha} ${externalHeadSha}`,
{ cwd: enterprisePath },
);
if (new RegExp('^cmp/', 'gm').test(externalDiff) || new RegExp('^msp/', 'gm').test(externalDiff)) {
logWarn(
`Diff detected in enterprise for shell between ${prevExternalSha} and ${externalHeadSha}`,
externalDiff,
);
skipBuild = false;
} else {
logInfo('No diff found for enterprise part of shell');
}
}
}

if (skipBuild) {
logInfo(`no change detected between ${prevGitSha} and ${headSha}. will skip build ${moduleName}`);
} else {
logWarn(`Diff detected between ${prevGitSha} and ${headSha}, will start new built for ${moduleName}`);
requireBuildMap[skipKey] = nextSha;
}
} catch (error) {
logError(`Compare diff failed for ${moduleName}`, error.toString());
requireBuildMap[skipKey] = moduleName === 'shell' ? `${headSha}/${externalHeadSha}` : headSha;
}
}

const metaPath = process.env.METAFILE;
Object.keys(requireBuildMap).forEach((moduleKey) => {
fs.appendFile(metaPath!, `${moduleKey}=${requireBuildMap[moduleKey]}${EOL}`, (err) => {
if (err) {
logError('err', err);
}
logSuccess(`emit ${moduleKey} value ${requireBuildMap[moduleKey]} to METAFILE`);
});
});

const data = { version: Date.parse(new Date().toString()) };

// generate version.json
const publicPath = path.resolve(process.cwd(), 'public');
fs.mkdir(publicPath, '0777', () => {
fs.writeFile(`${publicPath}/version.json`, JSON.stringify(data), (err) => {
if (err) {
logError('version.json generated fail', err);
} else {
logSuccess('version.json generated ok.');
}
});
});
};

export default checkBuildStatus;
90 changes: 0 additions & 90 deletions cli/lib/comparison-build.ts

This file was deleted.

6 changes: 3 additions & 3 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@erda-ui/cli",
"version": "1.0.17",
"version": "1.0.23",
"description": "Command line interface for rapid Erda UI development",
"bin": {
"erda-ui": "dist/bin/erda.js"
Expand All @@ -10,7 +10,7 @@
"build": "rm -rf ./dist && tsc --version && tsc --project .",
"local": "npm run build && local-package-publisher -p",
"remove-local": "local-package-publisher -u",
"pub": "npm version patch && npm run build && npm publish",
"pub": "npm run build && npm version patch && npm publish",
"precommit": "lint-staged"
},
"repository": {
Expand All @@ -34,7 +34,7 @@
"dayjs": "^1.10.4",
"dotenv": "^8.2.0",
"i18next-scanner": "^3.0.0",
"inquirer": "^8.0.0",
"inquirer": "8.0.0",
"inquirer-select-directory": "^1.2.0",
"js-base64": "^3.6.1",
"lodash": "^4.17.21",
Expand Down
3 changes: 1 addition & 2 deletions core/webpack.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const os = require('os');
const webpack = require('webpack');
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const moment = require('moment');

const gitRevisionPlugin = new GitRevisionPlugin();
const banner = `commit: ${gitRevisionPlugin.commithash().slice(0, 6)}
branch: ${gitRevisionPlugin.branch()}
buildTime: ${moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')}
buildTime: ${new Date().toLocaleString('zh-CH', { timeZone: 'Asia/Shanghai' })}
buildBy: ${os.userInfo().username}`;

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
"JDBC",
"jsjl",
"Jupyter",
"JVMs",
"jvms",
"JVMs",
"jxcx",
"jzts",
"keyframes",
Expand All @@ -150,6 +150,7 @@
"mesos",
"metadatas",
"metadatum",
"METAFILE",
"methd",
"middlewares",
"mindmap",
Expand Down
1 change: 1 addition & 0 deletions erda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
COLLECTOR_ADDR: "collector:7076"
COLLECTOR_PUBLIC_ADDR: "collector:7076"
SCHEDULER_ADDR: "scheduler:9091"
FDP_UI_ADDR: "fdp-ui:80"
resources:
cpu: ${request_cpu:0.2}
max_cpu: 0.2
Expand Down
Loading