name: showbridge release

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

permissions:
  contents: write

jobs:
  linux_amd64:
    runs-on: ubuntu-24.04
    steps:
      - name: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v6
        with:
          go-version-file: "go.mod"
      - name: install alsa lib
        run: sudo apt-get install -y libasound2-dev
      - name: setup env variables
        run: |
          echo "SHOWBRIDGE_VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
          echo "SHORT_COMMIT=${GITHUB_SHA::7}" >> $GITHUB_ENV
      - name: clean tag
        if: startsWith(github.ref, 'refs/tags/v')
        run: echo "SHOWBRIDGE_VERSION=${SHOWBRIDGE_VERSION#v}" >> $GITHUB_ENV
      - name: build
        run: |
          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
  linux_arm64:
    runs-on: ubuntu-24.04-arm
    steps:
      - name: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v6
        with:
          go-version-file: "go.mod"
      - name: install alsa lib
        run: sudo apt-get install -y libasound2-dev
      - name: setup env variables
        run: |
          echo "SHOWBRIDGE_VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
          echo "SHORT_COMMIT=${GITHUB_SHA::7}" >> $GITHUB_ENV
      - name: clean tag
        if: startsWith(github.ref, 'refs/tags/v')
        run: echo "SHOWBRIDGE_VERSION=${SHOWBRIDGE_VERSION#v}" >> $GITHUB_ENV
      - name: build
        run: |
          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
  windows:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: setup go
        uses: actions/setup-go@v6
        with:
          go-version-file: "go.mod"
      - name: setup zig
        uses: mlugg/setup-zig@v2
        with:
          version: 0.16.0
      - name: setup env variables
        run: |
          echo "SHOWBRIDGE_VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
          echo "SHORT_COMMIT=${GITHUB_SHA::7}" >> $GITHUB_ENV
      - name: clean tag
        if: startsWith(github.ref, 'refs/tags/v')
        run: echo "SHOWBRIDGE_VERSION=${SHOWBRIDGE_VERSION#v}" >> $GITHUB_ENV
      - 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 -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          zip showbridge_${SHOWBRIDGE_VERSION}_windows-amd64.zip showbridge
      - 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 -ldflags="-s -w -X main.version=${SHOWBRIDGE_VERSION}-${SHORT_COMMIT}" ./cmd/showbridge
          zip showbridge_${SHOWBRIDGE_VERSION}_windows-arm64.zip showbridge

      - name: upload windows artifacts
        uses: actions/upload-artifact@v7
        if: github.event_name == 'workflow_dispatch'
        with:
          name: showbridge-windows
          path: |
            showbridge_*.zip
  docker:
    runs-on: ubuntu-latest
    needs: linux_amd64
    steps:
      - name: Check out repository
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0

      - name: Set up QEMU
        uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

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

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

      - name: Build and push
        uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
        if: github.ref_type == 'tag'
        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
