Skip to content

chore: release and publish from github actions #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI
on:
pull_request:
push:
branches:
- main
- '8.0' # remove this after 8.0 is merged into main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 22
cache: 'pnpm'

- uses: pnpm/action-setup@v4
with:
version: 10.14.0
run_install: false

- run: pnpm install --frozen-lockfile
- run: pnpm run lint
- run: pnpm run typecheck
- run: pnpm run build
- run: pnpm run publint
53 changes: 53 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Release

on:
workflow_dispatch:
Copy link
Member

@kingyue737 kingyue737 Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be a common practice to trigger releases via Git tags. Since we can extract the version directly from the tag, this simplifies the process and makes the dist-tag.ts script unnecessary.


permissions:
id-token: write
contents: write

jobs:
publish:
runs-on: ubuntu-latest
environment: npm-publish

steps:
# ───── 基础环境 ─────
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm

- uses: pnpm/action-setup@v4
with:
version: 10.14.0
run_install: false

- run: pnpm install --frozen-lockfile

- id: pkg
run: echo "version=$(node -p \"require('./package.json').version\")" >> "$GITHUB_OUTPUT"

- id: tag
run: echo "tag=$(pnpm exec jiti scripts/dist-tag.ts '${{ steps.pkg.outputs.version }}')" >> "$GITHUB_OUTPUT"

- run: pnpm exec jiti scripts/changelog.ts '${{ steps.pkg.outputs.version }}' > tmp_release_body.md

- run: corepack prepare [email protected] --activate

- name: Publish to npm
run: |
pnpm exec [email protected] publish \
--tag '${{ steps.tag.outputs.tag }}' \
--access public

- uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.pkg.outputs.version }}
body_path: tmp_release_body.md
prerelease: ${{ startsWith(steps.pkg.outputs.version, '0') || contains(steps.pkg.outputs.version, '-') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
onlyBuiltDependencies:
- esbuild
47 changes: 47 additions & 0 deletions scripts/changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";

/**
* Extract the changelog section for the given version from CHANGELOG.md
* and print it to stdout (without leading/trailing blank lines).
*
* Usage:
* npx jiti scripts/changelog.ts 7.0.3 > tmp_release_body.md
*/

const version = process.argv[2];

if (!version) {
console.error("Usage: jiti scripts/changelog.ts <version>");
process.exit(1);
}

const header = `## ${version}`;
const changelogPath = join(process.cwd(), "CHANGELOG.md");
const lines = readFileSync(changelogPath, "utf8").split(/\r?\n/);

let capturing = false;
const section: string[] = [];

for (const line of lines) {
if (line.startsWith("## ")) {
if (capturing) break; // reached next version header
if (line.trim() === header) {
capturing = true; // start capturing after header
continue; // skip header line itself
}
}
if (capturing) section.push(line);
}

// Trim leading / trailing blank lines
while (section.length && section[0].trim() === "") section.shift();
while (section.length && section[section.length - 1].trim() === "")
section.pop();

if (section.length === 0) {
console.error(`No changelog entry found for version ${version}`);
process.exit(1);
}

console.log(section.join("\n"));
17 changes: 17 additions & 0 deletions scripts/dist-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** Resolve npm dist-tag from a semver string.
* Usage: jiti scripts/dist-tag.ts 1.2.3-beta.0 # → beta
* jiti scripts/dist-tag.ts # picks version from package.json
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";

// Prefer CLI arg, otherwise read package.json
const version: string =
process.argv[2] ??
JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf8")).version;

// Capture first recognised prerelease label
const tag =
version.match(/-(alpha|beta|rc)\b/i)?.[1].toLowerCase() ?? "latest";

console.log(tag);
Loading