Files
eng-standards-main/templates/gitea-build.yml
T
姚章浩 9d7f09ff4b fix(template): 通用 CI 模板改成 CI-only;去掉假成功占位;修四处会造成假绿的写法
模板是"默认就合规"的起点,所以它的每一处错误都会被复制到每个新仓。

① 删掉 deploy:test 作业:它打印 TODO 然后返回成功 —— 网页上看起来部署过了,实际什么都没做。
   那是假报成功,不是"还没做"。模板从此只做 CI,部署由项目另写独立 workflow。
② gitea-prod-deploy.yml 里的两个 TODO 步骤同理,改成 exit 1 并在文件头写明"红的占位是设计"。
③ 假绿四处:
   - gofmt -l 列出文件时退出码仍是 0 ⇒ 改成判输出是否为空;
   - 缺 package-lock.json 时回落 npm install ⇒ 改成直接失败(npm install 会重写锁文件);
   - GOFLAGS=-mod=mod ⇒ 改成 -mod=readonly,并加"编译前后比对依赖清单摘要";
   - 识别不到任何构建入口时打印"跳过编译"然后成功 ⇒ 改成失败。
④ 检出改成核对到**事件完整 SHA**(原来取的是"当时的分支 HEAD",事件之后又有人推一次就对不上了)。
⑤ 预览加了三条断言:包里真的有 index.html、报告里每个地址都 200、不再产生 `.` 根成员
   (原来会:通知发了、预览 404 而流水线显示成功)。

验证:模板 YAML 用 pyyaml 解析通过(build 模板 jobs=gate,build,无 deploy 作业;
prod-deploy jobs=guard,publish 且只有 workflow_dispatch);
new-project.sh 生成物里 grep '^  deploy' 计数为 0。
2026-10-12 00:11:40 +08:00

244 lines
13 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# <PROJECT> —— 提交触发的 CI(Gitea Actions)
# 来自 zheromiku/eng-standards-main 的 templates/gitea-build.yml;正本见 01-规范-Git与CI-CD(Gitea版).md §4
#
# ⚠️ 本模板**只做 CI**:不含任何部署作业,也不启动 compose / 不改线上。
# CD(部署测试/生产)是**每个项目自己设计**的独立 workflow(正本 §8)。
# 这里**不再**放 `deploy:test` 占位骨架 —— 占位作业会打印 TODO 然后返回成功,
# 那不是"还没做",那是**假报成功**(规范里明确禁止)。
#
# ⚠️ 四处形状不许动(动了会被规范判为不合规):
# · 触发只有:推任意分支(校验+编译,不部署)、workflow_dispatch
# · 提交信息闸门(gate)必须在编译之前,且**独立作业**(失败不编译,也便于一眼看出是信息问题不是代码问题)
# · 生产部署**不在这里**(独立 workflow,只由 workflow_dispatch 触发,见 §5.2)
# · 不用第三方 action(`uses:`)—— 本 runner 取不到就随机红;全部逻辑写在 run: 里
#
# ⚠️ 本 runner 实测特性(act 语义,见正本 §4.4):
# · act_runner 不会自动检出代码 ⇒ 必须自己 git init + fetch + checkout
# · 每步以 `bash -e -o pipefail` 执行 ⇒ 禁止 `cmd | head`(SIGPIPE 假红)
# · 工具链来自主机只读挂载:容器内 /opt/citools;可写缓存 /opt/citools-cache
#
# ⚠️ 三条"不许假绿"的写法(都踩过):
# · 判"输出即问题"的工具(gofmt -l / prettier --list-different)**要看输出**,不能看退出码
# · 缺锁文件 ⇒ 失败,**不回落** `npm install`(那会静默重写锁文件)
# · 识别不到任何构建入口 ⇒ 失败,**不**打印"跳过编译"然后成功
name: build
on:
push:
branches:
- "**"
workflow_dispatch:
env:
BRIDGE_URL: ${{ vars.CI_BRIDGE_URL }}
BRIDGE_TOKEN: ${{ secrets.CI_BRIDGE_TOKEN }}
GIT_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LANG: C.UTF-8
LC_ALL: C.UTF-8
# 同一 ref 的新推送取消旧的**校验/编译**运行(注意:生产部署 workflow 不许这么设,见 §5.4)
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: true
jobs:
gate:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: 检出代码(固定到事件完整 SHA)
run: |
set -uo pipefail
SERVER="${GITHUB_SERVER_URL:-http://192.168.50.223:3000}"
HOSTPORT="${SERVER#*://}"
BRANCH="${GITHUB_REF_NAME:-${GITHUB_REF#refs/heads/}}"
EVENT_SHA="${GITHUB_SHA}"
cd "${GITHUB_WORKSPACE:-$(pwd)}"
rm -rf ./* .git 2>/dev/null || true
git init -q .
git remote add origin "http://x-access-token:${GIT_TOKEN}@${HOSTPORT}/${GITHUB_REPOSITORY}.git"
git fetch -q --depth 50 origin "$BRANCH"
git checkout -q -B "$BRANCH" FETCH_HEAD
if [ "$(git rev-parse HEAD)" != "$EVENT_SHA" ]; then
git fetch -q --depth 50 origin "$EVENT_SHA" && git checkout -q --detach FETCH_HEAD \
|| { echo "❌ 取不到事件 SHA $EVENT_SHA"; exit 1; }
fi
# ⚠️ 必须比对**完整** SHA:分支 HEAD 会随时前进,"构建成功"必须对应到某个确定的提交
[ "$(git rev-parse HEAD)" = "$EVENT_SHA" ] \
|| { echo "❌ 检出 SHA 与事件 SHA 不一致:$(git rev-parse HEAD) / $EVENT_SHA"; exit 1; }
echo "已检出 ${GITHUB_REPOSITORY} 分支 ${BRANCH} @ ${EVENT_SHA:0:12}(== 事件完整 SHA)"
git log -1 --format=' %h %an %s'
- name: 提交信息规范校验(权威闸门,失败不编译)
run: python3 .gitea/scripts/ci_gate.py
build:
needs: [gate]
runs-on: ubuntu-latest
timeout-minutes: 40
steps:
- name: 检出代码(固定到事件完整 SHA)
run: |
set -uo pipefail
SERVER="${GITHUB_SERVER_URL:-http://192.168.50.223:3000}"
HOSTPORT="${SERVER#*://}"
BRANCH="${GITHUB_REF_NAME:-${GITHUB_REF#refs/heads/}}"
EVENT_SHA="${GITHUB_SHA}"
cd "${GITHUB_WORKSPACE:-$(pwd)}"
rm -rf ./* .git 2>/dev/null || true
git init -q .
git remote add origin "http://x-access-token:${GIT_TOKEN}@${HOSTPORT}/${GITHUB_REPOSITORY}.git"
git fetch -q --depth 50 origin "$BRANCH"
git checkout -q -B "$BRANCH" FETCH_HEAD
if [ "$(git rev-parse HEAD)" != "$EVENT_SHA" ]; then
git fetch -q --depth 50 origin "$EVENT_SHA" && git checkout -q --detach FETCH_HEAD \
|| { echo "❌ 取不到事件 SHA $EVENT_SHA"; exit 1; }
fi
[ "$(git rev-parse HEAD)" = "$EVENT_SHA" ] \
|| { echo "❌ 检出 SHA 与事件 SHA 不一致"; exit 1; }
git rev-parse HEAD > /tmp/ci-sha
git rev-parse --short=12 HEAD > /tmp/ci-sha12
date +%s > /tmp/ci-start
: > /tmp/ci-preview-url
echo skipped > /tmp/ci-build-status
echo skipped > /tmp/ci-gate-status
git rev-list --count FETCH_HEAD > /tmp/ci-commits-count 2>/dev/null || echo 1 > /tmp/ci-commits-count
# 依赖清单摘要:编译完再比一次,抓"某一步顺手改了 go.mod / 锁文件"
for f in go.mod go.sum package.json package-lock.json pnpm-lock.yaml yarn.lock requirements.txt; do
[ -f "$f" ] && printf '%s %s\n' "$(sha256sum "$f" | cut -c1-16)" "$f"
done > /tmp/ci-manifests-before
- name: 编译与自检
run: |
set +e; set +o pipefail
LOG=/tmp/ci-build.log; : > "$LOG"
run() { echo "-- $* --"; "$@" 2>&1 | tee -a "$LOG"; return "${PIPESTATUS[0]}"; }
runin() { local d="$1"; shift; echo "-- ($d) $* --"; ( cd "$d" && "$@" ) 2>&1 | tee -a "$LOG"; return "${PIPESTATUS[0]}"; }
export PATH=/opt/citools/go/bin:/opt/citools/bin:$PATH
export GOCACHE=/opt/citools-cache/gocache GOMODCACHE=/opt/citools-cache/gomod
export UV_CACHE_DIR=/opt/citools-cache/uv GOPATH=/opt/citools-cache/gopath
export npm_config_cache=/opt/citools-cache/npm
# ⚠️ GOFLAGS 必须是 readonly:`-mod=mod` 会在构建时**静默改写** go.mod/go.sum
export GOPROXY=https://goproxy.cn,direct GOTOOLCHAIN=local GOFLAGS=-mod=readonly
mkdir -p "$GOCACHE" "$GOMODCACHE" "$UV_CACHE_DIR" "$GOPATH" 2>/dev/null
echo "工具链: $(go version 2>&1) | $(uv --version 2>&1) | $(node -v 2>&1) | $(python3 -V 2>&1)"
fail=0; ran=""
if [ -f go.mod ]; then
ran="$ran go"
# ⚠️ `gofmt -l` 列出文件时**退出码仍是 0** —— 只看退出码就是永远的假绿
UGLY="$(gofmt -l $(git ls-files '*.go') 2>/dev/null)"
if [ -n "$UGLY" ]; then echo "❌ 这些 Go 文件没格式化:"; printf '%s\n' "$UGLY"; fail=1; fi
run go vet ./... || fail=1
run go build ./... || fail=1
run go test ./... || fail=1
fi
if [ -f pyproject.toml ] || [ -f requirements.txt ] || [ -f setup.py ]; then
ran="$ran python"
run ruff format --check . || fail=1
run ruff check . || fail=1
if [ -f requirements.txt ]; then run uv pip install -r requirements.txt || fail=1; fi
run uv run pytest -q || fail=1
fi
WEBPKG=""
for p in package.json web/package.json frontend/package.json ui/package.json; do
if [ -f "$p" ]; then WEBPKG="$p"; break; fi
done
if [ -n "$WEBPKG" ]; then
ran="$ran node"; D="$(dirname "$WEBPKG")"
# ⚠️ 缺锁文件就失败:`npm install` 会按 package.json 重算依赖树并**重写锁文件**
if [ -f "$D/package-lock.json" ]; then
runin "$D" npm ci || fail=1
else
echo "❌ $D 下没有 package-lock.json:不接受无约束安装(先提交锁文件)"; fail=1
fi
if grep -q '"format:check"[[:space:]]*:' "$WEBPKG" 2>/dev/null; then
runin "$D" npm run format:check || fail=1
fi
if grep -q '"build"[[:space:]]*:' "$WEBPKG" 2>/dev/null; then
runin "$D" npm run build || fail=1
fi
if grep -q '"test"[[:space:]]*:' "$WEBPKG" 2>/dev/null; then
runin "$D" npm test || fail=1
fi
fi
if [ -z "$ran" ]; then
# ⚠️ 这里必须失败:识别不到入口 = 什么都没验 = 不能报成功
echo "❌ 未识别到任何构建入口(go.mod / pyproject.toml / package.json 都没有)"
echo " 要么把入口补上,要么在项目清单里声明'不适用'并写明理由 —— 不允许静默跳过。"
fail=1
fi
# 依赖清单不许被编译过程改写
for f in go.mod go.sum package.json package-lock.json pnpm-lock.yaml yarn.lock requirements.txt; do
[ -f "$f" ] && printf '%s %s\n' "$(sha256sum "$f" | cut -c1-16)" "$f"
done > /tmp/ci-manifests-after
if ! diff -q /tmp/ci-manifests-before /tmp/ci-manifests-after >/dev/null; then
echo "❌ 依赖清单在编译过程中被改写了(下面是对比):"; diff /tmp/ci-manifests-before /tmp/ci-manifests-after
echo " 这通常是 GOFLAGS=-mod=mod 或缺锁回落 npm install 造成的;改写必须由人显式提交。"
fail=1
fi
if [ "$fail" -eq 0 ]; then echo success > /tmp/ci-build-status; else echo failure > /tmp/ci-build-status; fi
tail -c 4000 "$LOG" > /tmp/ci-build-tail
echo "构建结果: $(cat /tmp/ci-build-status)(识别:$ran)"
[ "$fail" -eq 0 ] || exit 1
- name: 发布网页预览(只在全部必需检查通过后)
if: always()
run: |
set +e; set +o pipefail
: > /tmp/ci-preview-url
if [ "$(cat /tmp/ci-build-status 2>/dev/null)" != "success" ]; then
echo "编译未成功,跳过预览发布"; exit 0
fi
WEB=""
for d in web/dist dist build out public web/build frontend/dist; do
if [ -f "$d/index.html" ]; then WEB="$d"; break; fi
done
if [ -z "$WEB" ]; then echo "未发现静态网页产物目录,跳过预览发布"; exit 0; fi
# ⚠️ 打包不要产生 `.` 根成员:先 cd 再让通配展开成员名(否则预览 404 而流水线显示成功)
( cd "$WEB" && { shopt -s dotglob; tar -czf /tmp/ci-web.tgz *; } ) || { echo "打包失败"; exit 0; }
tar -tzf /tmp/ci-web.tgz | grep -qx 'index.html' \
|| { echo "❌ 包里没有 index.html ⇒ 发出去也是 404 的预览,不发"; exit 0; }
B="${BRIDGE_URL:-}"
for cand in "$B" "http://172.17.0.1:8899" "http://192.168.50.134:8899" "http://100.92.49.78:8899"; do
if [ -n "$cand" ]; then curl -s -m 5 -o /dev/null "$cand/healthz" && { B="$cand"; break; }; fi
done
if [ -z "$B" ]; then echo "❌ 没有可达的 ci-bridge ⇒ 不发预览"; exit 0; fi
SHA="$(cat /tmp/ci-sha)"
echo "发布预览到 $B(产物目录 $WEB)"
CODE="$(curl -sS -m 90 -o /tmp/ci-pub.json -w '%{http_code}' -X POST \
"$B/api/ci/publish?repo=${GITHUB_REPOSITORY}&sha=${SHA}" \
-H "X-CI-Token: ${BRIDGE_TOKEN}" -H 'Content-Type: application/gzip' \
--data-binary @/tmp/ci-web.tgz)"
echo "publish HTTP $CODE $(cat /tmp/ci-pub.json)"
[ "$CODE" = "200" ] || { echo "❌ 发布接口返回 $CODE"; exit 0; }
for key in url url_tailscale; do
U="$(python3 -c "import json,sys;print(json.load(open('/tmp/ci-pub.json')).get(sys.argv[1],''))" "$key")"
[ -n "$U" ] || { echo "⚠️ 报告里 $key 为空"; continue; }
C="$(curl -s -o /dev/null -w '%{http_code}' -m 10 "$U")"
echo "预览探测 $key: $U → $C"
[ "$C" = "200" ] || { echo "❌ $U 不是 200(通知里给出打不开的链接等于没给)"; exit 0; }
[ "$key" = "url" ] && echo "$U" > /tmp/ci-preview-url
done
echo "✅ 预览已发布,且每个地址都 200"
- name: 上报 ci-bridge(通知失败不改变质量结果)
if: always()
run: |
set +e; set +o pipefail
B="${BRIDGE_URL:-}"
for cand in "$B" "http://172.17.0.1:8899" "http://192.168.50.134:8899" "http://100.92.49.78:8899"; do
if [ -n "$cand" ]; then curl -s -m 5 -o /dev/null "$cand/healthz" && { B="$cand"; break; }; fi
done
echo "使用 ci-bridge: $B"
S=$(date +%s); T=$(cat /tmp/ci-start 2>/dev/null); [ -n "$T" ] || T=$S
echo $((S - T)) > /tmp/ci-duration
CI_BRIDGE_RESOLVED="$B" python3 .gitea/scripts/ci_report.py