如何在 Nexus 中为 Docker 镜像附加测试报告元数据
解读
国内大多数公司把 Nexus 作为私有 Docker Registry,同时又要满足“质量门禁”——测试报告必须与镜像一起被审计。
面试官真正想知道的是:
- 你是否理解 Docker Manifest 与 OCI Image Spec 的元数据机制;
- 能否在不破坏镜像可移植性的前提下,把单元、接口、性能、安全四类测试报告挂到镜像上;
- 是否熟悉 Nexus3 的 Component-Domain 存储模型与 REST API v1 和 v2 的差异;
- 有没有工程化落地经验:CI 里哪一步写元数据、如何防并发覆盖、如何按版本清理。
回答时要体现“测试开发”视角:不是简单 push,而是把质量数据左移到镜像本身,让 SRE、安全、合规团队都能一键消费。
知识点
- OCI Image Spec v1.1:manifest、config、index 三层结构,config 的 Labels 与 Annotations 字段最大 4 K/128 K 限制。
- Nexus3 BlobStore 与 Component 元数据:Docker 格式实际存成两个 Component(layer + manifest),自定义属性只能挂在 Component 上,不能写进 Blob。
- Nexus3 Pro API:/service/rest/v1/components/{id}/attributes 可 PATCH 自定义键值;开源版无此接口,需通过 nexus-script-plugin 执行 Groovy 脚本写入。
- Docker Labels 与 BuildKit:
docker buildx build --label git.commit=$(git rev-parse HEAD) --label test.report="http://..."可写进 config,但 URL 长度受限。 - ORAS (OCI Registry As Storage):把测试报告当成 artifact,使用 mediaType application/vnd.test.report.v1+json 推送,与镜像共享同一个 repo,不同 tag。
- 签名与不可变 Tag:Nexus3 的 Docker Bearer Token Realm 与 Content Selectors 配合,可禁止覆盖已带测试报告的 tag。
- 清理策略:基于 nexus-task-logic 写 Groovy,扫描无 report 的 Component,保留最近 5 个版本,其余删除,防止磁盘爆炸。
- 合规审计:通过 Nexus3 Audit Log 与 ELK 对接,把“谁上传、谁挂报告、谁审批”三要素落库,满足国内等保 2.0 对“镜像可追溯”要求。
答案
分三步落地,兼顾开源版与 Pro 版差异。
第一步:CI 侧把报告转成标准格式
单元测试用 junit.xml,接口测试用 allure-report,性能测试用 jtl+png,安全测试用 sarif.json。统一压缩成 test-report.{git-short-sha}.tar.gz,计算 sha256 并生成 test-report.sha256 文件,防止篡改。
第二步:根据 Nexus 版本选方案
A. Pro 版(国内金融、运营商常见)
- 镜像 push 完成后,调用
curl -X PATCH -u admin:pwd https://nexus3.corp.io/service/rest/v1/components/{componentId}/attributes -d '{"test.unit":"PASS","test.performance":"7450tps","test.security":"0Critical","test.report_url":"http://qa.corp.io/reports/20240528-143052/index.html"}'
其中 componentId 通过/v1/components?repository=docker-hosted&name=myapp获取。 - 在 Nexus 前端配置 Content Selector:
format == "docker" and attributes.test.security != "0Critical"拒绝 pull,实现质量门禁。
B. 开源版(中小公司)
- 使用 nexus-script-plugin 预置 Groovy 脚本
attachTestMeta.groovy,接受参数repoName、imageTag、reportUrl、sha256。 - 脚本内部调用
transaction.withTransaction { repositoryManager.get(repoName).facet(ComponentMaintenance).updateComponent(component, { it.attributes().child('test').set('reportUrl', reportUrl) }) } - Jenkins 通过
ssh nexus-admin@nexus3 'curl -X POST -d @attachTestMeta.json http://localhost:8081/service/rest/v1/script/attachTestMeta/run'触发,保证内网安全。
第三步:消费侧统一入口
- 研发本地
docker pull前,执行公司 CLI:
corpctl image inspect myapp:1.2.3 --check-test
CLI 后台调 Nexus API,若缺少test.security属性或值非“0Critical”,直接阻断并提示“请先通过安全扫描”。 - 生产发布系统(如 Spinnaker)在 Bake Manifest 阶段注入
metadata.annotations["test.report/url"],实现 GitOps 可追溯。
通过以上流程,测试报告成为镜像不可分割的元数据,满足国内“上线必附报告”的合规要求,同时不污染镜像层,保持 OCI 标准兼容。
拓展思考
- 如果测试报告大于 128 K,无法塞进 Label,你会如何用 OCI Reference Types 把报告作为单独 artifact 并与镜像建立 digest 级关联?请给出 ORAS 推送命令与 Nexus 配置要点。
- 当 Nexus3 采用 S3 BlobStore 做后端,频繁 PATCH 属性会导致 Nexus 内嵌 OrientDB 事务锁竞争,如何改用 Kafka 队列异步写并保证最终一致性?
- 国内银行要求“测试报告国密签名”,你需要在 CI 里调用 SM2 签名生成
.sig文件,再把签名值写进 Nexus 属性。如何设计密钥托管与验签流程,既满足 DevSecOps 又不把私钥落盘?