Skip to content

Commit dccfbcf

Browse files
author
142vip.cn
committed
feat: docker镜像新增git相关记录的label标记
1 parent 0c42bec commit dccfbcf

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

.travis.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

Dockerfile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ RUN if [ "$CONTAINER_BUILD" = "true" ]; then \
2121

2222
FROM registry.cn-hangzhou.aliyuncs.com/142vip/nginx:1.23.0-alpine
2323

24+
ARG APP_NAME
2425
ARG APP_VERSION
25-
LABEL version=$APP_VERSION description="408CSFamily合集"
26-
LABEL author="【Github&公众号】:Rong姐姐好可爱" email="fairy_408@2925.com"
26+
ARG AUTHOR
27+
ARG EMAIL
28+
ARG DESCRIPTION
29+
ARG GIT_HASH
30+
ARG GIT_MESSAGE
31+
ARG HOME_PAGE
2732

33+
# 作者信息
34+
LABEL "author.name"="$AUTHOR" "author.email"="$EMAIL"
35+
36+
# 项目信息
37+
LABEL "repo.name"=$APP_NAME "repo.version"=$APP_VERSION \
38+
"repo.homePage"="$HOME_PAGE" "repo.description"="$DESCRIPTION"
39+
40+
# Git信息
41+
LABEL "git.hash"="$GIT_HASH" "git.message"="$GIT_MESSAGE"
2842
# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面 注意:--from参数
2943
COPY --from=build_base /apps/docs/.vuepress/dist/ /usr/share/nginx/html/
3044
COPY nginx.conf /etc/nginx/

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "微信公众号:储凡",
77
"email": "fairy_408@2925.com",
88
"url": "https://github.com/142vip",
9-
"homePage": "https://www.142vip.cn"
9+
"homePage": "https://408.142vip.cn"
1010
},
1111
"packageManager": "pnpm@8.9.2",
1212
"engines": {
@@ -18,7 +18,7 @@
1818
"postinstall": "pnpm build:mark-map",
1919
"prepare": "husky install",
2020
"dev": "vuepress dev docs",
21-
"build": "./scripts/bundle build",
21+
"build": "vuepress build docs",
2222
"build:proxy": "./scripts/bundle build_proxy",
2323
"build:mark-map": "./scripts/mark-map",
2424
"deploy:vercel": "vercel --prod",

scripts/bundle

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,68 @@
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 = '408CSFamily'
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+
* 获取最近一次Git提交信息
30+
* - 短哈希值
31+
* - 提交信息
32+
*/
33+
async function getGitInfo() {
34+
// 执行 git log 命令获取最新一次提交的哈希值和消息
35+
const gitLog = execSync('git log --no-merges -1 --pretty=format:"%h %s"').toString();
36+
37+
// 分割输出字符串以获取哈希值和消息
38+
const [commitHash, ...commitMessage] = gitLog.trim().split(' ');
39+
40+
// 输出最近一次提交的信息
41+
return {
42+
gitHash: commitHash,
43+
gitMessage: commitMessage.join('')
44+
}
45+
}
46+
2447
/**
2548
* 获取构建镜像的脚本
26-
* @param containerBuild
27-
* @param preBuild
28-
* @param needProxy
29-
* @returns {string[]}
49+
* @param containerBuild 是否容器内构建
50+
* @param preBuild 是否预编译
51+
* @param needProxy 是否配置代理路径
3052
*/
31-
function getBuildImageScript({containerBuild, preBuild, needProxy = false}) {
53+
async function getBuildImageScript({containerBuild, preBuild, needProxy = false}) {
3254
// 基础构建脚本
3355
let baseBuildScript = ''
3456

3557
if (preBuild) {
3658
baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build'
3759
}
3860

61+
const {gitHash, gitMessage} = await getGitInfo()
62+
3963
return [
4064
// 构建镜像
4165
`
4266
${baseBuildScript}
4367
docker build \
44-
--build-arg APP_VERSION=${packageVersion} \
4568
--build-arg CONTAINER_BUILD=${containerBuild} \
69+
--build-arg APP_VERSION=${packageVersion} \
70+
--build-arg APP_NAME=${projectName} \
71+
--build-arg HOME_PAGE=${packageInfo.authorInfo.homePage} \
72+
--build-arg AUTHOR=${packageInfo.authorInfo.name} \
73+
--build-arg EMAIL=${packageInfo.authorInfo.email} \
74+
--build-arg DESCRIPTION=${packageInfo.description} \
75+
--build-arg GIT_HASH=${gitHash} \
76+
--build-arg GIT_MESSAGE="${gitMessage}" \
4677
-t ${imageName} .
4778
`,
4879
// 推送镜像

0 commit comments

Comments
 (0)