name: release

on:
  push:
    tags:
    - 'v*'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - id: get-version
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - uses: docker/login-action@v3
        with:
          username: ${{ github.repository_owner }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            zhaojh329/rttys:${{ steps.get-version.outputs.version }}
            zhaojh329/rttys:latest

  ghcr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - id: get-version
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ghcr.io/${{ github.repository_owner }}/rttys:${{ steps.get-version.outputs.version }}
            ghcr.io/${{ github.repository_owner }}/rttys:latest

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - id: get-version
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - uses: actions/setup-go@v5
        with:
          go-version: 'stable'

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build ui
        run: |
          cd ui
          npm install
          npm run build
          cd ..

      - name: Build rttys release
        run: |
          sudo apt-get install -y ruby ruby-dev rubygems build-essential
          sudo gem install --no-document fpm
          ./build-deb.sh amd64
          ./build-deb.sh arm64
          COMPRESS=1 ./build.sh linux amd64
          COMPRESS=1 ./build.sh linux arm64
          COMPRESS=1 ./build.sh windows amd64

      - name: Upload Release Asset
        uses: softprops/action-gh-release@v1
        with:
          files: |
            rttys_${{ steps.get-version.outputs.version }}_amd64.deb
            rttys_${{ steps.get-version.outputs.version }}_arm64.deb
            rttys-${{ steps.get-version.outputs.version }}-linux-amd64.tar.bz2
            rttys-${{ steps.get-version.outputs.version }}-linux-arm64.tar.bz2
            rttys-${{ steps.get-version.outputs.version }}-windows-amd64.tar.bz2
          draft: true
