Files
gao-customer-h5/.gitea/workflows/release.yml
T
Timi 3ccb586015 ci(release): publish 失败不再静默
- curl 加 -f,4xx/5xx 直接中断,不再无条件打印 OK
- 再校验响应体的 fail/success,业务层异常同样中断
- 避免出现 tag 已创建、包却没上线的假成功
2026-11-18 00:39:15 +08:00

393 lines
15 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.
name: Release
# 发版规则:
# PR 标题 vX.X.X 合并 → 构建、上传产物、创建发布记录、立即上线
# 手动触发 publish=false → 只创建草稿,不上线,可用于演练
# 手动触发 publish=true → 与自动发版一致
on:
pull_request:
branches:
- master
types:
- closed
workflow_dispatch:
inputs:
publish:
description: 是否立即发布,false 只创建草稿
type: boolean
default: false
jobs:
release:
runs-on: act_runner_node
if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Resolve release version
id: version
run: |
pkg_version=$(node -p "require('./package.json').version")
echo "package.json 版本:$pkg_version"
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "手动触发"
echo "version=$pkg_version" >> $GITHUB_OUTPUT
echo "publish=${{ inputs.publish }}" >> $GITHUB_OUTPUT
exit 0
fi
title="${{ github.event.pull_request.title }}"
echo "PR 标题:$title"
if echo "$title" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
if [ "v$pkg_version" != "$title" ]; then
echo "FAIL: 标题 $title 与 package.json 版本 $pkg_version 不一致"
exit 1
fi
echo "version=$pkg_version" >> $GITHUB_OUTPUT
echo "publish=true" >> $GITHUB_OUTPUT
else
echo "标题不是 vX.X.X,只创建草稿"
echo "version=$pkg_version" >> $GITHUB_OUTPUT
echo "publish=false" >> $GITHUB_OUTPUT
fi
# 从 CHANGELOG.md 提取当前版本的说明,落到 release-note.txt
# 用文件而不是 step output 传递:note 是多行文本,多行 output 在各版本 runner 上行为不一致
- name: Resolve release note
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
if [ ! -f CHANGELOG.md ]; then
echo "WARN: 缺少 CHANGELOG.md,本次发布不带变更说明"
: > release-note.txt
exit 0
fi
node --input-type=module <<'NODE'
import fs from "node:fs";
const version = process.env.VERSION;
const heading = "## v" + version;
const lines = fs.readFileSync("CHANGELOG.md", "utf8").split(/\r?\n/);
const start = lines.findIndex(line => line.trim() === heading);
if (start < 0) {
console.error("WARN: CHANGELOG.md 中没有 " + heading + ",本次发布不带变更说明");
fs.writeFileSync("release-note.txt", "");
process.exit(0);
}
const LIMIT = 480;
const picked = [];
let inComment = false;
for (let i = start + 1; i < lines.length; i += 1) {
const raw = lines[i];
if (/^##\s/.test(raw)) {
break;
}
// 跳过 HTML 注释,避免注释内容占掉长度
if (inComment) {
if (raw.includes("-->")) {
inComment = false;
}
continue;
}
if (raw.includes("<!--")) {
if (!raw.includes("-->")) {
inComment = true;
}
continue;
}
if (raw.trim() === "") {
// 连续空行压成一个,只用于分隔段落
if (picked.length > 0 && picked[picked.length - 1] !== "") {
picked.push("");
}
continue;
}
// 去掉行尾空白,保留行首缩进(嵌套列表)
picked.push(raw.replace(/\s+$/, ""));
}
while (picked.length > 0 && picked[picked.length - 1] === "") {
picked.pop();
}
if (picked.length === 0) {
console.error("WARN: " + heading + " 下没有内容,本次发布不带变更说明");
}
// 按行累积到上限,不切半行,避免出现断句
const kept = [];
let total = 0;
for (const line of picked) {
const length = Array.from(line).length + (kept.length > 0 ? 1 : 0);
if (LIMIT < total + length) {
console.error("WARN: 内容超过 " + LIMIT + " 字,截断到第 " + kept.length + " 行");
break;
}
kept.push(line);
total += length;
}
// 尖括号转义:note 最终走 v-html 渲染,不能让 CHANGELOG 里的 HTML 被执行
const note = kept.join("\n").replace(/</g, "&lt;");
fs.writeFileSync("release-note.txt", note);
console.log("变更说明(" + Array.from(note).length + " 字):\n" + (note === "" ? "(空)" : note));
NODE
- name: Configure nexus auth
env:
NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
run: |
if [ -z "$NEXUS_USERNAME" ] || [ -z "$NEXUS_PASSWORD" ]; then
echo "FAIL: 缺少 secrets.NEXUS_USERNAME 或 secrets.NEXUS_PASSWORD"
exit 1
fi
auth=$(printf '%s:%s' "$NEXUS_USERNAME" "$NEXUS_PASSWORD" | base64 | tr -d '\n')
{
echo "registry=https://nexus.imyeyu.com/repository/npm-group/"
echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${auth}"
echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true"
echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${auth}"
echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true"
} > ~/.npmrc
echo "OK: nexus 认证已配置"
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
run: pnpm run build
- name: Ensure zip
run: |
if command -v zip > /dev/null 2>&1; then
exit 0
fi
apk add --no-cache zip
- name: Package bundle
id: bundle
run: |
if [ ! -f dist/index.html ]; then
echo "FAIL: dist/index.html 缺失,产物不可用于热更新"
exit 1
fi
cd dist
zip -qr ../bundle.zip .
cd ..
size=$(wc -c < bundle.zip | tr -d ' ')
sha256=$(sha256sum bundle.zip | cut -d' ' -f1)
echo "bundle 大小:$size 字节"
echo "sha256:$sha256"
echo "size=$size" >> $GITHUB_OUTPUT
echo "sha256=$sha256" >> $GITHUB_OUTPUT
- name: Upload bundle
id: upload
env:
API_BASE_URL: ${{ vars.TIMI_API_BASE_URL }}
API_TOKEN: ${{ secrets.TIMI_API_TOKEN }}
PRODUCT_ID: ${{ vars.APP_PRODUCT_ID }}
VERSION: ${{ steps.version.outputs.version }}
run: |
if [ -z "$API_BASE_URL" ] || [ -z "$API_TOKEN" ]; then
echo "FAIL: 缺少 vars.TIMI_API_BASE_URL 或 secrets.TIMI_API_TOKEN"
exit 1
fi
if [ -z "$PRODUCT_ID" ]; then
echo "FAIL: 缺少 vars.APP_PRODUCT_ID"
exit 1
fi
response=$(curl -sS -X POST "$API_BASE_URL/attach/create" \
-H "Token: $API_TOKEN" \
-F "bizType=APP_RELEASE" \
-F "bizId=$PRODUCT_ID" \
-F "name=bundle-$VERSION.zip" \
-F "file=@bundle.zip")
echo "响应:$response"
attachment_id=$(echo "$response" | node -e "
let s = '';
process.stdin.on('data', d => s += d).on('end', () => {
const body = JSON.parse(s);
const data = body.data || body;
if (!data.id) {
console.error('未取到附件 ID');
process.exit(1);
}
process.stdout.write(data.id);
});
")
echo "附件 ID:$attachment_id"
echo "attachment_id=$attachment_id" >> $GITHUB_OUTPUT
- name: Create release record
id: create
env:
API_BASE_URL: ${{ vars.TIMI_API_BASE_URL }}
API_TOKEN: ${{ secrets.TIMI_API_TOKEN }}
PRODUCT_ID: ${{ vars.APP_PRODUCT_ID }}
MIN_VERSION_CODE: ${{ vars.APP_MIN_VERSION_CODE }}
VERSION: ${{ steps.version.outputs.version }}
ARTIFACT_SIZE: ${{ steps.bundle.outputs.size }}
SHA256: ${{ steps.bundle.outputs.sha256 }}
ATTACHMENT_ID: ${{ steps.upload.outputs.attachment_id }}
NOTE_FILE: release-note.txt
run: |
if [ -z "$PRODUCT_ID" ]; then
echo "FAIL: 缺少 vars.APP_PRODUCT_ID"
exit 1
fi
payload=$(node -e "
const fs = require('fs');
const minCode = Number(process.env.MIN_VERSION_CODE || '1');
const noteFile = process.env.NOTE_FILE || 'release-note.txt';
const note = fs.existsSync(noteFile) ? fs.readFileSync(noteFile, 'utf8') : '';
process.stdout.write(JSON.stringify({
productId: process.env.PRODUCT_ID,
kind: 'BUNDLE',
versionName: process.env.VERSION,
minVersionCode: minCode,
attachmentId: process.env.ATTACHMENT_ID,
artifactSize: Number(process.env.ARTIFACT_SIZE),
sha256: process.env.SHA256,
note
}));
")
response=$(curl -sS -X POST "$API_BASE_URL/app/release/create" \
-H "Token: $API_TOKEN" \
-H "Content-Type: application/json" \
-d "$payload")
echo "响应:$response"
release_id=$(echo "$response" | node -e "
let s = '';
process.stdin.on('data', d => s += d).on('end', () => {
const body = JSON.parse(s);
const data = body.data || body;
if (!data.id) {
console.error('未取到发布 ID');
process.exit(1);
}
process.stdout.write(data.id);
});
")
echo "发布 ID:$release_id"
echo "release_id=$release_id" >> $GITHUB_OUTPUT
- name: Publish
if: ${{ steps.version.outputs.publish == 'true' }}
env:
API_BASE_URL: ${{ vars.TIMI_API_BASE_URL }}
API_TOKEN: ${{ secrets.TIMI_API_TOKEN }}
RELEASE_ID: ${{ steps.create.outputs.release_id }}
run: |
if [ -z "$RELEASE_ID" ]; then
echo "FAIL: 缺少发布记录 ID"
exit 1
fi
# -f 让 4xx/5xx 直接失败;否则下面那句 OK 会照打,tag 也会照建
response=$(curl -sS -f -X POST "$API_BASE_URL/app/release/publish?releaseId=$RELEASE_ID&channelCode=stable" \
-H "Token: $API_TOKEN") || {
echo "FAIL: 发布接口调用失败"
exit 1
}
echo "响应:$response"
# 业务层异常仍可能是 200 + fail:true,这里再兜一层
if printf '%s' "$response" | grep -qE '"fail"[[:space:]]*:[[:space:]]*true|"success"[[:space:]]*:[[:space:]]*false'; then
echo "FAIL: 发布未生效,请检查后端日志"
exit 1
fi
echo "OK: 已发布到 stable"
# 打 tag 的时机是「上线成功」而不是「PR 合并」:草稿发布和失败发布都不该留下版本标记。
# Gitea 建 release 会顺带创建 tag,所以不需要单独 git push refs/tags。
- name: Tag release
if: ${{ steps.version.outputs.publish == 'true' }}
env:
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
GITEA_SERVER_URL: ${{ github.server_url }}
GITEA_INTERNAL_URL: ${{ vars.TIMI_GITEA_INTERNAL_URL }}
GITEA_REPOSITORY: ${{ github.repository }}
VERSION: ${{ steps.version.outputs.version }}
TARGET: ${{ github.sha }}
NOTE_FILE: release-note.txt
MAX_RETRIES: 3
RETRY_DELAY: 10
run: |
if [ -z "$GITEA_TOKEN" ]; then
echo "FAIL: 缺少 secrets.RUNNER_TOKEN,无法创建 tag"
exit 1
fi
if [ -n "$GITEA_INTERNAL_URL" ]; then
api_base_url="$GITEA_INTERNAL_URL"
else
api_base_url="$GITEA_SERVER_URL"
fi
api_url="$api_base_url/api/v1/repos/$GITEA_REPOSITORY/releases"
# 用 node 拼 payload:release note 是多行 markdown,手拼 JSON 会炸
payload=$(node -e "
const fs = require('fs');
const file = process.env.NOTE_FILE;
const note = file && fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
process.stdout.write(JSON.stringify({
tag_name: 'v' + process.env.VERSION,
name: 'v' + process.env.VERSION,
target_commitish: process.env.TARGET,
draft: false,
prerelease: false,
body: note
}));
")
attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
echo "[Create release] 第 $attempt/$MAX_RETRIES 次"
http_code=$(curl -sS -o /tmp/release.json -w "%{http_code}" -X POST "$api_url" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
--connect-timeout 30 \
--max-time 60 \
-d "$payload" 2>/dev/null) || http_code="000"
echo "HTTP Status: $http_code"
# 409 表示 tag 与 release 已存在(重跑流水线时的正常情况)
if [ "$http_code" = "201" ] || [ "$http_code" = "409" ]; then
echo "OK: v$VERSION 的 tag 与 release 已就绪"
head -c 400 /tmp/release.json
echo
exit 0
fi
if [ $attempt -lt $MAX_RETRIES ]; then
echo "重试中,${RETRY_DELAY}s 后继续"
sleep $RETRY_DELAY
fi
attempt=$((attempt + 1))
done
echo "FAIL: 创建 tag / release 失败(接口或 token 权限问题)"
exit 1
- name: Summary
run: |
if [ "${{ steps.version.outputs.publish }}" = "true" ]; then
echo "版本 ${{ steps.version.outputs.version }} 已上线"
else
echo "版本 ${{ steps.version.outputs.version }} 已创建草稿,未上线"
echo "需要上线时调用 /app/release/publish?releaseId=${{ steps.create.outputs.release_id }}"
fi
echo "-----"
echo "变更说明(写入 App 更新日志):"
if [ -s release-note.txt ]; then
cat release-note.txt
else
echo "(空)CHANGELOG.md 中没有 v${{ steps.version.outputs.version }} 一节,本次发布不带更新说明"
fi
echo "-----"
echo "热更兼容最低原生版本:${{ vars.APP_MIN_VERSION_CODE }}"
echo "只有原生壳升级(增删原生插件)时才需要提高这个值,纯 JS 改动不用管"
echo "本次产物大小:${{ steps.bundle.outputs.size }} 字节"
echo "本次产物 sha256:${{ steps.bundle.outputs.sha256 }}"