name: Publish Docker image
on:
  push:
    branches:
      - 'main'
    tags:
      - 'v*'
jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
    
      - name: Check Out Repo
        uses: actions/checkout@v6

      - name: Extract Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: tufin/oasdiff
          
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

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

      - name: Build and push main
        id: docker_build_main
        uses: docker/build-push-action@v6
        with:
          push: true
          platforms: linux/amd64,linux/arm64
          context: ./
          file: ./Dockerfile
          tags: tufin/oasdiff:main, tufin/oasdiff:latest
          labels: ${{ steps.meta.outputs.labels }}
        if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'

      - name: Build and push tag
        id: docker_build_tag
        uses: docker/build-push-action@v6
        with:
          push: true
          platforms: linux/amd64,linux/arm64
          context: ./
          file: ./Dockerfile
          tags: tufin/oasdiff:${{ github.ref_name }}, tufin/oasdiff:stable, tufin/oasdiff:latest
          labels: ${{ steps.meta.outputs.labels }}
        if: github.ref != 'refs/heads/main' && github.event_name != 'pull_request'

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
