From 56a189e678edc80e68a0a8d6066fc05a61827e44 Mon Sep 17 00:00:00 2001 From: laily Date: Tue, 26 May 2026 12:50:26 +0800 Subject: [PATCH] ci: add cross-platform release workflow with version injection Build linux/darwin amd64 and arm64 artifacts on push, upload GitHub Release assets on v* tags, and expose version via agent-notify version. Co-authored-by: Cursor --- .github/workflows/release.yml | 110 ++++++++++++++++++++++++++++++++++ .gitignore | 1 + Makefile | 21 ++++++- README.md | 16 +++++ cmd/agent-notify/main.go | 3 + cmd/agent-notify/version.go | 16 +++++ 6 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 cmd/agent-notify/version.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..97cbe5e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,110 @@ +name: Release + +on: + push: + branches: [main] + tags: ["v*"] + pull_request: + branches: [main] + +permissions: + contents: read + +env: + GO_VERSION: "1.22.2" + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - name: Test + run: go test ./... + + build: + needs: test + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - goos: linux + goarch: amd64 + - goos: linux + goarch: arm64 + - goos: darwin + goarch: amd64 + - goos: darwin + goarch: arm64 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - name: Resolve version + id: meta + shell: bash + run: | + if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then + VERSION="${GITHUB_REF#refs/tags/}" + else + VERSION="dev-$(git rev-parse --short HEAD)" + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "Version: ${VERSION}" + + - name: Build binary + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + VERSION: ${{ steps.meta.outputs.version }} + run: | + mkdir -p dist + OUT="dist/agent-notify-${VERSION}_${GOOS}_${GOARCH}" + CGO_ENABLED=0 go build -trimpath \ + -ldflags "-s -w \ + -X main.version=${VERSION} \ + -X main.commit=${GITHUB_SHA} \ + -X main.buildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ + -o "${OUT}" ./cmd/agent-notify + chmod +x "${OUT}" + sha256sum "${OUT}" > "${OUT}.sha256" + tar -czf "${OUT}.tar.gz" -C dist "$(basename "${OUT}")" "$(basename "${OUT}").sha256" + rm "${OUT}" "${OUT}.sha256" + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: agent-notify-${{ steps.meta.outputs.version }}-${{ matrix.goos }}-${{ matrix.goarch }} + path: dist/agent-notify-${{ steps.meta.outputs.version }}_${{ matrix.goos }}_${{ matrix.goarch }}.tar.gz + if-no-files-found: error + + release: + if: startsWith(github.ref, 'refs/tags/v') + needs: build + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/download-artifact@v4 + with: + pattern: agent-notify-* + merge-multiple: true + path: dist + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 + with: + files: dist/*.tar.gz + generate_release_notes: true diff --git a/.gitignore b/.gitignore index 5b63778..7e19bd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin/ +/dist/ /.config/ diff --git a/Makefile b/Makefile index 01d6fcc..d577d07 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,28 @@ -.PHONY: build test install +.PHONY: build test install cross BINARY := agent-notify +VERSION ?= dev +COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none) +BUILD_DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) +LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildDate=$(BUILD_DATE) build: - go build -o bin/$(BINARY) ./cmd/agent-notify + go build -ldflags "$(LDFLAGS)" -o bin/$(BINARY) ./cmd/agent-notify test: go test ./... install: build install -m 755 bin/$(BINARY) $(HOME)/.local/bin/$(BINARY) + +cross: + @mkdir -p dist + @for target in \ + "linux amd64" \ + "linux arm64" \ + "darwin amd64" \ + "darwin arm64"; do \ + set -- $$target; \ + out=dist/$(BINARY)-$(VERSION)_$$1_$$2; \ + echo "building $$out"; \ + GOOS=$$1 GOARCH=$$2 CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o "$$out" ./cmd/agent-notify; \ + done diff --git a/README.md b/README.md index 7dd9e3a..84436af 100644 --- a/README.md +++ b/README.md @@ -98,4 +98,20 @@ agent-notify test claude --apply ```bash make test make build +make cross VERSION=v0.1.0 # 本地交叉编译 +agent-notify version ``` + +## CI / Release + +推送 `main` 分支或 `v*` tag 时,GitHub Actions 会构建并上传以下产物: + +- `agent-notify-_linux_amd64.tar.gz` +- `agent-notify-_linux_arm64.tar.gz` +- `agent-notify-_darwin_amd64.tar.gz` +- `agent-notify-_darwin_arm64.tar.gz` + +- **tag 发布**(如 `v0.1.0`):版本号为 tag 名,并自动创建 GitHub Release +- **main 分支**:版本号为 `dev-` + +每个压缩包内含二进制和 `.sha256` 校验文件。 diff --git a/cmd/agent-notify/main.go b/cmd/agent-notify/main.go index 53441b0..f565816 100644 --- a/cmd/agent-notify/main.go +++ b/cmd/agent-notify/main.go @@ -39,6 +39,8 @@ func run(cmd string, args []string) error { return cmdDoctor() case "logs": return cmdLogs(args) + case "version", "-V": + return cmdVersion() case "help", "-h", "--help": printUsage() return nil @@ -200,6 +202,7 @@ Commands: test cursor [--try-all] test claude [--apply] logs [--tail 30] + version doctor `) } diff --git a/cmd/agent-notify/version.go b/cmd/agent-notify/version.go new file mode 100644 index 0000000..bf3950b --- /dev/null +++ b/cmd/agent-notify/version.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +var ( + version = "dev" + commit = "none" + buildDate = "unknown" +) + +func cmdVersion() error { + fmt.Printf("agent-notify %s\n", version) + fmt.Printf("commit: %s\n", commit) + fmt.Printf("built: %s\n", buildDate) + return nil +}