name: Images

on:
  pull_request:
    branches: [ main ]
  push:
    branches:
      - main
      - "image/**"
      - "*image"
      - "image*"
    tags:
      - 'v*'
    paths-ignore:
      - .github/workflows/ci.yml
      - .github/workflows/lock.yml
      - ".github/ISSUE_TEMPLATE/**"
      - "docs/**"
      - "hack/**"
      - "**.md"
      - ".gitignore"
      - "Makefile"

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    strategy:
      matrix:
        dockerfile:
          - Dockerfile
          - Dockerfile.debian-stable-slim
          - Dockerfile.ubuntu
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

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

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

      - name: Set up Image Variables
        id: vars
        uses: ./.github/actions/setup-image-vars
        with:
          dockerfile: ${{ matrix.dockerfile }}

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

      - name: Build / Push
        uses: docker/build-push-action@v3
        with:
          context: .
          file: ${{ matrix.dockerfile }}
          platforms: linux/amd64,linux/arm64
          push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
          tags: |
            ghcr.io/${{ github.repository }}${{ steps.vars.outputs.suffix }}:${{ steps.vars.outputs.tag }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
