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" --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/* --generate-notes --title "$RELEASE_TAG" --target "$GITHUB_SHA" --repo "$GITHUB_REPOSITORY" fi