cloudflare 加载git信息
发表于|更新于
|总字数:1.1k|阅读时长:6分钟|浏览量:
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}`; }
const gitHash = execSync('git rev-parse --short HEAD').toString().trim();
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
const commitDateStr = execSync('git log -1 --format=%cd').toString().trim(); const commitDate = convertTime(new Date(commitDateStr));
const buildTime = convertTime(new Date());
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 }); }
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}`; }
const gitHash = execSync('git rev-parse --short HEAD').toString().trim();
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
const commitDateStr = execSync('git log -1 --format=%cd').toString().trim(); const commitDate = convertTime(new Date(commitDateStr));
const buildTime = convertTime(new Date());
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);
|