name: Build and Publish Docker Image

on:
  workflow_dispatch:
  push:
    tags: 
      - 'v*.*.*'
    
    # Don't trigger if it's just a documentation update
    paths-ignore:
      - '**.md'
      - '**.MD'
      - '**.yml'
      - 'docs/**'
      - 'LICENSE'
      - '.gitattributes'
      - '.gitignore'

jobs:
  build_and_publish_base_image:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v7

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v4

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

      - name: Set Release Date
        id: release_date
        run: |
          echo "release_date=$(date --rfc-3339=date)" >> $GITHUB_OUTPUT

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/${{ github.repository_owner }}/kcc
          # Always creates the "latest" tag
          flavor: |
            latest=true
          tags: |
            type=ref,event=tag
            type=raw,value=${{ steps.release_date.outputs.release_date }}

      - name: Build and push
        uses: docker/build-push-action@v7
        with:
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          context: .
          push: true
          tags: |
            ${{ steps.meta.outputs.tags }}
          cache-from: |
            type=registry,ref=ghcr.io/ciromattia/kcc:cache
            type=registry,ref=ghcr.io/${{ github.repository_owner }}/kcc:cache
          cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/kcc:cache,mode=max
