name: docker-builder

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

on:
  workflow_dispatch:
  push:
    branches:
      - develop
      - dev-[2-9][0-9].[0-9][0-9].x
    paths:
      - '.github/docker/Dockerfile.centreon-collect-*'
  pull_request:
    paths:
      - '.github/docker/Dockerfile.centreon-collect-*'

jobs:
  get-environment:
    uses: ./.github/workflows/get-environment.yml
    with:
      version_file: CMakeLists.txt

  create-and-push-docker:
    needs: [get-environment]

    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: collect
            dockerfile: centreon-collect-alma8
            image: centreon-collect-alma8
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect
            dockerfile: centreon-collect-alma9
            image: centreon-collect-alma9
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect
            dockerfile: centreon-collect-alma9-test
            image: centreon-collect-alma9-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}
          - runner: collect
            dockerfile: centreon-collect-mysql-alma9
            image: centreon-collect-mysql-alma9
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect
            dockerfile: centreon-collect-mysql-alma9-test
            image: centreon-collect-mysql-alma9-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}
          - runner: collect
            dockerfile: centreon-collect-debian-bullseye
            image: centreon-collect-debian-bullseye
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect
            dockerfile: centreon-collect-debian-bullseye-test
            image: centreon-collect-debian-bullseye-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}
          - runner: collect
            dockerfile: centreon-collect-debian-bookworm
            image: centreon-collect-debian-bookworm
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect
            dockerfile: centreon-collect-debian-bookworm-test
            image: centreon-collect-debian-bookworm-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}
          - runner: collect
            dockerfile: centreon-collect-ubuntu-jammy
            image: centreon-collect-ubuntu-jammy
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect-arm64
            dockerfile: centreon-collect-debian-bullseye
            image: centreon-collect-debian-bullseye-arm64
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect-arm64
            dockerfile: centreon-collect-debian-bullseye-test
            image: centreon-collect-debian-bullseye-arm64-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}
          - runner: collect-arm64
            dockerfile: centreon-collect-debian-bookworm
            image: centreon-collect-debian-bookworm-arm64
            tag: ${{ needs.get-environment.outputs.img_version }}
          - runner: collect-arm64
            dockerfile: centreon-collect-debian-bookworm-test
            image: centreon-collect-debian-bookworm-arm64-test
            tag: ${{ needs.get-environment.outputs.test_img_version }}

    runs-on: ${{ fromJson(format('["self-hosted", "{0}"]', matrix.runner)) }}

    name: build container ${{ matrix.image }}

    steps:
      - name: Checkout sources
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Login to Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}
          username: ${{ secrets.HARBOR_CENTREON_PUSH_USERNAME }}
          password: ${{ secrets.HARBOR_CENTREON_PUSH_TOKEN }}

      - name: Login to Proxy Registry
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          registry: ${{ vars.DOCKER_PROXY_REGISTRY_URL }}
          username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
          password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}

      - uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1

      - name: Build image ${{ matrix.image }}:${{ matrix.tag }}
        uses: docker/build-push-action@5176d81f87c23d6fc96624dfdbcd9f3830bbe445 # v6.5.0
        with:
          file: .github/docker/Dockerfile.${{ matrix.dockerfile }}
          context: .
          build-args: |
            "REGISTRY_URL=${{ vars.DOCKER_PROXY_REGISTRY_URL }}"
            "TRIPLET=${{ contains(matrix.runner, 'arm') && 'arm64-linux' || 'x64-linux' }}"
          platforms: ${{ contains(matrix.runner, 'arm') && 'linux/arm64' || 'linux/amd64' }}
          pull: true
          push: true
          tags: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ matrix.image }}:${{ matrix.tag }}
