diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5348b04 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,146 @@ +name: Build and release + +on: + push: + tags: + - "v*" + workflow_dispatch: + inputs: + tag: + description: "Release tag, for example v1.0.0" + required: true + type: string + +permissions: + contents: write + +concurrency: + group: release-${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref }} + cancel-in-progress: false + +jobs: + build: + name: Build ${{ matrix.goos }}/${{ matrix.goarch }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - goos: linux + goarch: amd64 + extension: "" + archive: tar.gz + - goos: linux + goarch: arm64 + extension: "" + archive: tar.gz + - goos: windows + goarch: amd64 + extension: .exe + archive: zip + - goos: windows + goarch: arm64 + extension: .exe + archive: zip + - goos: darwin + goarch: amd64 + extension: "" + archive: tar.gz + - goos: darwin + goarch: arm64 + extension: "" + archive: tar.gz + + steps: + - name: Check out source + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Prepare release metadata + id: metadata + shell: bash + run: | + if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then + release_tag="${{ inputs.tag }}" + else + release_tag="$GITHUB_REF_NAME" + fi + if [[ ! "$release_tag" =~ ^v[0-9] ]]; then + echo "Release tag must start with v followed by a number, for example v1.0.0" >&2 + exit 1 + fi + package_name="opencode2api_${release_tag}_${{ matrix.goos }}_${{ matrix.goarch }}" + echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT" + echo "package_name=$package_name" >> "$GITHUB_OUTPUT" + + - name: Build and package + shell: bash + env: + CGO_ENABLED: "0" + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + RELEASE_TAG: ${{ steps.metadata.outputs.release_tag }} + PACKAGE_NAME: ${{ steps.metadata.outputs.package_name }} + EXTENSION: ${{ matrix.extension }} + ARCHIVE_FORMAT: ${{ matrix.archive }} + run: | + mkdir -p "dist/$PACKAGE_NAME" + go build -trimpath -ldflags="-s -w -X main.version=$RELEASE_TAG" -o "dist/$PACKAGE_NAME/opencode2api$EXTENSION" ./ + cp README.md config.example.json "dist/$PACKAGE_NAME/" + if [[ "$ARCHIVE_FORMAT" == "zip" ]]; then + (cd dist && zip -q -r "$PACKAGE_NAME.zip" "$PACKAGE_NAME") + archive="$PACKAGE_NAME.zip" + else + tar -C dist -czf "dist/$PACKAGE_NAME.tar.gz" "$PACKAGE_NAME" + archive="$PACKAGE_NAME.tar.gz" + fi + (cd dist && sha256sum "$archive" > "$archive.sha256") + + - name: Upload release files + uses: actions/upload-artifact@v4 + with: + name: ${{ steps.metadata.outputs.package_name }} + path: | + dist/${{ steps.metadata.outputs.package_name }}.${{ matrix.archive }} + dist/${{ steps.metadata.outputs.package_name }}.${{ matrix.archive }}.sha256 + if-no-files-found: error + + release: + name: Publish GitHub Release + needs: build + runs-on: ubuntu-latest + steps: + - name: Resolve release tag + id: metadata + shell: bash + run: | + if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then + release_tag="${{ inputs.tag }}" + else + release_tag="$GITHUB_REF_NAME" + fi + echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT" + + - name: Download release files + uses: actions/download-artifact@v4 + with: + pattern: opencode2api_* + path: release-files + merge-multiple: true + + - name: Create or update release + shell: bash + env: + GH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ steps.metadata.outputs.release_tag }} + run: | + if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then + gh release upload "$RELEASE_TAG" release-files/* --clobber + else + gh release create "$RELEASE_TAG" release-files/* --generate-notes --title "$RELEASE_TAG" --target "$GITHUB_SHA" + fi diff --git a/README.md b/README.md index 180b258..beb45c1 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ WebUI 可查看最近一小时的请求率、成功率、状态码与延迟分 go build -o opencode2api ./ ``` +## 下载 + +预编译的 Windows、Linux 和 macOS 可执行文件可从 [GitHub Releases](https://github.com/jasonxu114514/opencode2api/releases) 下载。 + ## Docker Compose 部署 服务器安装 Docker 与 Docker Compose 后,可以在克隆项目后直接启动: