name: Build and Publish Docker Image

#on:
#  push:
#    branches: [ main, master ]
#    tags: [ 'v*' ]
#  pull_request:
#    branches: [ main, master ]
on:
  push:
    branches:
      - master # publish release on master
      - develop # publish snapshot on develop
      - feat-58-backpressure-reactive-streams
  workflow_dispatch:
  # allow manual trigger

env:
  REGISTRY_URL: "git.cleverthis.com"
  REPOSITORY: "clevermicro/amq-adapter-python-demo"
  DOCKER_HOST: "tcp://dind:2375"

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    services:
      dind:
        image: docker:dind
        cmd:
          - dockerd
          - -H
          - tcp://0.0.0.0:2375
          - --tls=false
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref_name }}

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

      - name: Login to Docker Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY_URL }}
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
        if: github.event_name != 'pull_request'

      - name: Extract metadata for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY_URL }}/clevermicro/amq-adapter-python-demo
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha,format=short

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          # push: ${{ github.event_name != 'pull_request' }}
          push: true
          tags: ${{ env.REGISTRY_URL }}/${{ env.REPOSITORY }}:latest
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
