name: Build and Push Docker Image
run-name: Build triggered by ${{ gitea.actor }}

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-22.04 # Using specific Ubuntu version instead of latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Log in to private registry
        uses: docker/login-action@v3
        with:
          registry: registry.acayip.dev
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Generate image tag
        id: meta
        run: |
          VERSION=$(date +%Y%m%d)-$(git rev-parse --short HEAD)
          echo "TAG=registry.acayip.dev/log101-dot-dev:${VERSION}" >> $GITHUB_OUTPUT

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: ${{ gitea.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.TAG }}
          # Add build caching for faster builds
          cache-from: type=gha
          cache-to: type=gha,mode=max
          # Add labels for better tracking
          labels: |
            org.opencontainers.image.source=${{ gitea.repository }}
            org.opencontainers.image.revision=${{ gitea.sha }}
            org.opencontainers.image.created=${{ gitea.event.created_at }}
