From 5bde85a60829949d04a411db78be2aee7dacb943 Mon Sep 17 00:00:00 2001 From: bitdan Date: Thu, 26 Nov 2026 21:08:21 +0800 Subject: [PATCH] fix(distribution): regenerate the site download manifest on every deploy docs/_data/downloads.json was committed by hand and the release pipeline only refreshed the copy in the tap repository, so the terminal installers' site fallback would keep installing 0.3.2 after the next release. Build and deploy the site from GitHub Actions instead of the legacy branch build. Before the Jekyll build, scripts/site-downloads.mjs reads the latest stable release and writes the manifest from the SHA-256 digests GitHub records for each asset. The workflow redeploys after the Release workflow finishes (workflow_run runs on master, which the github-pages environment requires) and checks that the rendered TSV lists every asset before publishing. The installers now read the site manifest first and fall back to the tap copy, since raw.githubusercontent.com is unreachable on some networks. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/pages.yml | 84 +++++++++++++++++++++++++++++++++ .gitignore | 2 + TECHNICAL.md | 2 +- docs/_config.yml | 3 +- docs/_data/downloads.json | 25 ---------- docs/install.sh | 5 +- docs/install/install.ps1 | 5 +- package.json | 2 +- scripts/site-downloads.mjs | 33 +++++++++++++ scripts/site-downloads.test.mjs | 36 ++++++++++++++ 10 files changed, 165 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/pages.yml delete mode 100644 docs/_data/downloads.json create mode 100644 scripts/site-downloads.mjs create mode 100644 scripts/site-downloads.test.mjs diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml new file mode 100644 index 00000000..54482647 --- /dev/null +++ b/.github/workflows/pages.yml @@ -0,0 +1,84 @@ +name: Pages +run-name: 官网 · ${{ github.event_name == 'workflow_run' && format('发布 {0} 后更新', github.event.workflow_run.head_branch) || github.event_name }} + +# 官网(docs/)由 Actions 构建部署(仓库设置 Pages 来源为 GitHub Actions)。构建前按最新正式版 Release 生成 +# docs/_data/downloads.json(scripts/site-downloads.mjs),终端安装脚本读取的 install/downloads.tsv 因此随发版更新。 +# - docs 或生成脚本改动推送到 master 时部署;PR 中只构建,检查清单生成与 Jekyll 能通过。 +# - 正式版发布流水线结束后重新部署。workflow_run 总在默认分支上运行:github-pages 环境只允许 master 部署, +# 由标签触发的发布流水线不能直接部署。流水线失败也照常运行,数据只取已公开的最新正式版。 +on: + push: + branches: [master] + paths: ['docs/**', 'scripts/site-downloads.mjs', 'scripts/distribution.mjs', 'scripts/release-assets.mjs', '.github/workflows/pages.yml'] + pull_request: + paths: ['docs/**', 'scripts/site-downloads.mjs', 'scripts/distribution.mjs', 'scripts/release-assets.mjs', '.github/workflows/pages.yml'] + workflow_run: + workflows: [Release] + types: [completed] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: pages-${{ github.event_name == 'pull_request' && github.ref || 'deploy' }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + build: + name: Build site + # 测试版标签(含连字符)不会改变最新正式版,跳过。 + if: github.event_name != 'workflow_run' || !contains(github.event.workflow_run.head_branch, '-') + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - uses: actions/setup-node@v6 + with: + node-version: '22' + + # releases/latest 只返回已公开的最新正式版;附件不全或缺少 SHA-256 时构建失败,线上保留上一次部署。 + - name: Generate download manifest + env: + GH_TOKEN: ${{ github.token }} + run: | + gh api "repos/$GITHUB_REPOSITORY/releases/latest" > "$RUNNER_TEMP/release.json" + node scripts/site-downloads.mjs "$RUNNER_TEMP/release.json" docs/_data/downloads.json + + - uses: actions/configure-pages@v6 + if: github.event_name != 'pull_request' + + # 与传统模式相同的 github-pages 构建环境,_config.yml 与插件行为不变。 + - uses: actions/jekyll-build-pages@v1 + with: + source: ./docs + destination: ./_site + + # 部署前确认安装清单渲染出了全部附件,避免空清单上线后终端安装失败。 + - name: Check install manifest + run: | + rows=$(grep -c "$(printf '\t')https://github.com/bitdan/snap-desk/releases/download/" _site/install/downloads.tsv) + test "$rows" -eq "$(jq '.assets | length' docs/_data/downloads.json)" + cat _site/install/downloads.tsv + + - uses: actions/upload-pages-artifact@v5 + if: github.event_name != 'pull_request' + + deploy: + name: Deploy + needs: build + if: github.event_name != 'pull_request' + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + pages: write + id-token: write + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - id: deployment + uses: actions/deploy-pages@v5 diff --git a/.gitignore b/.gitignore index 90a3c836..adef7f24 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ scripts/ci/__pycache__/ out/ release-assets/ *.log +# 官网下载清单在 Pages 部署时按最新正式版生成(scripts/site-downloads.mjs)。 +docs/_data/downloads.json diff --git a/TECHNICAL.md b/TECHNICAL.md index 4c69003d..0f1246c6 100644 --- a/TECHNICAL.md +++ b/TECHNICAL.md @@ -105,7 +105,7 @@ Linux 另需截图库的系统依赖,Debian / Ubuntu 可执行 `bash scripts/c - AppImage 由 linuxdeploy 把主程序依赖的系统库一并打包;deb 依赖系统安装的库,因此小得多。 - 正式版公开后,`distribute.yml` 把安装描述推送到包管理器(测试版不分发);清单由 `scripts/distribution.mjs` 按附件实际哈希生成,也可 `gh workflow run distribute.yml -f tag=v<版本>` 补发。对应 secret 未配置的渠道会跳过: - - 终端安装脚本读取 tap 的 `downloads.tsv`。流水线从实际发布附件生成它和 `downloads.json`,并与 Scoop、Homebrew 清单一起推送。GitHub Pages 使用 `docs/_data/downloads.json` 渲染备用 TSV,供 tap 暂不可用时安装。 + - 终端安装脚本先读取官网的 `install/downloads.tsv`,失败时改读 tap 的副本。官网由 `pages.yml` 构建部署:构建前 `scripts/site-downloads.mjs` 按最新正式版 Release 附件的 SHA-256 生成 `docs/_data/downloads.json`(不提交到仓库),发布流水线结束后自动重新部署。tap 的 `downloads.tsv` / `downloads.json` 由分发流水线从实际发布附件生成,与 Scoop、Homebrew 清单一起推送。 - Scoop(独立 EXE)与 Homebrew Cask(DMG)共用 tap 仓库 `bitdan/homebrew-snap-desk`(`bucket/`、`Casks/`),需 `TAP_TOKEN`。安装:`scoop bucket add snap-desk https://github.com/bitdan/homebrew-snap-desk`、`brew install --cask bitdan/snap-desk/snap-desk`。 - WinGet(NSIS 安装包,`bitdan.SnapDesk`)由 wingetcreate 向 `microsoft/winget-pkgs` 提 PR,需 `WINGET_TOKEN`(classic PAT,`public_repo`),合并要等社区审核。 - AUR `snap-desk-bin`(解包 deb),需 `AUR_SSH_PRIVATE_KEY`,且包名需先在 AUR 网站登录账号后由首次推送创建。 diff --git a/docs/_config.yml b/docs/_config.yml index 1b6d43f0..0472157c 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -1,4 +1,5 @@ -# GitHub Pages(传统模式,从 master 的 /docs 用 Jekyll 构建)发布配置。 +# GitHub Pages 发布配置:.github/workflows/pages.yml 用 Jekyll 构建 docs/ 并部署, +# 构建前生成 _data/downloads.json(最新正式版的下载清单,不提交到仓库)。 # performance/ 是项目内部的性能测试脚本与结果,只在仓库中查看,不发布到官网。 exclude: - performance diff --git a/docs/_data/downloads.json b/docs/_data/downloads.json deleted file mode 100644 index 4fc85b34..00000000 --- a/docs/_data/downloads.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "version": "0.3.2", - "assets": { - "standalone": { - "url": "https://github.com/bitdan/snap-desk/releases/download/v0.3.2/snap-desk-v0.3.2-windows-x64-standalone.exe", - "sha256": "d4a7bb30aff382aed55c01c6140aca77c0b141afd78870cc83149f7b67633079" - }, - "setup": { - "url": "https://github.com/bitdan/snap-desk/releases/download/v0.3.2/snap-desk-v0.3.2-windows-x64-setup.exe", - "sha256": "71171d53f0766a060cc6bfb3e9ca67c6f24ffb1133e49956ff803b97ba832853" - }, - "dmg": { - "url": "https://github.com/bitdan/snap-desk/releases/download/v0.3.2/snap-desk-v0.3.2-macos-arm64.dmg", - "sha256": "62e040de44fec95271a6c5c4704837a1d391558a8d26e0313db0abef5b8e0444" - }, - "deb": { - "url": "https://github.com/bitdan/snap-desk/releases/download/v0.3.2/snap-desk-v0.3.2-linux-x64.deb", - "sha256": "1d770c6c6d614d4b21bc2b82ef188b924ea908e473f8b6b118ed0e518c6bb392" - }, - "appimage": { - "url": "https://github.com/bitdan/snap-desk/releases/download/v0.3.2/snap-desk-v0.3.2-linux-x64.AppImage", - "sha256": "c5bdd8a2eccae4d1db2a8c9a5efbf6a7d4269d6252c81cb8ad10dc74be6ca4f1" - } - } -} diff --git a/docs/install.sh b/docs/install.sh index c1bd4241..9c2bb4fc 100644 --- a/docs/install.sh +++ b/docs/install.sh @@ -44,8 +44,9 @@ main() { if [ "$kind" = deb ]; then command -v apt >/dev/null && command -v sudo >/dev/null || fail '--deb requires apt and sudo.' fi - if ! find_asset 'https://raw.githubusercontent.com/bitdan/homebrew-snap-desk/HEAD/downloads.tsv'; then - find_asset 'https://bitdan.github.io/snap-desk/install/downloads.tsv' || fail 'Could not resolve a stable download. See GitHub Releases.' + # 官网清单在每次发版后重新部署;raw.githubusercontent.com 在部分网络下不可达,tap 仓库的副本只作备用。 + if ! find_asset 'https://bitdan.github.io/snap-desk/install/downloads.tsv'; then + find_asset 'https://raw.githubusercontent.com/bitdan/homebrew-snap-desk/HEAD/downloads.tsv' || fail 'Could not resolve a stable download. See GitHub Releases.' fi work=$(mktemp -d "${TMPDIR:-/tmp}/snap-desk.XXXXXXXX") || fail 'Could not create a temporary directory.' diff --git a/docs/install/install.ps1 b/docs/install/install.ps1 index a6e661c9..8dea3b29 100644 --- a/docs/install/install.ps1 +++ b/docs/install/install.ps1 @@ -24,9 +24,10 @@ if ([Environment]::OSVersion.Platform -ne 'Win32NT' -or $architecture -notin 'AMD64', 'X64') { throw 'Snap Desk requires x64 Windows.' } - $asset = Get-SnapDeskAsset 'https://raw.githubusercontent.com/bitdan/homebrew-snap-desk/HEAD/downloads.tsv' + # 官网清单在每次发版后重新部署;raw.githubusercontent.com 在部分网络下不可达,tap 仓库的副本只作备用。 + $asset = Get-SnapDeskAsset 'https://bitdan.github.io/snap-desk/install/downloads.tsv' if (!$asset) { - $asset = Get-SnapDeskAsset 'https://bitdan.github.io/snap-desk/install/downloads.tsv' + $asset = Get-SnapDeskAsset 'https://raw.githubusercontent.com/bitdan/homebrew-snap-desk/HEAD/downloads.tsv' } if (!$asset) { throw 'Could not resolve a stable download. See GitHub Releases.' } diff --git a/package.json b/package.json index b262a453..2dca6810 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "type": "module", "scripts": { "check:architecture": "node scripts/check-architecture.mjs", - "test": "npm run check:architecture && node --test scripts/sync-version.test.mjs scripts/update-manifest.test.mjs scripts/release-assets.test.mjs scripts/release-signing.test.mjs scripts/release-notes.test.mjs scripts/package-gpui.test.mjs scripts/check-architecture.test.mjs scripts/distribution.test.mjs scripts/downloads.test.mjs scripts/install.test.mjs", + "test": "npm run check:architecture && node --test scripts/sync-version.test.mjs scripts/update-manifest.test.mjs scripts/release-assets.test.mjs scripts/release-signing.test.mjs scripts/release-notes.test.mjs scripts/package-gpui.test.mjs scripts/check-architecture.test.mjs scripts/distribution.test.mjs scripts/downloads.test.mjs scripts/site-downloads.test.mjs scripts/install.test.mjs", "version:sync": "node scripts/sync-version.mjs", "desktop:dev": "cargo run -p snap-desk-gpui", "desktop:package": "node scripts/package-gpui.mjs", diff --git a/scripts/site-downloads.mjs b/scripts/site-downloads.mjs new file mode 100644 index 00000000..a839ad13 --- /dev/null +++ b/scripts/site-downloads.mjs @@ -0,0 +1,33 @@ +// 官网下载清单:Pages 部署时(.github/workflows/pages.yml)按最新正式版 Release 生成 docs/_data/downloads.json, +// 再由 docs/install/downloads.tsv 渲染成终端安装脚本读取的清单。文件不提交到仓库,发版后重新部署即可更新。 +// SHA-256 取自 GitHub 为每个附件记录的 digest(上传时由 GitHub 计算),不需要下载附件。 +// +// 用法:gh api repos/bitdan/snap-desk/releases/latest > release.json +// node scripts/site-downloads.mjs release.json docs/_data/downloads.json +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import { DISTRIBUTED, assetName, downloadManifest } from './distribution.mjs' + +/** 从 Release 接口数据取出分发附件的 SHA-256,生成与包管理器渠道一致的下载清单。 */ +export function releaseDownloads(release) { + if (release.draft || release.prerelease) throw new Error(`官网只收录已公开的正式版:${release.tag_name}`) + const version = String(release.tag_name).replace(/^v/, '') + const hashes = Object.fromEntries(Object.keys(DISTRIBUTED).map(key => { + const name = assetName(key, version) + const digest = release.assets.find(asset => asset.name === name)?.digest + const sha256 = /^sha256:([a-f0-9]{64})$/.exec(digest ?? '')?.[1] + if (!sha256) throw new Error(`${release.tag_name} 缺少附件或 SHA-256:${name}`) + return [key, sha256] + })) + return downloadManifest(version, hashes) +} + +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + const [input, output] = process.argv.slice(2) + if (!input || !output) throw new Error('用法:node scripts/site-downloads.mjs <输出文件>') + const downloads = releaseDownloads(JSON.parse(readFileSync(input, 'utf8'))) + mkdirSync(dirname(resolve(output)), { recursive: true }) + writeFileSync(output, JSON.stringify(downloads, null, 2) + '\n') + console.log(`官网下载清单:${downloads.version} → ${output}`) +} diff --git a/scripts/site-downloads.test.mjs b/scripts/site-downloads.test.mjs new file mode 100644 index 00000000..a1cc7ea2 --- /dev/null +++ b/scripts/site-downloads.test.mjs @@ -0,0 +1,36 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { assetName, DISTRIBUTED } from './distribution.mjs' +import { releaseDownloads } from './site-downloads.mjs' + +const version = '3.12.2' +const digests = Object.fromEntries(Object.keys(DISTRIBUTED).map((key, index) => [key, String(index + 1).repeat(64)])) + +/** 与 GitHub Release 接口相同的结构:分发附件之外还有签名、更新包等其他附件。 */ +function release(overrides = {}) { + const assets = Object.keys(DISTRIBUTED).map(key => ({ name: assetName(key, version), digest: `sha256:${digests[key]}` })) + assets.push({ name: `${assetName('setup', version)}.sig`, digest: `sha256:${'f'.repeat(64)}` }, { name: 'updater.json', digest: null }) + return { tag_name: `v${version}`, draft: false, prerelease: false, assets, ...overrides } +} + +test('官网下载清单取自最新正式版附件的 SHA-256', () => { + const downloads = releaseDownloads(release()) + assert.equal(downloads.version, version) + assert.deepEqual(Object.keys(downloads.assets).sort(), Object.keys(DISTRIBUTED).sort()) + assert.deepEqual(downloads.assets.setup, { + url: `https://github.com/bitdan/snap-desk/releases/download/v${version}/snap-desk-v${version}-windows-x64-setup.exe`, + sha256: digests.setup, + }) +}) + +test('缺少附件、digest 或不是正式版时停止部署', () => { + const withoutDmg = release() + withoutDmg.assets = withoutDmg.assets.filter(asset => !asset.name.endsWith('.dmg')) + assert.throws(() => releaseDownloads(withoutDmg), /macos-arm64\.dmg/) + const withoutDigest = release() + withoutDigest.assets[0].digest = null + assert.throws(() => releaseDownloads(withoutDigest), /SHA-256/) + assert.throws(() => releaseDownloads(release({ prerelease: true })), /正式版/) + assert.throws(() => releaseDownloads(release({ draft: true })), /正式版/) + assert.throws(() => releaseDownloads({ ...release(), tag_name: 'v3.12.3-beta.1' }), /SHA-256|正式版/) +})