Skip to content

Commit 0353c1c

Browse files
authored
feat: Generate app build info after yarn install (#270)
1 parent 8b2c51b commit 0353c1c

File tree

9 files changed

+49
-49
lines changed

9 files changed

+49
-49
lines changed

.build-info.generate.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const dayjs = require('dayjs');
2+
const fs = require('fs');
3+
4+
const getContent = () => {
5+
const getCommitHashShort = () =>
6+
require('child_process')
7+
.execSync('git rev-parse --short HEAD')
8+
.toString()
9+
.trim();
10+
11+
const getCommitHash = () =>
12+
require('child_process').execSync('git rev-parse HEAD').toString().trim();
13+
14+
return {
15+
display: getCommitHashShort(),
16+
version: `${getCommitHashShort()} - ${dayjs().format()}`,
17+
commit: getCommitHash(),
18+
date: dayjs().format(),
19+
};
20+
};
21+
22+
const generateAppBuild = () => {
23+
try {
24+
fs.writeFileSync(
25+
'./.build-info.json',
26+
JSON.stringify(getContent(), null, 2)
27+
);
28+
console.log('✅ Generate `.build-info.json`');
29+
} catch (error) {
30+
console.error(error);
31+
throw new Error('❌ Failed to generate `.build-info.json`');
32+
}
33+
};
34+
35+
generateAppBuild();

.github/workflows/github-ci.yml

-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ jobs:
3737
- name: Install local dependencies
3838
run: yarn install
3939

40-
- name: Build the build version
41-
run: |
42-
sed -i "s/__DEVELOPMENT__/$GITHUB_RUN_ID ($(date +"%Y-%m-%d"))/g" app-build.json
43-
sed -i "s/__VERSION__/PIPELINE $GITHUB_RUN_ID/g" app-build.json
44-
sed -i "s/__COMMIT__/$GITHUB_SHA/g" app-build.json
45-
sed -i "s/__DATE__/$(date +"%Y-%m-%d %T")/g" app-build.json
46-
4740
- name: Check coding rules and types
4841
run: yarn lint
4942

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ yarn-error.log*
4444

4545
# cypress
4646
/cypress/videos
47+
48+
# Build info
49+
.build-info.json

.gitlab-ci.yml

-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ build:
1414
script:
1515
- echo "Installing dependencies"
1616
- yarn install
17-
- echo "Building application version"
18-
- sed -i "s/__DEVELOPMENT__/$CI_PIPELINE_ID ($(date +"%Y-%m-%d"))/g" app-build.json
19-
- sed -i "s/__VERSION__/PIPELINE $CI_PIPELINE_ID - JOB $CI_JOB_ID/g" app-build.json
20-
- sed -i "s/__COMMIT__/$CI_COMMIT_SHORT_SHA/g" app-build.json
21-
- sed -i "s/__DATE__/$(date +"%Y-%m-%d %T")/g" app-build.json
2217
- echo "Building application"
2318
- yarn build
2419

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
FROM node:16-alpine AS deps
33
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
44
RUN apk add --no-cache libc6-compat
5+
RUN apk add --no-cache git
56
WORKDIR /app
67
COPY . .
78
RUN yarn install --frozen-lockfile
89

910
# Rebuild the source code only when needed
1011
FROM node:16-alpine AS builder
12+
RUN apk add --no-cache git
1113
WORKDIR /app
1214
COPY . .
1315
COPY --from=deps /app/node_modules ./node_modules

app-build.json

-6
This file was deleted.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
},
1111
"scripts": {
1212
"prepare": "husky install",
13-
"postinstall": "yarn theme:generate-typing",
13+
"postinstall": "yarn build:info && yarn theme:generate-typing",
1414
"test": "cypress open",
1515
"test:ci": "cypress run --component",
1616
"dev": "yarn docs:build && next dev",
17-
"build": "yarn docs:build && next build",
17+
"build": "yarn build:info && yarn docs:build && next build",
18+
"build:info": "node .build-info.generate.js",
1819
"start": "next start",
1920
"pretty": "prettier -w .",
2021
"lint": "eslint ./src --fix && tsc --noEmit",

src/app/layout/AccountMenu/index.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ import {
2525
} from 'react-icons/fi';
2626
import { Link, useNavigate } from 'react-router-dom';
2727

28-
import appBuild from '@/../app-build.json';
28+
import buildInfo from '@/../.build-info.json';
2929
import { useAccount } from '@/app/account/account.service';
3030
import { Icon } from '@/components/Icons';
3131

3232
const AppVersion = ({ ...rest }) => {
3333
const { t } = useTranslation();
3434

35-
const { hasCopied, onCopy } = useClipboard(JSON.stringify(appBuild, null, 2));
35+
const { hasCopied, onCopy } = useClipboard(
36+
JSON.stringify(buildInfo, null, 2)
37+
);
3638

37-
if (!appBuild?.version) {
39+
if (!buildInfo?.version) {
3840
return null;
3941
}
4042

@@ -91,7 +93,7 @@ const AppVersion = ({ ...rest }) => {
9193
</Flex>
9294
<Text as="span" noOfLines={2}>
9395
{t('layout:accountMenu.version.label')}{' '}
94-
<strong>{appBuild?.display ?? appBuild?.version}</strong>
96+
<strong>{buildInfo?.display ?? buildInfo?.version}</strong>
9597
</Text>
9698
</Flex>
9799
</>

vercel.js

-25
This file was deleted.

0 commit comments

Comments
 (0)