Skip to content

Commit 62f412c

Browse files
XmmShpx6eull
andauthored
Dev/subtiltle docker support (#21)
* fix(Docker): 修复了docker构建时无法读到 git 信息的问题 * fix: 纠正构建版本信息中的拼写错误 * feat(subtitle): 在Docker环境下显示"Docker 构建"副标题 * 优化副标题逻辑 --------- Co-authored-by: x6eull <[email protected]>
1 parent f5a09ed commit 62f412c

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ COPY ./package*.json ./
55
COPY ./.npmrc ./
66
RUN npm install
77
COPY . .
8+
ENV VITE_IS_DOCKER_ENVIRONMENT=true
89
ENV VITE_ENABLE_RUNTIME_CONFIG=true
910
RUN npm run build
1011

vite.config.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ import react from '@vitejs/plugin-react-swc';
33
/**@ts-ignore */
44
import { execSync } from 'node:child_process';
55

6-
function sh(cmd: string) {
7-
return execSync(cmd, { encoding: 'utf-8' }).trim();
6+
function sh(cmd: string, fallback = '[unknown]') {
7+
try {
8+
return execSync(cmd, { encoding: 'utf-8' }).trim();
9+
} catch {
10+
return fallback;
11+
}
812
}
13+
/**@ts-ignore */
14+
const isDocker: boolean = process.env.VITE_IS_DOCKER_ENVIRONMENT?.match(/true|1/i);
915
const headSha = sh('git rev-parse HEAD');
1016
const headCommitCount = sh('git rev-list --count HEAD');
1117
const headBranch = sh('git rev-parse --abbrev-ref HEAD');
12-
const anyChanges = sh('git status --porcelain -uall').length > 0;
18+
const anyChanges = sh('git status --porcelain -uall', '').length > 0;
19+
const BUILD_INFO = isDocker ? '使用 Docker 构建' : `构建版本 {${headCommitCount}${anyChanges ? '*' : ''}}`;
20+
1321
// https://vitejs.dev/config/
1422
export default defineConfig({
1523
define: {
16-
'import.meta.env.VITE_BUILD_INFO': JSON.stringify(`构建版本 ${headCommitCount}${anyChanges ? '*' : ''}`),
17-
'import.meta.env.VITE_BUILD_INFO_DETAIL': JSON.stringify(`${anyChanges ? '有未提交的更改\n' : ''}branch: ${headBranch}\ncommit: ${headSha}`),
24+
'import.meta.env.VITE_BUILD_INFO': JSON.stringify(BUILD_INFO),
25+
'import.meta.env.VITE_BUILD_INFO_DETAIL': JSON.stringify(
26+
`${anyChanges ? '有未提交的更改\n' : ''}提交总数: ${headCommitCount}\nbranch: ${headBranch}\ncommit: ${headSha}`
27+
),
1828
},
1929
plugins: [react()],
2030
css: {

0 commit comments

Comments
 (0)