mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
178 lines
5.3 KiB
YAML
178 lines
5.3 KiB
YAML
name: Release binaries
|
|
|
|
on:
|
|
push:
|
|
tags:
|
|
- "v*"
|
|
workflow_dispatch:
|
|
inputs:
|
|
tag:
|
|
description: "Release tag, for example v1.0.0"
|
|
required: true
|
|
type: string
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
concurrency:
|
|
group: release-${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref }}
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
prepare:
|
|
name: Prepare release
|
|
runs-on: ubuntu-latest
|
|
outputs:
|
|
release_tag: ${{ steps.metadata.outputs.release_tag }}
|
|
steps:
|
|
- name: Resolve release metadata
|
|
id: metadata
|
|
shell: bash
|
|
env:
|
|
DISPATCH_TAG: ${{ inputs.tag }}
|
|
run: |
|
|
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
|
|
release_tag="$DISPATCH_TAG"
|
|
else
|
|
release_tag="$GITHUB_REF_NAME"
|
|
fi
|
|
if [[ ! "$release_tag" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z]+([.-][0-9A-Za-z]+)*)?$ ]]; then
|
|
echo "Release tag must be a semantic version such as v1.0.0 or v1.0.0-rc.1" >&2
|
|
exit 1
|
|
fi
|
|
echo "release_tag=$release_tag" >> "$GITHUB_OUTPUT"
|
|
|
|
verify:
|
|
name: Verify source
|
|
runs-on: ubuntu-latest
|
|
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: Vet
|
|
run: go vet ./...
|
|
|
|
- name: Build
|
|
run: go build ./...
|
|
|
|
build:
|
|
name: Build ${{ matrix.goos }}/${{ matrix.goarch }}
|
|
needs: [prepare, verify]
|
|
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 package name
|
|
id: metadata
|
|
shell: bash
|
|
env:
|
|
RELEASE_TAG: ${{ needs.prepare.outputs.release_tag }}
|
|
run: |
|
|
package_name="opencode2api_${RELEASE_TAG}_${{ matrix.goos }}_${{ matrix.goarch }}"
|
|
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: ${{ needs.prepare.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" ./cmd/opencode2api
|
|
cp README.md README.zh-CN.md LICENSE config.example.json "dist/$PACKAGE_NAME/"
|
|
cp -r docs "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: [prepare, build]
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: write
|
|
steps:
|
|
- 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: ${{ needs.prepare.outputs.release_tag }}
|
|
run: |
|
|
release_type=()
|
|
if [[ "$RELEASE_TAG" == *-* ]]; then
|
|
release_type+=(--prerelease)
|
|
fi
|
|
if gh release view "$RELEASE_TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
|
|
gh release upload "$RELEASE_TAG" release-files/* --clobber --repo "$GITHUB_REPOSITORY"
|
|
else
|
|
gh release create "$RELEASE_TAG" release-files/* "${release_type[@]}" --generate-notes --title "$RELEASE_TAG" --target "$GITHUB_SHA" --repo "$GITHUB_REPOSITORY"
|
|
fi
|