feat(取证): 开机自报这份 jar 出自哪笔 commit

制品名自始没 bump 过(aiimage-backend-0.0.1-SNAPSHOT.jar),pom 也没有 git-commit-id,
所以出问题时光看线上机器答不出"在跑的是哪版代码"——10-07 那次是靠"哪个类只有那笔才有"人工认的。

不用 git-commit-id 插件:现网出包是 mvn -o 离线,新插件当场解析失败,等于给发布通道加一根
只有联网才通的管子。改成打包前由 scripts/_git_stamp.py 生成 src/main/resources/git.properties
(已加进 .gitignore,入库就成孤儿构建产物,target/classes 里那个孤儿 V148 就是这么来的),
开机念 [build-info];念不到时打 [build-info-missing] 而不是静默当干净构建。

拒绝的职责不在这里:一个元数据文件读不到不该把在跑实例打死,那是发布闸门的事。
This commit is contained in:
2026-10-08 17:36:32 +08:00
parent 305441b0bd
commit 520044c8a4
3 changed files with 233 additions and 0 deletions
@@ -0,0 +1,94 @@
package com.nanri.aiimage.config;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
/**
* 开机把「这个 jar 是从哪一笔代码打出来的」念出来。
*
* <p>存在的理由:制品名自始没 bump 过({@code aiimage-backend-0.0.1-SNAPSHOT.jar}),pom 也没接
* git-commit-id,所以出问题时光看线上机器无法回答"现在跑的是哪版代码"——2026-10-07 那次 41 分钟
* 宕机就是靠"哪个类只有那笔才有"人工认出来的。章由 {@code scripts/_git_stamp.py write} 在打包前
* 生成,随 jar 落在 {@code BOOT-INF/classes/git.properties}。
*
* <p>读不到时**不许静默**:打 {@link #MISSING_ANCHOR} 一行 WARN,巡检(scripts/_prod_watch.py 的
* SIGNATURES)能按这个锚点计数。真正的拦在发布闸门({@code _git_stamp.py verify}),不在这里——
* 这里拒绝启动会把一个只是没盖章的在跑实例打死,代价和收益不成比例。
*/
@Component
public class BuildInfoLogger {
static final String RESOURCE = "git.properties";
static final String MISSING_ANCHOR = "[build-info-missing]";
static final String INFO_ANCHOR = "[build-info]";
private static final Logger log = LoggerFactory.getLogger(BuildInfoLogger.class);
/** 一份构建章。{@code commit} 为空串表示章在但没盖住。 */
record BuildInfo(String commit, String branch, String dirty, String buildTime) {
boolean usable() {
return commit != null && !commit.isBlank();
}
}
/**
* 读 classpath 上的章。返回 null 代表**文件不存在**;
* 存在但内容空则返回一个 {@link #BuildInfo#usable()} 为 false 的对象——
* 这两种情况在日志里必须分开,否则"忘了打包前盖章"和"章被清空"会混成同一条。
*/
static BuildInfo read(Resource resource) {
if (!resource.exists()) {
return null;
}
Properties p = new Properties();
try (InputStream in = resource.getInputStream()) {
p.load(new InputStreamReader(in, StandardCharsets.UTF_8));
} catch (IOException ex) {
log.warn("{} 读到 {} 但解析失败:{}", MISSING_ANCHOR, RESOURCE, ex.getMessage());
return new BuildInfo("", "", "", "");
}
return new BuildInfo(p.getProperty("commit", "").trim(), p.getProperty("branch", "").trim(),
p.getProperty("dirty", "").trim(), p.getProperty("build_time", "").trim());
}
/** 一行话的描述,供日志与测试共用(测试拿它断言"脏包不会被念成干净")。 */
static String describe(BuildInfo info) {
if (info == null) {
return "commit=unknown(jar 内无 " + RESOURCE + ",这份包是跳过 _git_stamp.py write 打出来的)";
}
if (!info.usable()) {
return "commit=empty(" + RESOURCE + " 里 commit 键为空,等于没盖章)";
}
return "commit=" + info.commit().substring(0, Math.min(12, info.commit().length()))
+ " branch=" + (info.branch().isEmpty() ? "?" : info.branch())
+ " dirty=" + info.dirty()
+ " built=" + (info.buildTime().isEmpty() ? "?" : info.buildTime());
}
@EventListener(ApplicationReadyEvent.class)
public void logOnBoot() {
logIt(read(new ClassPathResource(RESOURCE)));
}
/** 与 {@link #logOnBoot()} 分开,是为了让测试能直接喂三种形状进来,不必伪造 classpath。 */
String logIt(BuildInfo info) {
String line = describe(info);
if (info == null || !info.usable() || "true".equalsIgnoreCase(info.dirty())) {
log.warn("{} {}", MISSING_ANCHOR, line);
} else {
log.info("{} {}", INFO_ANCHOR, line);
}
return line;
}
}