name: Build

on:
  push:
    branches:
      - main
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      DATABASE_URL: postgresql://watchlink:watchlink@localhost:5432/watchlink?schema=public
      IMAGE_NAME: git.wilkensxl.de/mrsphay/watchlink:latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Install dependencies
        run: npm install

      - name: Typecheck
        run: npm run typecheck

      - name: Test
        run: npm run test

      - name: Build
        run: npm run build

      - name: Build Docker image
        run: docker build -t "$IMAGE_NAME" .

      - name: Publish Docker image
        if: ${{ secrets.REGISTRY_TOKEN != '' }}
        run: |
          echo "${{ secrets.REGISTRY_TOKEN }}" | docker login git.wilkensxl.de -u "${{ gitea.actor }}" --password-stdin
          docker push "$IMAGE_NAME"
