|
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 = '408CSFamily' |
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 | + * 获取最近一次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 | + |
24 | 47 | /** |
25 | 48 | * 获取构建镜像的脚本 |
26 | | - * @param containerBuild |
27 | | - * @param preBuild |
28 | | - * @param needProxy |
29 | | - * @returns {string[]} |
| 49 | + * @param containerBuild 是否容器内构建 |
| 50 | + * @param preBuild 是否预编译 |
| 51 | + * @param needProxy 是否配置代理路径 |
30 | 52 | */ |
31 | | -function getBuildImageScript({containerBuild, preBuild, needProxy = false}) { |
| 53 | +async function getBuildImageScript({containerBuild, preBuild, needProxy = false}) { |
32 | 54 | // 基础构建脚本 |
33 | 55 | let baseBuildScript = '' |
34 | 56 |
|
35 | 57 | if (preBuild) { |
36 | 58 | baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build' |
37 | 59 | } |
38 | 60 |
|
| 61 | + const {gitHash, gitMessage} = await getGitInfo() |
| 62 | + |
39 | 63 | return [ |
40 | 64 | // 构建镜像 |
41 | 65 | ` |
42 | 66 | ${baseBuildScript} |
43 | 67 | docker build \ |
44 | | - --build-arg APP_VERSION=${packageVersion} \ |
45 | 68 | --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}" \ |
46 | 77 | -t ${imageName} . |
47 | 78 | `, |
48 | 79 | // 推送镜像 |
|
0 commit comments