|
12 | 12 | */
|
13 | 13 |
|
14 | 14 | const { execShell } = require('./.exec')
|
| 15 | +const { execSync } = require('child_process'); |
15 | 16 | const { Select } = require('enquirer')
|
16 | 17 |
|
17 |
| -const packageVersion = require('../package.json').version |
18 |
| -const projectName = 'JavaScriptCollection' |
19 | 18 | // 仓库地址
|
20 | 19 | 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 | + |
21 | 25 | // 镜像地址
|
22 | 26 | const imageName = `${repoAddress}:${projectName}-${packageVersion}`
|
23 | 27 |
|
| 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 | + |
24 | 48 | /**
|
25 | 49 | * 获取构建镜像的脚本
|
26 |
| - * @param containerBuild |
27 |
| - * @param preBuild |
28 |
| - * @param needProxy |
29 |
| - * @returns {string[]} |
| 50 | + * @param containerBuild 是否容器内构建 |
| 51 | + * @param preBuild 是否预编译 |
| 52 | + * @param needProxy 是否配置代理路径 |
30 | 53 | */
|
31 |
| -function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) { |
| 54 | +async function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) { |
32 | 55 | // 基础构建脚本
|
33 | 56 | let baseBuildScript = ''
|
34 | 57 |
|
35 | 58 | if (preBuild) {
|
36 | 59 | baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build'
|
37 | 60 | }
|
38 | 61 |
|
| 62 | + const {gitHash,gitMessage}=await getGitInfo() |
| 63 | + |
39 | 64 | return [
|
40 | 65 | // 构建镜像
|
41 | 66 | `
|
42 | 67 | ${baseBuildScript}
|
43 | 68 | docker build \
|
44 |
| - --build-arg APP_VERSION=${packageVersion} \ |
45 | 69 | --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}" \ |
46 | 78 | -t ${imageName} .
|
47 | 79 | `,
|
48 | 80 | // 推送镜像
|
|
0 commit comments