# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
  # release:
  #   types: [published]
  workflow_dispatch:

jobs:
  build-bin:
  #   permissions:
  #     contents: write
    # runs-on: ubuntu-latest
    runs-on: debian-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Print build env
      run: |
        cat /etc/os-release
        uname -a
    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.24.4'
    - name: Setup NodeJS
      uses: actions/setup-node@v1
      with:
        node-version: '24.2.0'
    - name: Build web
      working-directory: ./apps/web
      run: |
        npm ci
        npm run build
        mv dist/* ../api/web
    - name: Build server for linux
      working-directory: ./apps/api
      run: GOOS=linux go build -ldflags="-s -w" --tags=fts5 -o ./yabl .
    - name: Build book-reaper for linux
      working-directory: ./apps/book-reaper
      run: GOOS=linux go build -ldflags="-s -w" --tags=fts5 -o ./book-reaper ./cmd/book-reaper
    - name: Upload build artifacts
      uses: actions/upload-artifact@v3
      with:
        name: build-binaries
        path: |
          apps/api/yabl
          apps/book-reaper/book-reaper

  # echo BUILD FOR DARWIN
  # GOOS=darwin go build -v -o ./yabl-darwin-x86_64 .
  # echo BUILD FOR WINDOWS
  # GOOS=windows go build -v -o ./yabl-windows-x86_64.exe .
  # echo done
    # - name: upload linux artifact
    #   uses: actions/upload-release-asset@v1
    #   env:
    #     GITHUB_TOKEN: ${{ github.token }}
    #   with:
    #     upload_url: ${{ github.event.release.upload_url }}
    #     asset_path: ./yabl-linux-x86_64
    #     asset_name: server-linux
    #     asset_content_type: application/octet-stream
    # - name: upload darwin artifact
    #   uses: actions/upload-release-asset@v1
    #   env:
    #     GITHUB_TOKEN: ${{ github.token }}
    #   with:
    #     upload_url: ${{ github.event.release.upload_url }}
    #     asset_path: ./yabl-darwin-x86_64
    #     asset_name: yabl-darwin-x86_64
    #     asset_content_type: application/octet-stream
    # - name: upload windows artifact
    #   uses: actions/upload-release-asset@v1
    #   env:
    #     GITHUB_TOKEN: ${{ github.token }}
    #   with:
    #     upload_url: ${{ github.event.release.upload_url }}
    #     asset_path: ./yabl-windows-x86_64.exe
    #     asset_name: yabl-windows-x86_64.exe
    #     asset_content_type: application/octet-stream
  publish-docker:
    runs-on: debian-latest
    needs: [build-bin]

    steps:
      - uses: actions/checkout@v3
      - name: Download build artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-binaries
          path: ./build
      - name: Build Docker image
        run: |
          docker build -t git.mi6e4ka.dev/yabl/app:latest -f ./docker/Dockerfile .
      - name: Login to local Docker registry
        run: |
          echo ${{ secrets.REGISTRY_PASSWORD }} | docker login git.mi6e4ka.dev \
            --username ${{ secrets.REGISTRY_USERNAME }} \
            --password-stdin
      - name: Push image
        run: |
          docker push git.mi6e4ka.dev/yabl/app:latest 
