on:
  push:
    branches:
      - main
  workflow_dispatch:

name: Web Build
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Set version
        run: |
          if [[ "${GITHUB_REF}" == refs/heads/main ]]; then
              echo "RELEASE_VERSION=latest" >> $GITHUB_ENV
          else
              echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
          fi

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

      - name: Login to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: .
          file: ./Dockerfile
          push: true
          cache-from: type=gha
          cache-to: type=gha,mode=max
          tags: |
            ghcr.io/imnyang/imnya.ng:latest
            ghcr.io/imnyang/imnya.ng:${{ github.run_id }}
  update:
    runs-on: self-hosted
    steps:
      - name: Pull and restart container
        run: |
          docker-compose -f /home/neko/Git/imnya.ng/compose.yml pull web
          docker-compose -f /home/neko/Git/imnya.ng/compose.yml up -d web
    needs: build
