Skip to content

Commit b64a1fc

Browse files
author
微信公众号:储凡
authored
Merge pull request #95 from lir0115/feat/dockerfile-add-git
2 parents e6bac1f + b7d0dae commit b64a1fc

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

Dockerfile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@ RUN if [ "$CONTAINER_BUILD" = "true" ]; then \
2222
fi;
2323

2424
FROM registry.cn-hangzhou.aliyuncs.com/142vip/nginx:1.23.0-alpine
25+
26+
ARG APP_NAME
2527
ARG APP_VERSION
26-
LABEL version=$APP_VERSION description="JavaScriptCollection文档合集、博客"
27-
LABEL author="【Github&公众号】:储凡" email="[email protected]"
28+
ARG AUTHOR
29+
ARG EMAIL
30+
ARG DESCRIPTION
31+
ARG GIT_HASH
32+
ARG GIT_MESSAGE
33+
ARG HOME_PAGE
34+
35+
# 作者信息
36+
LABEL "author.name"="$AUTHOR" "author.email"="$EMAIL"
37+
38+
# 项目信息
39+
LABEL "repo.name"=$APP_NAME "repo.version"=$APP_VERSION \
40+
"repo.homePage"="$HOME_PAGE" "repo.description"="$DESCRIPTION"
41+
42+
# Git信息
43+
LABEL "git.hash"="$GIT_HASH" "git.message"="$GIT_MESSAGE"
2844

2945
# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面 注意:--from参数
3046
COPY --from=build_base /apps/docs/.vuepress/dist/ /usr/share/nginx/html/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "微信公众号:储凡",
77
"email": "[email protected]",
88
"url": "https://github.com/142vip",
9-
"homePage": "https://www.142vip.cn"
9+
"homePage": "https://code.142vip.cn"
1010
},
1111
"packageManager": "[email protected]",
1212
"engines": {

scripts/bundle

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,69 @@
1212
*/
1313

1414
const { execShell } = require('./.exec')
15+
const { execSync } = require('child_process');
1516
const { Select } = require('enquirer')
1617

17-
const packageVersion = require('../package.json').version
18-
const projectName = 'JavaScriptCollection'
1918
// 仓库地址
2019
const repoAddress = 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book'
20+
21+
const packageInfo=require('../package.json')
22+
const packageVersion = packageInfo.version
23+
const projectName = packageInfo.name
24+
2125
// 镜像地址
2226
const imageName = `${repoAddress}:${projectName}-${packageVersion}`
2327

28+
29+
/**
30+
* 获取最近一次Git提交信息
31+
* - 短哈希值
32+
* - 提交信息
33+
*/
34+
async function getGitInfo(){
35+
// 执行 git log 命令获取最新一次提交的哈希值和消息
36+
const gitLog = execSync('git log --no-merges -1 --pretty=format:"%h %s"').toString();
37+
38+
// 分割输出字符串以获取哈希值和消息
39+
const [commitHash, ...commitMessage] = gitLog.trim().split(' ');
40+
41+
// 输出最近一次提交的信息
42+
return {
43+
gitHash: commitHash,
44+
gitMessage: commitMessage.join('')
45+
}
46+
}
47+
2448
/**
2549
* 获取构建镜像的脚本
26-
* @param containerBuild
27-
* @param preBuild
28-
* @param needProxy
29-
* @returns {string[]}
50+
* @param containerBuild 是否容器内构建
51+
* @param preBuild 是否预编译
52+
* @param needProxy 是否配置代理路径
3053
*/
31-
function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) {
54+
async function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) {
3255
// 基础构建脚本
3356
let baseBuildScript = ''
3457

3558
if (preBuild) {
3659
baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build'
3760
}
3861

62+
const {gitHash,gitMessage}=await getGitInfo()
63+
3964
return [
4065
// 构建镜像
4166
`
4267
${baseBuildScript}
4368
docker build \
44-
--build-arg APP_VERSION=${packageVersion} \
4569
--build-arg CONTAINER_BUILD=${containerBuild} \
70+
--build-arg APP_VERSION=${packageVersion} \
71+
--build-arg APP_NAME=${projectName} \
72+
--build-arg HOME_PAGE=${packageInfo.authorInfo.homePage} \
73+
--build-arg AUTHOR=${packageInfo.authorInfo.name} \
74+
--build-arg EMAIL=${packageInfo.authorInfo.email} \
75+
--build-arg DESCRIPTION=${packageInfo.description} \
76+
--build-arg GIT_HASH=${gitHash} \
77+
--build-arg GIT_MESSAGE="${gitMessage}" \
4678
-t ${imageName} .
4779
`,
4880
// 推送镜像

0 commit comments

Comments
 (0)