name: showbridge release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  setup_release:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.set_version.outputs.version }}
      short_commit: ${{ steps.set_commit.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: set environment variables
        run: echo "SHOWBRIDGE_VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
      - name: clean tag
        if: startsWith(github.ref, 'refs/tags/v')
        run: echo "SHOWBRIDGE_VERSION=${SHOWBRIDGE_VERSION#v}" >> $GITHUB_ENV
      - name: set version
        id: set_version
        run: echo "version=${SHOWBRIDGE_VERSION}" >> $GITHUB_OUTPUT
      - name: set commit
        id: set_commit
        run: echo "short_commit=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
      - name: create release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          generate_release_notes: true
  linux_amd64:
    runs-on: ubuntu-24.04
    needs: setup_release
    env:
      SHOWBRIDGE_VERSION: ${{ needs.setup_release.outputs.version }}
      SHORT_COMMIT: ${{ needs.setup_release.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"
      - name: install alsa lib
        run: sudo apt-get install -y libasound2-dev
      - name: build
        run: |
          CGO_ENABLED=1 go build -o showbridge -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          tar -czf showbridge_${SHOWBRIDGE_VERSION}_linux-amd64.tar.gz showbridge
      - name: upload linux artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-linux-amd64
          path: |
            showbridge_*.tar.gz
      - name: upload artifacts to release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          files: showbridge_*.tar.gz
  linux_arm64:
    runs-on: ubuntu-24.04-arm
    needs: setup_release
    env:
      SHOWBRIDGE_VERSION: ${{ needs.setup_release.outputs.version }}
      SHORT_COMMIT: ${{ needs.setup_release.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"
      - name: install alsa lib
        run: sudo apt-get install -y libasound2-dev
      - name: build
        run: |
          CGO_ENABLED=1 go build -o showbridge -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          tar -czf showbridge_${SHOWBRIDGE_VERSION}_linux-arm64.tar.gz showbridge
      - name: upload linux artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-linux-arm64
          path: |
            showbridge_*.tar.gz
      - name: upload artifacts to release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          files: showbridge_*.tar.gz
  windows:
    runs-on: ubuntu-latest
    needs: setup_release
    env:
      SHOWBRIDGE_VERSION: ${{ needs.setup_release.outputs.version }}
      SHORT_COMMIT: ${{ needs.setup_release.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"
      - name: setup zig
        uses: mlugg/setup-zig@v2
        with:
          version: 0.16.0
      - name: build amd64
        run: |
          CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC="zig cc -target x86_64-windows-gnu" CXX="zig c++ -target x86_64-windows-gnu" go build -o showbridge.exe -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          zip showbridge_${SHOWBRIDGE_VERSION}_windows-amd64.zip showbridge.exe
      - name: build arm64
        run: |
          CGO_ENABLED=1 GOOS=windows GOARCH=arm64 CC="zig cc -target aarch64-windows-gnu" CXX="zig c++ -target aarch64-windows-gnu" go build -o showbridge.exe -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          zip showbridge_${SHOWBRIDGE_VERSION}_windows-arm64.zip showbridge.exe
      - name: upload windows artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-windows
          path: |
            showbridge_*.zip
      - name: upload artifacts to release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          files: showbridge_*.zip
  macos_arm64:
    runs-on: macos-26
    needs: setup_release
    env:
      SHOWBRIDGE_VERSION: ${{ needs.setup_release.outputs.version }}
      SHORT_COMMIT: ${{ needs.setup_release.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"
      - name: build
        run: |
          CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -o showbridge -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          tar -czf showbridge_${SHOWBRIDGE_VERSION}_macOS-arm64.tar.gz showbridge
      - name: upload macos artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-macos-arm64
          path: |
            showbridge_*.tar.gz
      - name: upload artifacts to release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          files: showbridge_*.tar.gz
  macos_amd64:
    runs-on: macos-26-intel
    needs: setup_release
    env:
      SHOWBRIDGE_VERSION: ${{ needs.setup_release.outputs.version }}
      SHORT_COMMIT: ${{ needs.setup_release.outputs.short_commit }}
    steps:
      - name: checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v7
        with:
          go-version-file: "go.mod"
      - name: build
        run: |
          CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o showbridge -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          tar -czf showbridge_${SHOWBRIDGE_VERSION}_macOS-amd64.tar.gz showbridge
      - name: upload macos artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-macos-amd64
          path: |
            showbridge_*.tar.gz
      - name: upload artifacts to release
        uses: softprops/action-gh-release@v3
        if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')
        with:
          draft: true
          files: showbridge_*.tar.gz
  docker:
    runs-on: ubuntu-latest
    needs: setup_release
    if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch'
    steps:
      - name: Check out repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0

      - name: Login to Docker Hub
        uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Setup Docker metadata
        id: meta
        uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
        with:
          images: |
            jwetzell/showbridge

      - name: Build and push
        uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
        with:
          push: true
          context: ./
          file: ./Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          platforms: linux/amd64
