Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ astro build 每项目详情页 / 列表页读「人工
| `HUB_PROJECTS_DIR` | `src/data/projects` | 数据文件目录(演练/测试用) |
| `HUB_META_CACHE` | `src/data/cache/github-meta.json` | 自动字段缓存位置 |
| `HUB_META_REPORT` | `github-meta-report.json` | 本次抓取报告(未入库,CI 可取用) |
| `HUB_VERSION` | 空 | 覆盖页脚版本号;缺省现场取 `git describe --tags --always --dirty` |
| `HUB_VERSION` | 空 | 覆盖页脚版本号(回退链最优先一级);缺省按 `build-info.ts` 的回退链取值 |
| `HUB_SKIP_OG` | 空 | 置 `1` 跳过 OG 图本地化(离线构建/演练用;缺图自动回退首字图) |
| `HUB_OG_FORCE` | 空 | 置 `1` 强制重新抓取仓库 OG 图 |
| `HUB_OG_REPORT` | `og-report.json` | 本次 OG 图本地化报告(未入库) |
Expand Down Expand Up @@ -207,7 +207,7 @@ tests/ 单测 + 进程级集成测试 + 夹具
- **构建摘要必含**(§9.2 / §9.3):自动字段刷新结果(成功 / 缓存兜底 / 降级置空 / 数据错误 + 逐项降级原因)、archive 与超过 365 天未更新的异常项目清单、外链巡检结果与**连续失败点名**(阈值:连续 2 次)、两侧发布结果。
- **不自动改数据**:异常项目与坏链只进摘要,是否下架/修正走人工数据 PR;工作流**不向 `main` 写提交**(自动字段缓存如有变化,作为 `reports` 产物上传,由维护者随数据 PR 提交)。
- **密钥红线**:CF Pages deploy hook URL 只放 GitHub Secrets,名为 `CLOUDFLARE_PAGES_DEPLOY_HOOK`;`build` 里有一道自检,扫到仓库文件里出现 deploy hook URL 直接失败;日志只打印 HTTP 状态码,不回显 URL。
- **版本号一致性**(§12 第 10 条):页脚版本号 = 构建时 `git describe --tags --always`(`src/lib/build-info.ts`)。两侧都从同一提交构建,所以值相同;需要固定时可用 `HUB_VERSION` 覆盖。刻意**不用 `--dirty`**:构建本身会改写自动字段缓存文件,带 `--dirty` 会让每次 CI 构建都得到 `<sha>-dirty`,两侧无法比较。
- **版本号一致性**(§12 第 10 条):页脚版本号由 `src/lib/build-info.ts` 一段代码统一取值,主站与镜像共用、优先级相同,回退链为 ① `HUB_VERSION` → ② `git describe --tags --always` → ③ `CF_PAGES_COMMIT_SHA` 截 7 位 → ④ `v0.0.0-dev`。镜像侧走 Actions 完整历史,命中 ②;主站侧 CF Pages 是浅克隆,`git describe` 必然失败,由 CF Pages 构建环境自带的 `CF_PAGES_COMMIT_SHA` 截 7 位接住,形态与 ② 的短 SHA 一致,两侧页脚可比对。**生产不需要也不应该在控制台手填 `HUB_VERSION`**(① 只留给本地/特殊覆盖)。刻意**不用 `--dirty`**:构建本身会改写自动字段缓存文件,带 `--dirty` 会让每次 CI 构建都得到 `<sha>-dirty`,两侧无法比较。

### 演练(不发出真实发布)

Expand Down
2 changes: 1 addition & 1 deletion src/lib/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* GITHUB_API_BASE GitHub API 基址(默认 https://api.github.com;演练时指向不可达地址)
* GITHUB_TOKEN CI 工作流自带只读 token(可选,本地缺省走未认证额度)
* HUB_SKIP_FETCH 置 1 时完全跳过抓取,只用缓存/空值(演练降级路径最简方式)
* HUB_VERSION 覆盖页脚版本号(CI 注入 git describe;本地缺省现场取)
* HUB_VERSION 覆盖页脚版本号(优先级最高;生产不用于钉死版本,取值链见 build-info.ts)
*/
import { resolve } from 'node:path';

Expand Down
56 changes: 48 additions & 8 deletions src/lib/build-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/**
* 版本号与构建时间(SPEC §0 / §12 第 8 条:页脚版本号由构建时注入,仓库源码里不写死)。
*
* 版本号取 `git describe --tags --always`;CI 可用 HUB_VERSION 覆盖
* (浅克隆场景下由工作流带 fetch-depth: 0 保证 describe 可用)。
* 主站(Cloudflare Pages)与镜像(GitHub Pages)**共用本文件这一段取值逻辑**,
* 不各写一套(§12 第 10 条「双线同版」的前提)。回退链优先级固定为:
* ① `HUB_VERSION` 仅本地 / 特殊场景覆盖,生产**不**用它钉死版本
* ② `git describe --tags --always`
* ③ `CF_PAGES_COMMIT_SHA` Cloudflare Pages 构建环境原生变量,截短为 7 位
* ④ `v0.0.0-dev` 以上均不可用时的占位符
*
* ②→③ 这一跳是本文件存在的原因:CF Pages 的 Git 集成用浅克隆(shallow clone),
* 构建环境里没有 tag 也没有完整历史,`git describe` 必然失败;而 CF Pages 自身
* 提供 `CF_PAGES_COMMIT_SHA`(本次构建对应的完整提交 SHA),截 7 位后与镜像侧
* `git describe --always` 的无 tag 输出形态一致(短 SHA),两侧页脚即可比对。
*
* 刻意**不带 `--dirty`**(双线部署需要):构建本身会改写自动字段缓存
* `src/data/cache/github-meta.json`,带 `--dirty` 会让每次 CI 构建都得到
Expand All @@ -13,24 +22,55 @@ import { execFileSync } from 'node:child_process';

const UNKNOWN_VERSION = 'v0.0.0-dev';

function readGitDescribe(): string {
const injected = process.env.HUB_VERSION?.trim();
if (injected) return injected;
/** ③ 的截短长度:与 ② 在无 tag 仓库下的短 SHA 形态(默认 7 位)对齐。 */
const SHORT_SHA_LENGTH = 7;

/** ② 的取值器;取不到返回 undefined(而不是抛错),便于回退到 ③。 */
export type DescribeVersion = () => string | undefined;

function readGitDescribe(): string | undefined {
try {
return execFileSync('git', ['describe', '--tags', '--always'], {
const described = execFileSync('git', ['describe', '--tags', '--always'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
return described || undefined;
} catch {
return UNKNOWN_VERSION;
// 浅克隆 / 无 .git / 非仓库目录:交给回退链的下一级
return undefined;
}
}

/**
* 按冻结契约的回退链解析版本号。纯函数:环境变量与 `git describe` 取值器都可注入,
* 方便单测覆盖「无 git 元数据 + 有 CF_PAGES_COMMIT_SHA」这类只能在 CI 里复现的分支。
*/
export function resolveVersion(
env: NodeJS.ProcessEnv = process.env,
describe: DescribeVersion = readGitDescribe,
): string {
// ① HUB_VERSION:显式覆盖优先,本地构建与特殊场景用
const injected = env.HUB_VERSION?.trim();
if (injected) return injected;

// ② git describe --tags --always
const described = describe()?.trim();
if (described) return described;

// ③ Cloudflare Pages 构建环境原生变量,截短为 7 位短 SHA
// 统一转小写:`git describe` 输出的是小写短 SHA,两条线要比对同一个值(§12 第 10 条)
const commitSha = env.CF_PAGES_COMMIT_SHA?.trim().toLowerCase();
if (commitSha) return commitSha.slice(0, SHORT_SHA_LENGTH);

// ④ 全都不可用才回退占位符
return UNKNOWN_VERSION;
}

let cached: { version: string; builtAt: string } | undefined;

export function getBuildInfo(): { version: string; builtAt: string } {
cached ??= {
version: readGitDescribe(),
version: resolveVersion(),
builtAt: new Date().toISOString().replace('T', ' ').slice(0, 16) + ' UTC',
};
return cached;
Expand Down
60 changes: 60 additions & 0 deletions tests/build-info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 页脚版本号回退链的单测(SPEC §0 / §12 第 8、10 条):
* ① HUB_VERSION → ② git describe → ③ CF_PAGES_COMMIT_SHA 截 7 位 → ④ v0.0.0-dev。
* 主站(CF Pages 浅克隆,`git describe` 必失败)与镜像(完整历史)共用这段逻辑,
* 所以「③ 生效且形态与镜像的短 SHA 一致」是双线同版的关键一条。
*/
import assert from 'node:assert/strict';
import { test } from 'node:test';

import { resolveVersion } from '../src/lib/build-info.js';

const FULL_SHA = '10f8fa9c4e2b7d3a5f60718293a4b5c6d7e8f901';

/** 模拟「有 git 元数据、describe 成功」。 */
const describeOk = (value: string) => () => value;
/** 模拟「浅克隆 / 无 .git,describe 失败」。 */
const describeFails = () => undefined;

test('① HUB_VERSION 覆盖优先于 git describe(本地覆盖不退化)', () => {
assert.equal(resolveVersion({ HUB_VERSION: 'v1.2.3' }, describeOk('10f8fa9')), 'v1.2.3');
assert.equal(resolveVersion({ HUB_VERSION: ' v1.2.3 ' }, describeFails), 'v1.2.3');
});

test('② git describe 成功时直接采用(镜像侧路径)', () => {
assert.equal(resolveVersion({}, describeOk('10f8fa9')), '10f8fa9');
assert.equal(resolveVersion({}, describeOk('v1.0.0-3-gabcdef1')), 'v1.0.0-3-gabcdef1');
});

test('③ git describe 失败时取 CF_PAGES_COMMIT_SHA 并截短为 7 位(主站侧路径)', () => {
const version = resolveVersion({ CF_PAGES_COMMIT_SHA: FULL_SHA }, describeFails);
assert.equal(version, '10f8fa9');
assert.equal(version.length, 7);
assert.notEqual(version, 'v0.0.0-dev');
});

test('③ 短 SHA 形态与镜像侧 git describe 输出一致(双线可比对)', () => {
assert.equal(
resolveVersion({ CF_PAGES_COMMIT_SHA: FULL_SHA }, describeFails),
resolveVersion({}, describeOk('10f8fa9')),
);
});

test('② 优先于 ③:两者都可用时不看 CF_PAGES_COMMIT_SHA', () => {
assert.equal(
resolveVersion({ CF_PAGES_COMMIT_SHA: FULL_SHA }, describeOk('v1.0.0-3-gabcdef1')),
'v1.0.0-3-gabcdef1',
);
});

test('④ 全都不可用才回退 v0.0.0-dev', () => {
assert.equal(resolveVersion({}, describeFails), 'v0.0.0-dev');
assert.equal(
resolveVersion({ HUB_VERSION: ' ', CF_PAGES_COMMIT_SHA: ' ' }, describeFails),
'v0.0.0-dev',
);
});

test('空字符串的 describe 结果不算命中,继续走回退链', () => {
assert.equal(resolveVersion({ CF_PAGES_COMMIT_SHA: FULL_SHA }, describeOk('')), '10f8fa9');
});
Loading