Skip to content

Commit

Permalink
feat: docker镜像新增git相关记录的label标记
Browse files Browse the repository at this point in the history
  • Loading branch information
142vip.cn committed Nov 4, 2023
1 parent d11ee9a commit b7d0dae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
20 changes: 18 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@ RUN if [ "$CONTAINER_BUILD" = "true" ]; then \
fi;

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

ARG APP_NAME
ARG APP_VERSION
LABEL version=$APP_VERSION description="JavaScriptCollection文档合集、博客"
LABEL author="【Github&公众号】:储凡" email="fairy@2925.com"
ARG AUTHOR
ARG EMAIL
ARG DESCRIPTION
ARG GIT_HASH
ARG GIT_MESSAGE
ARG HOME_PAGE

# 作者信息
LABEL "author.name"="$AUTHOR" "author.email"="$EMAIL"

# 项目信息
LABEL "repo.name"=$APP_NAME "repo.version"=$APP_VERSION \
"repo.homePage"="$HOME_PAGE" "repo.description"="$DESCRIPTION"

# Git信息
LABEL "git.hash"="$GIT_HASH" "git.message"="$GIT_MESSAGE"

# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面 注意:--from参数
COPY --from=build_base /apps/docs/.vuepress/dist/ /usr/share/nginx/html/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "微信公众号:储凡",
"email": "fairy_408@2925.com",
"url": "https://github.com/142vip",
"homePage": "https://www.142vip.cn"
"homePage": "https://code.142vip.cn"
},
"packageManager": "pnpm@8.9.2",
"engines": {
Expand Down
48 changes: 40 additions & 8 deletions scripts/bundle
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,69 @@
*/

const { execShell } = require('./.exec')
const { execSync } = require('child_process');
const { Select } = require('enquirer')

const packageVersion = require('../package.json').version
const projectName = 'JavaScriptCollection'
// 仓库地址
const repoAddress = 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book'

const packageInfo=require('../package.json')
const packageVersion = packageInfo.version
const projectName = packageInfo.name

// 镜像地址
const imageName = `${repoAddress}:${projectName}-${packageVersion}`


/**
* 获取最近一次Git提交信息
* - 短哈希值
* - 提交信息
*/
async function getGitInfo(){
// 执行 git log 命令获取最新一次提交的哈希值和消息
const gitLog = execSync('git log --no-merges -1 --pretty=format:"%h %s"').toString();

// 分割输出字符串以获取哈希值和消息
const [commitHash, ...commitMessage] = gitLog.trim().split(' ');

// 输出最近一次提交的信息
return {
gitHash: commitHash,
gitMessage: commitMessage.join('')
}
}

/**
* 获取构建镜像的脚本
* @param containerBuild
* @param preBuild
* @param needProxy
* @returns {string[]}
* @param containerBuild 是否容器内构建
* @param preBuild 是否预编译
* @param needProxy 是否配置代理路径
*/
function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) {
async function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) {
// 基础构建脚本
let baseBuildScript = ''

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

const {gitHash,gitMessage}=await getGitInfo()

return [
// 构建镜像
`
${baseBuildScript}
docker build \
--build-arg APP_VERSION=${packageVersion} \
--build-arg CONTAINER_BUILD=${containerBuild} \
--build-arg APP_VERSION=${packageVersion} \
--build-arg APP_NAME=${projectName} \
--build-arg HOME_PAGE=${packageInfo.authorInfo.homePage} \
--build-arg AUTHOR=${packageInfo.authorInfo.name} \
--build-arg EMAIL=${packageInfo.authorInfo.email} \
--build-arg DESCRIPTION=${packageInfo.description} \
--build-arg GIT_HASH=${gitHash} \
--build-arg GIT_MESSAGE="${gitMessage}" \
-t ${imageName} .
`,
// 推送镜像
Expand Down

0 comments on commit b7d0dae

Please sign in to comment.