name: Build and Push Docker Image

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build-and-push-image:
    name: 'Docker Build and Push'
    env:
      REGISTRY_USERNAME: nicholas
      IMAGE_REGISTRY: git.uuard.com
      IMAGE_NAME: nicholas/proving-ground
      IMAGE_TAG: ${{ github.sha }}
    runs-on: shared-ci
    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@v4

      - name: 🔐 Gitea Registry Login
        uses: docker/login-action@v3
        with:
          registry: ${{ env.IMAGE_REGISTRY }}
          username: ${{ env.REGISTRY_USERNAME }}
          password: ${{ secrets.CD_CD_BUILD_PUSH }}

      - name: 🐋 Build Docker image
        run: docker build -t $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_TAG ./container

      - name: 🚀 Push Docker image
        run: docker push $IMAGE_REGISTRY/$IMAGE_NAME:$IMAGE_TAG

      - name: 🧹 Prune images
        if: always()
        run: docker image prune -f
