cloudflare加载git信息

前言

我之前会把git的信息获取到后塞到head

笔记博客关于我jenkins自动化迁移到cloudflare后发现获取git的参数有问题

cloudflare编译的结果git分支会显示HEAD,git提交数永远显示1

开始

经过我一系列的测试,发现在构建命令前增加几个命令即可

1
git fetch --unshallow && git fetch origin && git checkout main && git pull origin main

附录

获取git参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const fs = require('fs');
const { execSync } = require('child_process');
const path = require('path');

function convertTime(date) {
date.setUTCHours(date.getUTCHours() + 8);
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
const hours = String(date.getUTCHours()).padStart(2, '0');
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
const seconds = String(date.getUTCSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 获取 Git 哈希
const gitHash = execSync('git rev-parse --short HEAD').toString().trim();
// 获取 Git 分支
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
// 获取 Git 提交时间
const commitDateStr = execSync('git log -1 --format=%cd').toString().trim();
const commitDate = convertTime(new Date(commitDateStr));
// 获取构建时间
const buildTime = convertTime(new Date());
// 获取 Git 提交次数
const commitCount = execSync('git rev-list --count HEAD').toString().trim();

const buildInfo = {
buildTime,
gitBranch,
gitHash,
commitCount,
commitDate
};

// 确保目录存在
const outputDir = path.join(__dirname, '../default_data');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

// 写入为 JavaScript 文件,包含 const 变量
const jsContent = `export const buildInfo = ${JSON.stringify(buildInfo, null, 2)};`;

fs.writeFileSync(path.join(outputDir, 'buildInfo.js'), jsContent);
console.log('构建信息已保存为 JavaScript 文件:', buildInfo);

react中注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const App = () => {
// 判断当前环境是生产环境还是开发环境
const isProduction = process.env.NODE_ENV === 'production';
return (
<>
{isProduction && (
<Helmet>
{/* 动态注入构建信息到 <head> */}
<meta name="git-hash" content={buildInfo.gitHash}/>
<meta name="git-branch" content={buildInfo.gitBranch}/>
<meta name="commit-date" content={buildInfo.commitDate}/>
<meta name="commit-count" content={buildInfo.commitCount}/>
<meta name="build-time" content={buildInfo.buildTime}/>
</Helmet>
)}
</>
)
}

vuepress中注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 如果是生产环境,则注入构建信息到 head
let headConfig = [
['meta', {name: 'author', content: author}],
['meta', {name: 'description', content: description}],
];

// 如果是 build,就不打印
if (process.env.NODE_ENV === 'production') {
headConfig = [
...headConfig,
['meta', {name: 'git-branch', content: buildInfo.gitBranch}],
['meta', {name: 'git-commit-sha', content: buildInfo.gitHash}],
['meta', {name: 'git-commit-time', content: buildInfo.commitDate}],
['meta', {name: 'git-commit-count', content: buildInfo.commitCount}],
['meta', {name: 'build-time', content: buildInfo.buildTime}],
['meta', {name: 'time-zone', content: timeZone}],
['script', {
src: 'https://umami.tteam.icu/script.js',
async: 'async',
defer: 'defer',
'data-website-id': '6e757c22-77d9-495a-85b6-d2cbd2efcbb3',
}],
];
}

// 然后在配置中填入变量

hexo中注入

hexo中有点特殊,需要用官方的Injector

把获取脚本放入cmd目录即可实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require('fs');
const { execSync } = require('child_process');
const path = require('path');

function convertTime(date) {
date.setUTCHours(date.getUTCHours() + 8);
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
const hours = String(date.getUTCHours()).padStart(2, '0');
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
const seconds = String(date.getUTCSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 获取 Git 哈希
const gitHash = execSync('git rev-parse --short HEAD').toString().trim();
// 获取 Git 分支
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
// 获取 Git 提交时间
const commitDateStr = execSync('git log -1 --format=%cd').toString().trim();
const commitDate = convertTime(new Date(commitDateStr));
// 获取构建时间
const buildTime = convertTime(new Date());
// 获取 Git 提交次数
const commitCount = execSync('git rev-list --count HEAD').toString().trim();

const buildInfo = {
buildTime,
gitBranch,
gitHash,
commitCount,
commitDate
};

// 确保目录存在
const outputDir = path.join(__dirname, '../scripts');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

const jsContent = `hexo.extend.injector.register('head_begin', '<meta name="git-branch" content="${gitBranch}">', 'default');
hexo.extend.injector.register('head_begin', '<meta name="git-commit-sha" content="${gitHash}">', 'default');
hexo.extend.injector.register('head_begin', '<meta name="git-commit-time" content="${commitDate}">', 'default');
hexo.extend.injector.register('head_begin', '<meta name="git-commit-count" content="${commitCount}">', 'default');
hexo.extend.injector.register('head_begin', '<meta name="build-time" content="${buildTime}">', 'default');`;

fs.writeFileSync(path.join(outputDir, 'gitData.js'), jsContent);
console.log('构建信息:', buildInfo);