name: Build Docker Image

on:
  push:
    branches:
      - deployment

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Debug Runner Environment
        run: |
          export GIT_DISCOVERY_ACROSS_FILESYSTEM=1
          echo "Current Directory: $(pwd)"
          echo "GITHUB_WORKSPACE: $GITHUB_WORKSPACE"
          docker version || echo "Docker not installed"
          git --version || echo "Git not installed"

      - name: Debug Workspace
        run: |
          echo "Listing workspace directory:"
          ls -la $GITHUB_WORKSPACE

      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Verify Repository
        run: |
          cd $GITHUB_WORKSPACE
          git status || echo "Not a valid Git repository"

      - name: Clean Workspace
        run: |
          mkdir -p $GITHUB_WORKSPACE
          cd $GITHUB_WORKSPACE
          git reset --hard
          git clean -fdx

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

      - name: Login to Gitea Docker Registry
        run: |
          echo "${{ secrets.DOCKER_REGISTRY_TOKEN }}" | docker login git.internet-master.cz --username "gitea" --password-stdin

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: git.internet-master.cz/fencl/martinfencl.eu:latest

      - name: Clean up Docker Environment
        run: |
          docker system prune -f
          docker logout git.internet-master.cz
      
      #- name: Call Webhook to redeploy the container
      #  run: |
      #    curl -k -X POST https://192.168.4.8:9443/api/webhooks/1b9a79d8-682a-4304-a990-c2b71b79e038
