name: Build and Push Docker Container
on:
  push:
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          token: '${{ secrets.ACTION_ACCESS_TOKEN }}'
          submodules: recursive
          lfs: true

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

      - name: Login to Gitea Registry
        uses: docker/login-action@v4
        with:
          registry: ${{ vars.DOCKER_REGISTRY_URL }}
          username: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
          password: ${{ secrets.ACTION_ACCESS_TOKEN }}

      - name: Repo owner to lowercase (for Gitea namespace)
        run: echo "REPO_OWNER_LC=$(echo '${{ gitea.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

      - name: Build and push Docker image for latest tag
        uses: docker/build-push-action@v7
        with:
          context: .
          push: true
          tags: ${{ vars.DOCKER_REGISTRY_URL }}/${{ env.REPO_OWNER_LC }}/simple-nanoshare:latest
          platforms: linux/amd64

      - name: Invoke Portainer Stack Deployment
        if: ${{ vars.PORTAINER_STACK_WEBHOOK_URL && vars.PORTAINER_STACK_WEBHOOK_URL != '' }}
        uses: distributhor/workflow-webhook@v3
        with:
          webhook_url: ${{ vars.PORTAINER_STACK_WEBHOOK_URL }}
