diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index dba1761..9e77088 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -1,7 +1,7 @@ name: Publish image -# 推送到 main、推版本标签(v*)或手动触发时构建并发布; -# PR 只构建两种架构验证 Dockerfile,不登录 GHCR、不发布。 +# 推送到 main、推版本标签(v*)或手动触发时构建并发布(amd64 + arm64); +# PR 只构建 linux/amd64 验证 Dockerfile,不登录 GHCR、不发布(arm64 走 QEMU 太慢,PR 不划算)。 on: push: branches: @@ -34,12 +34,32 @@ jobs: uses: actions/checkout@v7 with: persist-credentials: false + # 需要 tag 历史:非 tag 构建用 git describe 推导快照号。 + fetch-depth: 0 - name: Read source revision id: source run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + # 版本号注入规则: + # tag v4.5.0 -> 4.5.0 + # 其它(main / PR / 手动分支) -> 快照号 <最近tag>-SNAPSHOT-<短sha>,如 4.4.0-SNAPSHOT-8d598e3 + # PR 只构建不发布,快照号带上 sha 也没有副作用。 + - name: Resolve build version + id: version + run: | + if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then + value="${GITHUB_REF_NAME#v}" + else + base="$(git describe --tags --abbrev=0 --match 'v*' 2>/dev/null || true)" + base="${base#v}" + short="$(git rev-parse --short HEAD)" + value="${base:-dev}-SNAPSHOT-${short}" + fi + echo "value=${value}" >> "$GITHUB_OUTPUT" + - name: Set up QEMU + if: github.event_name != 'pull_request' uses: docker/setup-qemu-action@v4 - name: Set up Docker Buildx @@ -61,6 +81,7 @@ jobs: type=raw,value=sha-${{ steps.source.outputs.sha }} labels: | org.opencontainers.image.revision=${{ steps.source.outputs.sha }} + org.opencontainers.image.version=${{ steps.version.outputs.value }} - name: Log in to GHCR if: github.event_name != 'pull_request' @@ -75,9 +96,12 @@ jobs: with: context: . file: docker/Dockerfile - platforms: linux/amd64,linux/arm64 + # PR 只用 amd64 验证 Dockerfile;发布分支/标签才构建 arm64(需 QEMU)。 + platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }} push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + VERSION=${{ steps.version.outputs.value }} cache-from: type=gha cache-to: type=gha,mode=max diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d7764b0 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,146 @@ +name: Release + +# 只允许“手动填表”触发;推送到 main 不会触发发布。 +# 版本号在构建期注入(-ldflags -X .../config.Version),所以这里不回写任何源码: +# 选择升级类型(patch/minor/major),工作流自动推算下一个版本、抓取提交标题作为 +# Release 正文,打 tag、建 GitHub Release,再触发 publish-image 按该 tag 构建镜像。 +on: + workflow_dispatch: + inputs: + bump: + description: "版本升级类型" + required: true + type: choice + default: patch + options: + - patch + - minor + - major + +permissions: + contents: write + # 用 GITHUB_TOKEN 推的 tag 不会触发 push 事件(GitHub 限制), + # 需要显式 workflow_dispatch 触发 publish-image,故要 actions: write。 + actions: write + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +jobs: + release: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Checkout + uses: actions/checkout@v5 + with: + # 需要完整 tag 历史来判断 tag 是否已存在、并生成 Release 正文。 + fetch-depth: 0 + + - name: Resolve next version + id: version + env: + BUMP: ${{ inputs.bump }} + run: | + set -euo pipefail + PREV_TAG="$(git describe --tags --abbrev=0 --match 'v*' HEAD 2>/dev/null || true)" + echo "PREV_TAG=$PREV_TAG" >> "$GITHUB_ENV" + BASE="${PREV_TAG#v}" + # 没有可用 tag 时以 0.0.0 作为 semver 推算基线(patch -> v0.0.1,需要 1.0.0 请选 major)。 + if ! printf '%s' "$BASE" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then + BASE="0.0.0" + fi + IFS=. read -r MAJOR MINOR PATCH <<< "$BASE" + case "$BUMP" in + major) MAJOR=$((MAJOR+1)); MINOR=0; PATCH=0 ;; + minor) MINOR=$((MINOR+1)); PATCH=0 ;; + patch) PATCH=$((PATCH+1)) ;; + *) echo "::error::未知的 bump 类型:$BUMP"; exit 1 ;; + esac + SEMVER="$MAJOR.$MINOR.$PATCH" + TAG="v$SEMVER" + if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then + echo "::error::tag $TAG 已存在" + exit 1 + fi + echo "SEMVER=$SEMVER" >> "$GITHUB_ENV" + echo "TAG=$TAG" >> "$GITHUB_ENV" + echo "tag=$TAG" >> "$GITHUB_OUTPUT" + echo "semver=$SEMVER" >> "$GITHUB_OUTPUT" + echo "从 ${PREV_TAG:-<无 tag>} 按 $BUMP 升级到 $TAG" + + - name: Generate release notes + run: | + set -euo pipefail + RANGE="HEAD" + if [ -n "${PREV_TAG:-}" ]; then + RANGE="${PREV_TAG}..HEAD" + fi + git log "$RANGE" --pretty=format:'%s' > /tmp/subjects.txt + + { + echo "## What's Changed" + if [ -n "${PREV_TAG:-}" ]; then + echo + echo "Compared to ${PREV_TAG}" + fi + found=0 + for pair in "feat:Features" "fix:Fixes" "perf:Performance" "refactor:Refactor" \ + "docs:Docs" "test:Tests" "build:Build" "ci:CI" "style:Style" "chore:Chores"; do + key="${pair%%:*}" + title="${pair#*:}" + mapfile -t items < <( + grep -E "^${key}(\([^)]*\))?!?:[[:space:]]+" /tmp/subjects.txt \ + | grep -v -i '^chore(release)' \ + | sed -E "s/^${key}(\([^)]*\))?!?:[[:space:]]+//" + ) + if [ "${#items[@]}" -gt 0 ]; then + echo + echo "### ${title}" + printf -- '- %s\n' "${items[@]}" + found=1 + fi + done + mapfile -t others < <( + grep -vE '^(feat|fix|perf|refactor|docs|test|build|ci|style|chore)(\([^)]*\))?!?:[[:space:]]+' /tmp/subjects.txt \ + | grep -v -i '^chore(release)' \ + | grep -v '^$' + ) + if [ "${#others[@]}" -gt 0 ]; then + echo + echo "### Other" + printf -- '- %s\n' "${others[@]}" + found=1 + fi + if [ "$found" -eq 0 ]; then + echo + echo "No commits" + fi + } > release-notes.md + echo "----- release-notes.md -----" + cat release-notes.md + + - name: Create and push tag + run: | + set -euo pipefail + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git tag -a "$TAG" -m "$TAG" + git push origin "$TAG" + + - name: Create GitHub Release + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + gh release create "$TAG" --title "$TAG" --notes-file release-notes.md + + - name: Trigger image publish + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + # publish-image 从 tag vX.Y.Z 解析出版本号并注入二进制, + # 构建并推送带版本号的 GHCR 镜像(latest 仍只跟 main)。 + gh workflow run publish-image.yml --ref "$TAG" diff --git a/AGENTS.md b/AGENTS.md index 27f98a0..83d8e41 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -138,6 +138,39 @@ Vercel 的 Go runtime 会**缓冲整段响应**:用 700ms 间隔 flush 的测 `/robots.txt` 返回的是静态 etag),放了 `index.html` 就会抢走 `/`,拿不到和 `/health` 同构的 JSON。仓库里没有 `public/`,`.gitignore` 和 `.vercelignore` 都排除它。 +## 发布 / 版本号 + +版本号**不在源码里写死**:`internal/config/config.go` 是 `var Version = "dev"`,由构建期 +`-ldflags "-X oc2api/internal/config.Version=x.y.z"` 注入,`/health` 直接透出。没注入就是 +`dev`。所以发布不需要改代码、不需要回写提交,只打 tag 即可。 + +不同构建的版本号来源(见 `publish-image.yml` 的 Resolve build version): + +| 构建 | 版本号 | +| --- | --- | +| tag `v4.5.0` | `4.5.0`(标签发布,打 GHCR 版本 tag) | +| 其它(`main` / 手动分支 / PR) | `4.4.0-SNAPSHOT-8d598e3`(最近 tag + `-SNAPSHOT-` + 短 sha,goreleaser 风格);PR 只构建不发布,带 sha 无副作用 | + +**不要手改 `Version`、也不要手动 `git tag`**,统一走 GitHub Actions 的填表发布: + +1. 打开 Actions → **Release** → Run workflow(`workflow_dispatch`),只填一个输入: + - `bump`:版本升级类型,`patch` / `minor` / `major`(默认 `patch`)。 +2. 工作流(`.github/workflows/release.yml`)会自动:读最近一个 `v*` tag → 按 `bump` + 推算下一个版本(没有 tag 时从 `0.0.0` 起算)→ 校验 tag 不存在 → 用 `git log` + 抓取上个 tag 到当前 HEAD 的提交标题、按类型分组生成 Release 正文 → 打 annotated tag → + `gh release create --notes-file` → 显式 `gh workflow run publish-image.yml --ref `。 +3. 显式派发是必须的:用 `GITHUB_TOKEN` 推的 tag **不会**再触发 `push` 事件(GitHub 限制), + 所以要手动触发 `publish-image`。它把 `refs/tags/vX.Y.Z` 解析成 `4.5.0`,作为 + `--build-arg VERSION=4.5.0` 传给 `docker/Dockerfile`,产出带版本号的 GHCR 镜像 + (`latest` 仍只跟 `main`)。 + +前提:**release.yml 必须在默认分支上**,`workflow_dispatch` 的 Run workflow 按钮才会出现; +一般做法是先把本分支合进 `main`。另外推 tag 需要 Actions 有 `contents: write`,且默认分支 +不要用分支保护挡住 tag 推送。发布不需要 npm,也没有 npm 包。 + +本地构建要带版本号时同样走构建参数:`VERSION=4.5.0 docker compose build` +(见 `docker/docker-compose.yml`),或 `docker build --build-arg VERSION=4.5.0 ...`。 + ## 测试 ```bash diff --git a/README.md b/README.md index 4d97fe0..9f7083b 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ docker run -d --name oc2api --restart unless-stopped \ 容器监听 `PORT`,默认 `8080`。镜像基于 `scratch`,里面只有静态二进制和 CA 证书。 当前镜像带 mihomo 内核(约 47 MB);想要轻量镜像可构建时加 `--build-arg GO_TAGS=nomihomo`(约 9 MB,仅 http/socks5)。 +镜像里的版本号由构建参数注入:`--build-arg VERSION=4.5.0`,不传则为 `dev`(`/health` 可见)。 +官方 CI 构建时:tag 发布用 `4.5.0`,其余(`main`/`latest`、PR)用快照号 `4.4.0-SNAPSHOT-`。 云函数环境建议把超时调到 300 秒,否则长回答的流式响应会被提前截断。 **本地构建** diff --git a/docker/Dockerfile b/docker/Dockerfile index 67698e7..61fe75e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,11 +1,14 @@ # 纯 Go 运行时:编译成静态二进制,镜像里只有这个二进制和 CA 证书。 FROM golang:1.24-alpine AS build ARG GO_TAGS="" +# 版本号构建期注入,默认 dev(不指定时不代表任何发布版本)。 +ARG VERSION="dev" WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -tags "$GO_TAGS" -trimpath -buildvcs=false -ldflags "-s -w" -o /out/oc2api ./cmd/oc2api +RUN CGO_ENABLED=0 GOOS=linux go build -tags "$GO_TAGS" -trimpath -buildvcs=false \ + -ldflags "-s -w -X oc2api/internal/config.Version=${VERSION}" -o /out/oc2api ./cmd/oc2api FROM scratch COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index de5fcc2..9cfdccf 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,6 +5,9 @@ services: build: context: .. dockerfile: docker/Dockerfile + args: + # 本地构建可用 VERSION=4.5.0 docker compose build 注入版本号,默认 dev。 + VERSION: "${VERSION:-dev}" container_name: oc2api ports: - "${PORT:-8080}:8080" diff --git a/internal/config/config.go b/internal/config/config.go index 1a1b72b..c25f310 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,8 +11,13 @@ import ( "time" ) -// Version 与发布标签保持一致,/health 直接透出。 -const Version = "4.4.0" +// Version 由构建期注入,不在源码里写死: +// +// -ldflags "-X oc2api/internal/config.Version=x.y.z" +// +// 未注入时为 dev。正式镜像由 .github/workflows/release.yml 按填写的 tag 传入版本号, +// /health 直接透出。所以这里必须是 var(const 无法被 -X 覆盖),也不要手改。 +var Version = "dev" // DefaultZenAPIBaseURL 只到站点根:具体路径由 zen 包拼,上游改路径时不用动部署配置。 const DefaultZenAPIBaseURL = "https://opencode.ai" diff --git a/scripts/build-go.sh b/scripts/build-go.sh index 329159b..442a92a 100755 --- a/scripts/build-go.sh +++ b/scripts/build-go.sh @@ -4,6 +4,19 @@ set -euo pipefail GO_VERSION="${GO_VERSION:-1.24.13}" +# 版本号构建期注入:优先用显式 VERSION;否则生成 goreleaser 风格快照号 +# <最近tag>-SNAPSHOT-<短sha>(如 4.4.0-SNAPSHOT-8d598e3);取不到 git 时退回 dev。 +VERSION="${VERSION:-}" +if [ -z "$VERSION" ]; then + short="$(git rev-parse --short HEAD 2>/dev/null || true)" + base="$(git describe --tags --abbrev=0 --match 'v*' 2>/dev/null || true)" + base="${base#v}" + if [ -n "$short" ]; then + VERSION="${base:-dev}-SNAPSHOT-${short}" + else + VERSION="dev" + fi +fi if ! command -v go >/dev/null 2>&1; then echo "go not found, downloading ${GO_VERSION}" @@ -18,6 +31,7 @@ mkdir -p bin public # 只用 robots.txt 占位,不放 index.html——静态文件优先于 rewrites,会抢走 /。 printf 'User-agent: *\nDisallow:\n' > public/robots.txt # Vercel 走无内核构建:-tags nomihomo 排除 mihomo,二进制约 8MB。 -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags nomihomo -trimpath -buildvcs=false -ldflags "-s -w" -o bin/oc2api ./cmd/oc2api +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags nomihomo -trimpath -buildvcs=false \ + -ldflags "-s -w -X oc2api/internal/config.Version=${VERSION}" -o bin/oc2api ./cmd/oc2api chmod +x bin/oc2api ls -la bin