name: Build Push and Restart Compose

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    env:
      IMAGE_NAME: lshfly-registry.duckdns.org/stocksearch:latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Check secrets
        run: |
          test -n "${{ secrets.REGISTRY_USERNAME }}" || (echo "REGISTRY_USERNAME is empty" && exit 1)
          test -n "${{ secrets.REGISTRY_PASSWORD }}" || (echo "REGISTRY_PASSWORD is empty" && exit 1)

      - name: Login to registry
        run: |
          echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login lshfly-registry.duckdns.org \
            -u "${{ secrets.REGISTRY_USERNAME }}" \
            --password-stdin

      - name: Build image
        run: |
          docker build -t ${IMAGE_NAME} .

      - name: Push image
        run: |
          docker push ${IMAGE_NAME}

      - name: Restart compose
        run: |
          cd /home/opc/programs
          docker compose pull
          docker compose up -d
