---
name: Docker images build and push

on:
    push:
        branches:
            - develop
        paths:
            - util/dockerfiles/**
    workflow_dispatch:


jobs:

    obtain-targets:
        runs-on: ubuntu-24.04
        outputs:
            targets: ${{ steps.generate.outputs.targets }}

        steps:
            - name: Checkout
              uses: actions/checkout@v3

            - name: List targets
              id: generate
              uses: docker/bake-action/subaction/list-targets@v4
              with:
                  target: default
                  workdir: util/dockerfiles

    docker-buildx-bake-base-images:
        # This job is used to build the base images -- the images which other
        # images use as a base (usually an Ubuntu release).
        # We do this prior to building the other images to ensure that the base
        # are available when building the other images.
        runs-on: [self-hosted, linux, x64]
        steps:
            - name: Set up QEMU
              uses: docker/setup-qemu-action@v3

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

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

            - name: Build and Push
              uses: docker/bake-action@v6
              with:
                  source: '{{defaultContext}}:util/dockerfiles'
                  targets: base-images
                  provenance: true
                  push: true

    docker-buildx-bake-all:
        runs-on: [self-hosted, linux, x64]
        needs:
            - obtain-targets
            - docker-buildx-bake-base-images

        strategy:
            fail-fast: false
            # Limit the number of parallel jobs to not overwhelm the runners
            max-parallel: 4
            matrix:
                target: ${{ fromJSON(needs.obtain-targets.outputs.targets) }}

        permissions:
            packages: write
            contents: read

        steps:

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

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

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

            - name: Build and Push
              uses: docker/bake-action@v6
              with:
                  source: '{{defaultContext}}:util/dockerfiles'
                  targets: ${{ matrix.target }}
                  provenance: true
                  push: true