|
| 1 | +name: Release |
| 2 | + |
| 3 | +# 优化版(对比原 publish.yml):原流程只做「同步 root 版本 → npm publish」,测试要靠 |
| 4 | +# test.yml 恰好绿着。本流程把一次 v* tag 推送变成完整发布流水线: |
| 5 | +# |
| 6 | +# verify(版本号管理)→ test(双平台全量测试,矩阵同 CI)→ publish(npm)→ release(GitHub Release)→ notify(结果摘要) |
| 7 | +# |
| 8 | +# 沿用 publish.yml 的两条血泪教训,并固化成步骤: |
| 9 | +# 1. root package.json 在 git 里是编排壳(版本停在 0.7.2),真实版本在 |
| 10 | +# dsh-mneme/package.json —— npm publish 前必须显式同步 root 版本。 |
| 11 | +# 2. 「skip if exists」幂等守卫:同一版本重复推 tag 不会重复发布。 |
| 12 | +# |
| 13 | +# 新增的版本号管理规则: |
| 14 | +# - tag 必须与 dsh-mneme/package.json version 完全一致(vX.Y.Z ↔ X.Y.Z),否则直接失败; |
| 15 | +# - tag 对应版本必须在 dsh-mneme/CHANGELOG.md 里有小节(文档先行,发布时自动抽取作 Release 说明)。 |
| 16 | +# |
| 17 | +# 手动触发(workflow_dispatch)支持 dry_run:只跑 verify + test,不发布不建 Release。 |
| 18 | + |
| 19 | +on: |
| 20 | + push: |
| 21 | + tags: |
| 22 | + - 'v*' |
| 23 | + workflow_dispatch: |
| 24 | + inputs: |
| 25 | + dry_run: |
| 26 | + description: '只验证 + 测试,不发布(dry run)' |
| 27 | + type: boolean |
| 28 | + default: false |
| 29 | + |
| 30 | +permissions: |
| 31 | + contents: write |
| 32 | + |
| 33 | +jobs: |
| 34 | + # ── 1. 版本号管理:tag ↔ package.json ↔ CHANGELOG 三方一致 ────────────── |
| 35 | + verify: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + outputs: |
| 38 | + version: ${{ steps.ver.outputs.version }} |
| 39 | + steps: |
| 40 | + - uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - uses: actions/setup-node@v4 |
| 43 | + with: |
| 44 | + node-version: 24 |
| 45 | + |
| 46 | + - name: Resolve version (tag push or package.json) |
| 47 | + id: ver |
| 48 | + run: | |
| 49 | + PKG_V="$(node -p "require('./dsh-mneme/package.json').version")" |
| 50 | + if [ "$GITHUB_REF_TYPE" = "tag" ]; then |
| 51 | + TAG_V="${GITHUB_REF_NAME#v}" |
| 52 | + echo "tag=v$TAG_V, package.json=$PKG_V" |
| 53 | + if [ "$TAG_V" != "$PKG_V" ]; then |
| 54 | + echo "::error::tag v$TAG_V 与 dsh-mneme/package.json 版本 $PKG_V 不一致" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + echo "version=$PKG_V" >> "$GITHUB_OUTPUT" |
| 58 | + else |
| 59 | + echo "version=$PKG_V" >> "$GITHUB_OUTPUT" |
| 60 | + fi |
| 61 | +
|
| 62 | + # 文档更新守门:CHANGELOG 缺该版本小节 → 说明发布说明没写,阻断并提示 |
| 63 | + - name: CHANGELOG has a section for this version |
| 64 | + run: | |
| 65 | + V="${{ steps.ver.outputs.version }}" |
| 66 | + if ! grep -qE "^## \[${V}\]" dsh-mneme/CHANGELOG.md; then |
| 67 | + echo "::error::dsh-mneme/CHANGELOG.md 缺少 ## [${V}] 小节——请先补发布说明" |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + echo "CHANGELOG section found for ${V}" |
| 71 | +
|
| 72 | + # ── 2. 双平台全量测试(Windows + Linux × Node 22/24,与 CI 同矩阵)──────── |
| 73 | + test: |
| 74 | + needs: verify |
| 75 | + strategy: |
| 76 | + fail-fast: false |
| 77 | + matrix: |
| 78 | + os: [ubuntu-latest, windows-latest] |
| 79 | + node-version: [22, 24] |
| 80 | + runs-on: ${{ matrix.os }} |
| 81 | + defaults: |
| 82 | + run: |
| 83 | + working-directory: dsh-mneme |
| 84 | + steps: |
| 85 | + - uses: actions/checkout@v4 |
| 86 | + |
| 87 | + - uses: actions/setup-node@v4 |
| 88 | + with: |
| 89 | + node-version: ${{ matrix.node-version }} |
| 90 | + cache: npm |
| 91 | + cache-dependency-path: dsh-mneme/package-lock.json |
| 92 | + |
| 93 | + - name: Install dependencies |
| 94 | + run: npm ci |
| 95 | + |
| 96 | + - name: Sync check (src ↔ lib) |
| 97 | + run: node scripts/check-sync.js |
| 98 | + |
| 99 | + - name: Full test suite |
| 100 | + run: npm test |
| 101 | + |
| 102 | + # ── 3. 发布到 npm(沿用原 publish.yml 的同步与幂等守卫)────────────────── |
| 103 | + publish: |
| 104 | + needs: [verify, test] |
| 105 | + if: github.event_name == 'push' || inputs.dry_run != true |
| 106 | + runs-on: ubuntu-latest |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v4 |
| 109 | + |
| 110 | + - uses: actions/setup-node@v4 |
| 111 | + with: |
| 112 | + node-version: 24 |
| 113 | + registry-url: https://registry.npmjs.org/ |
| 114 | + |
| 115 | + - name: Registry current state (diagnostic) |
| 116 | + run: | |
| 117 | + npm view @modusensus/dsh-mneme versions --json || true |
| 118 | + npm view @modusensus/dsh-mneme time --json || true |
| 119 | +
|
| 120 | + # 血泪教训:npm 在跑 prepublishOnly 前已加载 manifest,必须显式同步 root 版本 |
| 121 | + - name: Sync root version from dsh-mneme |
| 122 | + run: | |
| 123 | + V="${{ needs.verify.outputs.version }}" |
| 124 | + echo "Syncing root package.json to v${V}" |
| 125 | + node -e "const fs=require('fs');const v=process.argv[1];const p=JSON.parse(fs.readFileSync('./package.json','utf8'));p.version=v;fs.writeFileSync('./package.json',JSON.stringify(p,null,2)+'\n')" "$V" |
| 126 | + node -p "console.log('root now:', require('./package.json').version)" |
| 127 | +
|
| 128 | + - name: Publish @modusensus/dsh-mneme (skip if exists) |
| 129 | + run: | |
| 130 | + V="${{ needs.verify.outputs.version }}" |
| 131 | + if npm view "@modusensus/dsh-mneme@${V}" version >/dev/null 2>&1; then |
| 132 | + echo "v${V} already published on registry — skipping" |
| 133 | + else |
| 134 | + npm publish --access public |
| 135 | + fi |
| 136 | + env: |
| 137 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 138 | + |
| 139 | + - name: Verify published version |
| 140 | + run: | |
| 141 | + V="${{ needs.verify.outputs.version }}" |
| 142 | + echo "Published: $(npm view "@modusensus/dsh-mneme@${V}" version)" |
| 143 | +
|
| 144 | + # ── 4. GitHub Release(按 v0.7.9 / v0.7.10 的既定格式,说明自动取自 CHANGELOG)── |
| 145 | + release: |
| 146 | + needs: [verify, test] |
| 147 | + if: github.event_name == 'push' || inputs.dry_run != true |
| 148 | + runs-on: ubuntu-latest |
| 149 | + steps: |
| 150 | + - uses: actions/checkout@v4 |
| 151 | + |
| 152 | + # 从 dsh-mneme/CHANGELOG.md 抽取本版本小节作为 Release 说明(verify 已保证存在) |
| 153 | + - name: Extract release notes from CHANGELOG |
| 154 | + run: | |
| 155 | + V="${{ needs.verify.outputs.version }}" |
| 156 | + node -e ' |
| 157 | + const fs = require("fs"); |
| 158 | + const v = process.argv[1]; |
| 159 | + const md = fs.readFileSync("dsh-mneme/CHANGELOG.md", "utf8"); |
| 160 | + const re = new RegExp("^## \\[" + v.replace(/\./g, "\\.") + "\\][^\\n]*\\n", "m"); |
| 161 | + const start = md.search(re); |
| 162 | + if (start === -1) { console.error("changelog section not found: " + v); process.exit(1); } |
| 163 | + const rest = md.slice(start + md.slice(start).indexOf("\n") + 1); |
| 164 | + const next = rest.search(/^## \[/m); |
| 165 | + const body = (next === -1 ? rest : rest.slice(0, next)).trim(); |
| 166 | + fs.mkdirSync("notes", { recursive: true }); |
| 167 | + fs.writeFileSync("notes/body.md", body + "\n"); |
| 168 | + console.log("--- extracted notes ---\n" + body.slice(0, 400)); |
| 169 | + ' "$V" |
| 170 | +
|
| 171 | + - name: Create / update GitHub Release |
| 172 | + env: |
| 173 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 174 | + VERSION: ${{ needs.verify.outputs.version }} |
| 175 | + run: | |
| 176 | + TITLE="$(head -1 notes/body.md | sed -E 's/^#+ *//; s/^\*\*//; s/\*\*$//')" |
| 177 | + if [ -z "$TITLE" ]; then TITLE="v${VERSION}"; fi |
| 178 | + if gh release view "v${VERSION}" >/dev/null 2>&1; then |
| 179 | + gh release edit "v${VERSION}" --title "v${VERSION} · ${TITLE}" --notes-file notes/body.md |
| 180 | + else |
| 181 | + gh release create "v${VERSION}" --title "v${VERSION} · ${TITLE}" --notes-file notes/body.md |
| 182 | + fi |
| 183 | +
|
| 184 | + # ── 5. 通知与运行摘要(接入外部通知时往这里加一步 webhook 即可)──────────── |
| 185 | + notify: |
| 186 | + needs: [verify, test, publish, release] |
| 187 | + if: always() |
| 188 | + runs-on: ubuntu-latest |
| 189 | + steps: |
| 190 | + - name: Job summary |
| 191 | + run: | |
| 192 | + { |
| 193 | + echo "## Release pipeline — ${{ needs.verify.outputs.version }}" |
| 194 | + echo "" |
| 195 | + echo "| job | result |" |
| 196 | + echo "|-----|--------|" |
| 197 | + echo "| verify | ${{ needs.verify.result }} |" |
| 198 | + echo "| test (win/linux × node 22/24) | ${{ needs.test.result }} |" |
| 199 | + echo "| publish (npm) | ${{ needs.publish.result }} |" |
| 200 | + echo "| release (GitHub) | ${{ needs.release.result }} |" |
| 201 | + } >> "$GITHUB_STEP_SUMMARY" |
| 202 | + # 可选:企业微信/Telegram/邮件 webhook。配置 secret(如 NOTIFY_WEBHOOK)后取消注释: |
| 203 | + # - name: Notify webhook |
| 204 | + # if: needs.publish.result == 'success' |
| 205 | + # run: | |
| 206 | + # curl -sf -X POST "${{ secrets.NOTIFY_WEBHOOK }}" \ |
| 207 | + # -H 'Content-Type: application/json' \ |
| 208 | + # -d '{"msgtype":"text","text":{"content":"dsh-mneme ${{ needs.verify.outputs.version }} 已发布"}}' |
0 commit comments