name: docker-collect-testing

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-*-test"
      - ".github/scripts/collect-prepare-test-robot.sh"
      - ".github/scripts/collect-setup-database.sh"
      - "vcpkg.json"
      - "resources/*.sql"
  pull_request:
    paths:
      - ".github/docker/Dockerfile.centreon-collect-*-test"
      - ".github/scripts/collect-prepare-test-robot.sh"
      - ".github/scripts/collect-setup-database.sh"
      - "vcpkg.json"
      - "resources/*.sql"

jobs:
  dependency-scan:
    uses: centreon/security-tools/.github/workflows/dependency-analysis.yml@main

  get-environment:
    needs: [dependency-scan]
    if: github.repository == 'centreon/centreon-collect'
    uses: ./.github/workflows/get-environment.yml
    with:
      version_file: CMakeLists.txt

  dockerize:
    needs: [get-environment]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      github.repository == 'centreon/centreon-collect'

    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: centreon-collect
            distrib: alma8
            database: mariadb
          - runner: centreon-collect
            distrib: alma9
            database: mariadb
          - runner: centreon-collect
            distrib: alma10
            database: mariadb
          - runner: centreon-collect
            distrib: alma8
            database: mysql
            dockerfile: centreon-collect-mysql-alma8-test
            image: centreon-collect-mysql-alma8-test
          - runner: centreon-collect
            distrib: alma9
            database: mysql
            dockerfile: centreon-collect-mysql-alma9-test
            image: centreon-collect-mysql-alma9-test
          - runner: centreon-collect
            distrib: bookworm
            database: mariadb
          - runner: centreon-collect-arm64
            distrib: bookworm
            database: mariadb
          - runner: centreon-collect
            distrib: trixie
            database: mariadb
          - runner: centreon-collect-arm64
            distrib: trixie
            database: mariadb
          - runner: centreon-collect
            distrib: bullseye
            database: mariadb
          - runner: centreon-collect-arm64
            distrib: bullseye
            database: mariadb
          - runner: centreon-collect
            distrib: jammy
            database: mariadb
          - runner: centreon-collect-arm64
            distrib: jammy
            database: mariadb
          # - runner: centreon-collect
          #   distrib: noble
          #   database: mariadb
          # - runner: centreon-collect-arm64
          #   distrib: noble
          #   database: mariadb

    runs-on: ${{ matrix.runner }}

    name: build image ${{ matrix.database }} ${{ matrix.distrib }} ${{ contains(matrix.runner, 'arm') && ' arm' || '' }}

    env:
      IMAGE_NAME: centreon-collect-${{ matrix.database }}-${{ matrix.distrib }}${{ contains(matrix.runner, 'arm') && '-arm64' || '' }}-test

    steps:
      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

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

      - uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
        with:
          driver-opts: image=${{ vars.DOCKER_PROXY_REGISTRY_URL }}/moby/buildkit:buildx-stable-1

      - name: Build image ${{ env.IMAGE_NAME }}:${{ needs.get-environment.outputs.test_img_version }}
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
        env:
          DOCKER_BUILD_RECORD_UPLOAD: false
          DOCKER_BUILD_SUMMARY: false
          DOCKER_BUILD_CHECKS_ANNOTATIONS: false
        with:
          file: .github/docker/Dockerfile.centreon-collect-${{ matrix.database }}-${{ matrix.distrib }}-test
          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 }}/${{ env.IMAGE_NAME }}:${{ needs.get-environment.outputs.test_img_version }}

  set-skip-label:
    needs: [get-environment, dockerize]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    uses: ./.github/workflows/set-pull-request-skip-label.yml
