feat: add desktop updater and release automation
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import {
|
||||
existsSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
const START_MARKER = "<!-- vnt-downloads:start -->";
|
||||
const END_MARKER = "<!-- vnt-downloads:end -->";
|
||||
|
||||
const TOOL_TARGETS = [
|
||||
["Windows", "x86_64", "x86_64-pc-windows-msvc"],
|
||||
["Windows", "x86", "i686-pc-windows-msvc"],
|
||||
["Windows", "ARM64", "aarch64-pc-windows-msvc"],
|
||||
["Linux", "x86_64", "x86_64-unknown-linux-musl"],
|
||||
["Linux", "ARM64", "aarch64-unknown-linux-musl"],
|
||||
["Linux", "ARMv7 hard-float", "armv7-unknown-linux-musleabihf"],
|
||||
["Linux", "ARMv7 soft-float", "armv7-unknown-linux-musleabi"],
|
||||
["Linux", "ARM hard-float", "arm-unknown-linux-musleabihf"],
|
||||
["Linux", "ARM soft-float", "arm-unknown-linux-musleabi"],
|
||||
["Linux", "MIPS little-endian", "mipsel-unknown-linux-musl"],
|
||||
["Linux", "MIPS big-endian", "mips-unknown-linux-musl"],
|
||||
["macOS", "Apple Silicon", "aarch64-apple-darwin"],
|
||||
["macOS", "Intel", "x86_64-apple-darwin"],
|
||||
["FreeBSD 13.2", "x86_64", "x86_64-unknown-freebsd"],
|
||||
];
|
||||
|
||||
const DOWNLOADS = [
|
||||
{
|
||||
product: "VNT Desktop",
|
||||
platform: "Windows",
|
||||
architecture: "x86_64",
|
||||
format: "EXE 安装包",
|
||||
filename: ({ version }) => `VNT.Desktop_${version}_windows_x64-setup.exe`,
|
||||
},
|
||||
{
|
||||
product: "VNT Desktop",
|
||||
platform: "Windows",
|
||||
architecture: "x86_64",
|
||||
format: "MSI 安装包",
|
||||
filename: ({ version }) => `VNT.Desktop_${version}_windows_x64.msi`,
|
||||
},
|
||||
...TOOL_TARGETS.map(([platform, architecture, target]) => ({
|
||||
product: "VNT 工具包",
|
||||
platform,
|
||||
architecture,
|
||||
format: "ZIP",
|
||||
filename: ({ tag }) => `vnt2-${target}-${tag}.zip`,
|
||||
})),
|
||||
];
|
||||
|
||||
export function buildDownloadSection({ version, tag, releaseUrl, assetNames }) {
|
||||
const rows = DOWNLOADS.flatMap((download) => {
|
||||
const filename = download.filename({ version, tag });
|
||||
if (!assetNames.has(filename)) return [];
|
||||
const url = `${releaseUrl}/${encodeURIComponent(filename)}`;
|
||||
return [
|
||||
`| ${download.product} | ${download.platform} | ${download.architecture} | [${download.format}](${url}) |`,
|
||||
];
|
||||
});
|
||||
|
||||
if (rows.length === 0) return undefined;
|
||||
|
||||
return [
|
||||
START_MARKER,
|
||||
"## 下载",
|
||||
"",
|
||||
"> VNT 工具包包含 `vnt2_web`、`vnt2_cli` 和 `vnt2_ctrl`。",
|
||||
"",
|
||||
"| 产品 | 平台 | 架构 | 下载 |",
|
||||
"|---|---|---|---|",
|
||||
...rows,
|
||||
END_MARKER,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function mergeDownloadSection(notes, section) {
|
||||
const start = notes.indexOf(START_MARKER);
|
||||
if (start === -1) {
|
||||
const existing = notes.trimEnd();
|
||||
return existing.length === 0 ? `${section}\n` : `${existing}\n\n${section}\n`;
|
||||
}
|
||||
|
||||
const end = notes.indexOf(END_MARKER, start);
|
||||
if (end === -1) {
|
||||
throw new Error("release notes contain an incomplete download section");
|
||||
}
|
||||
|
||||
return `${notes.slice(0, start)}${section}${notes.slice(end + END_MARKER.length)}`;
|
||||
}
|
||||
|
||||
function releaseAssetId(url) {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
if (parsed.hostname !== "api.github.com") return undefined;
|
||||
return parsed.pathname.match(/\/releases\/assets\/(\d+)$/)?.[1];
|
||||
}
|
||||
|
||||
export function normalizeUpdaterManifest(manifest, assets) {
|
||||
const downloadUrls = new Map(
|
||||
assets.map((asset) => [String(asset.id), asset.browser_download_url]),
|
||||
);
|
||||
let changed = false;
|
||||
const platforms = Object.fromEntries(
|
||||
Object.entries(manifest.platforms || {}).map(([target, platform]) => {
|
||||
const assetId = releaseAssetId(platform.url);
|
||||
if (!assetId) return [target, platform];
|
||||
|
||||
const downloadUrl = downloadUrls.get(assetId);
|
||||
if (!downloadUrl) {
|
||||
throw new Error(
|
||||
`updater platform ${target} references unknown release asset ${assetId}`,
|
||||
);
|
||||
}
|
||||
changed = true;
|
||||
return [target, { ...platform, url: downloadUrl }];
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
changed,
|
||||
manifest: changed ? { ...manifest, platforms } : manifest,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeReleaseUpdaterManifest({ repository, tag, release }) {
|
||||
const latestAsset = release.assets.find((asset) => asset.name === "latest.json");
|
||||
if (!latestAsset) throw new Error(`release ${tag} does not contain latest.json`);
|
||||
|
||||
const manifest = JSON.parse(
|
||||
execFileSync(
|
||||
"gh",
|
||||
[
|
||||
"api",
|
||||
"-H",
|
||||
"Accept: application/octet-stream",
|
||||
`repos/${repository}/releases/assets/${latestAsset.id}`,
|
||||
],
|
||||
{ encoding: "utf8", env: process.env, windowsHide: true },
|
||||
),
|
||||
);
|
||||
const normalized = normalizeUpdaterManifest(manifest, release.assets);
|
||||
if (!normalized.changed) return;
|
||||
|
||||
const manifestDir = mkdtempSync(join(tmpdir(), "vnt-updater-manifest-"));
|
||||
const manifestPath = join(manifestDir, "latest.json");
|
||||
try {
|
||||
writeFileSync(manifestPath, `${JSON.stringify(normalized.manifest, null, 2)}\n`);
|
||||
execFileSync(
|
||||
"gh",
|
||||
["release", "upload", tag, manifestPath, "--repo", repository, "--clobber"],
|
||||
{ stdio: "inherit", env: process.env, windowsHide: true },
|
||||
);
|
||||
} finally {
|
||||
rmSync(manifestDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function requiredEnv(name) {
|
||||
const value = process.env[name];
|
||||
if (!value) throw new Error(`${name} is required`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const repository = requiredEnv("GITHUB_REPOSITORY");
|
||||
const tag = requiredEnv("GITHUB_REF_NAME");
|
||||
const serverUrl = requiredEnv("GITHUB_SERVER_URL").replace(/\/$/, "");
|
||||
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
|
||||
const configPath = resolve(workspace, "vnt-desktop/src-tauri/tauri.conf.json");
|
||||
const version = JSON.parse(readFileSync(configPath, "utf8")).version;
|
||||
const release = JSON.parse(
|
||||
execFileSync(
|
||||
"gh",
|
||||
["api", `repos/${repository}/releases/tags/${tag}`],
|
||||
{ encoding: "utf8", env: process.env, windowsHide: true },
|
||||
),
|
||||
);
|
||||
|
||||
normalizeReleaseUpdaterManifest({ repository, tag, release });
|
||||
const assetNames = new Set(release.assets.map((asset) => asset.name));
|
||||
const releaseUrl = `${serverUrl}/${repository}/releases/download/${tag}`;
|
||||
const section = buildDownloadSection({ version, tag, releaseUrl, assetNames });
|
||||
if (!section) throw new Error(`release ${tag} does not contain recognized assets`);
|
||||
|
||||
const notes = release.body || "";
|
||||
const updatedNotes = mergeDownloadSection(notes, section);
|
||||
if (updatedNotes === notes) return;
|
||||
|
||||
const notesFile = join(
|
||||
process.env.RUNNER_TEMP || tmpdir(),
|
||||
`vnt-release-notes-${process.pid}.md`,
|
||||
);
|
||||
try {
|
||||
writeFileSync(notesFile, updatedNotes);
|
||||
execFileSync(
|
||||
"gh",
|
||||
["release", "edit", tag, "--repo", repository, "--notes-file", notesFile],
|
||||
{ stdio: "inherit", env: process.env, windowsHide: true },
|
||||
);
|
||||
} finally {
|
||||
if (existsSync(notesFile)) unlinkSync(notesFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
||||
main();
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
buildDownloadSection,
|
||||
mergeDownloadSection,
|
||||
normalizeUpdaterManifest,
|
||||
} from "./update-release-downloads.mjs";
|
||||
|
||||
test("builds links for desktop installers and target tool bundles", () => {
|
||||
const section = buildDownloadSection({
|
||||
version: "2.0.0",
|
||||
tag: "v2.0.0",
|
||||
releaseUrl: "https://github.com/vnt-dev/vnt/releases/download/v2.0.0",
|
||||
assetNames: new Set([
|
||||
"VNT.Desktop_2.0.0_windows_x64-setup.exe",
|
||||
"vnt2-x86_64-unknown-linux-musl-v2.0.0.zip",
|
||||
]),
|
||||
});
|
||||
|
||||
assert.match(section, /VNT\.Desktop_2\.0\.0_windows_x64-setup\.exe/);
|
||||
assert.match(section, /vnt2-x86_64-unknown-linux-musl-v2\.0\.0\.zip/);
|
||||
assert.doesNotMatch(section, /VNT\.Desktop_2\.0\.0_windows_x64\.msi/);
|
||||
});
|
||||
|
||||
test("replaces an existing release download section", () => {
|
||||
const oldSection = [
|
||||
"<!-- vnt-downloads:start -->",
|
||||
"old links",
|
||||
"<!-- vnt-downloads:end -->",
|
||||
].join("\n");
|
||||
const merged = mergeDownloadSection(`Release notes\n\n${oldSection}\n`, "new links");
|
||||
|
||||
assert.equal(merged, "Release notes\n\nnew links\n");
|
||||
});
|
||||
|
||||
test("replaces GitHub API updater URLs with public download URLs", () => {
|
||||
const manifest = {
|
||||
version: "2.0.0",
|
||||
platforms: {
|
||||
"windows-x86_64": {
|
||||
signature: "windows-signature",
|
||||
url: "https://api.github.com/repos/vnt-dev/vnt/releases/assets/101",
|
||||
},
|
||||
},
|
||||
};
|
||||
const assets = [
|
||||
{
|
||||
id: 101,
|
||||
browser_download_url:
|
||||
"https://github.com/vnt-dev/vnt/releases/download/v2.0.0/VNT.exe",
|
||||
},
|
||||
];
|
||||
|
||||
const normalized = normalizeUpdaterManifest(manifest, assets);
|
||||
|
||||
assert.equal(normalized.changed, true);
|
||||
assert.equal(
|
||||
normalized.manifest.platforms["windows-x86_64"].url,
|
||||
assets[0].browser_download_url,
|
||||
);
|
||||
assert.equal(
|
||||
normalized.manifest.platforms["windows-x86_64"].signature,
|
||||
"windows-signature",
|
||||
);
|
||||
});
|
||||
|
||||
test("rejects updater URLs for unknown release assets", () => {
|
||||
assert.throws(
|
||||
() =>
|
||||
normalizeUpdaterManifest(
|
||||
{
|
||||
platforms: {
|
||||
"windows-x86_64": {
|
||||
signature: "signature",
|
||||
url: "https://api.github.com/repos/vnt-dev/vnt/releases/assets/999",
|
||||
},
|
||||
},
|
||||
},
|
||||
[],
|
||||
),
|
||||
/unknown release asset 999/,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
name: PR Checks
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: pr-checks-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
frontend:
|
||||
name: Frontend checks
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v6
|
||||
with:
|
||||
version: 10.34.5
|
||||
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
cache-dependency-path: pnpm-lock.yaml
|
||||
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Build shared Web frontend
|
||||
run: pnpm build:web
|
||||
|
||||
- name: Build Tauri frontend
|
||||
run: pnpm build:desktop-ui
|
||||
|
||||
- name: Test release metadata scripts
|
||||
run: node --test .github/scripts/*.test.mjs
|
||||
|
||||
rust:
|
||||
name: ${{ matrix.name }} Rust checks
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- name: Windows
|
||||
os: windows-latest
|
||||
- name: Linux
|
||||
os: ubuntu-22.04
|
||||
- name: macOS
|
||||
os: macos-latest
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Install Linux dependencies
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf xdg-utils
|
||||
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: clippy
|
||||
|
||||
- name: Check workspace
|
||||
run: cargo check --workspace --all-targets
|
||||
|
||||
- name: Run Clippy
|
||||
run: cargo clippy --workspace --all-targets -- -D warnings
|
||||
|
||||
- name: Run tests
|
||||
run: cargo test --workspace --all-targets
|
||||
@@ -1,4 +1,4 @@
|
||||
name: Rust
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -17,8 +17,36 @@ permissions:
|
||||
packages: write
|
||||
|
||||
jobs:
|
||||
build-web:
|
||||
name: Build Web frontend
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v5
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v6
|
||||
with:
|
||||
version: 10.34.5
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
cache-dependency-path: pnpm-lock.yaml
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
- name: Build shared Web frontend
|
||||
run: pnpm build:web
|
||||
- name: Upload Web static files
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: vnt-web-static
|
||||
path: vnt-web/static
|
||||
retention-days: 1
|
||||
|
||||
build:
|
||||
name: Build ${{ matrix.TARGET }}
|
||||
needs: build-web
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -60,7 +88,14 @@ jobs:
|
||||
OS: ${{ matrix.OS }}
|
||||
FEATURES: ${{ matrix.FEATURES }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v5
|
||||
- name: Replace Web static files with current frontend build
|
||||
run: rm -rf vnt-web/static && mkdir -p vnt-web/static
|
||||
- name: Download Web static files
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: vnt-web-static
|
||||
path: vnt-web/static
|
||||
- name: Init submodules
|
||||
run: git submodule update --init --recursive --remote && git submodule status
|
||||
- name: Cargo cache
|
||||
@@ -279,4 +314,62 @@ jobs:
|
||||
file: ./artifacts/*/*.zip
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: true
|
||||
file_glob: true
|
||||
file_glob: true
|
||||
|
||||
desktop-windows:
|
||||
name: Build and publish VNT Desktop for Windows
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
needs: deploy
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v5
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v6
|
||||
with:
|
||||
version: 10.34.5
|
||||
- name: Install Node.js
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
cache-dependency-path: pnpm-lock.yaml
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
- name: Verify tag matches desktop version
|
||||
shell: pwsh
|
||||
run: |
|
||||
$config = Get-Content vnt-desktop/src-tauri/tauri.conf.json | ConvertFrom-Json
|
||||
$tagVersion = "${{ github.ref_name }}" -replace '^v', ''
|
||||
if ($tagVersion -ne $config.version) {
|
||||
throw "Tag ${{ github.ref_name }} does not match desktop version $($config.version)"
|
||||
}
|
||||
- name: Build and publish Windows desktop installers
|
||||
uses: tauri-apps/tauri-action@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
|
||||
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
|
||||
with:
|
||||
projectPath: vnt-desktop
|
||||
tagName: ${{ github.ref_name }}
|
||||
releaseName: VNT ${{ github.ref_name }}
|
||||
releaseDraft: false
|
||||
prerelease: false
|
||||
releaseAssetNamePattern: 'VNT.Desktop_[version]_windows_[arch][setup][ext]'
|
||||
args: --bundles nsis,msi
|
||||
|
||||
release-notes:
|
||||
name: Add download links to release notes
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
needs: desktop-windows
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v5
|
||||
- name: Update updater metadata and release notes
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: node .github/scripts/update-release-downloads.mjs
|
||||
|
||||
Reference in New Issue
Block a user