name: monitoring-agent

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

on:
  schedule:
    - cron: "30 0 * * 1-5"
  workflow_dispatch:
    inputs:
      nightly_manual_trigger:
        description: "Set to true for linux nightly run"
        required: true
        default: false
        type: boolean
      force_signing:
        description: "Force signing"
        required: true
        default: false
        type: boolean
      labels:
        description: "Labels to add to the run (comma separated)"
        required: false
        default: ""
        type: string

  pull_request:
    paths:
      - ".github/workflows/monitoring-agent.yml"
      - ".version"
      - ".version.centreon-monitoring-agent"
      - agent/**
      - common/**
      - custom-triplets/**
      - packaging/centreon-monitoring-agent/**
      - CMakeLists.txt
      - CMakeListsLinux.txt
      - CMakeListsWindows.txt
      - vcpkg.json
      - tests/**/cma.robot
  push:
    branches:
      - develop
      - dev-[2-9][0-9].[0-9][0-9].x
      - master
      - "[2-9][0-9].[0-9][0-9].x"
      - "release-[2-9][0-9].[0-9][0-9]-next"
      - "hotfix-[2-9][0-9].[0-9][0-9]-next"
    paths:
      - ".version"
      - ".version.centreon-monitoring-agent"
      - agent/**
      - common/**
      - custom-triplets/**
      - packaging/centreon-monitoring-agent/**
      - CMakeLists.txt
      - CMakeListsLinux.txt
      - CMakeListsWindows.txt
      - vcpkg.json
      - tests/**/cma.robot
    tags:
      - "centreon-monitoring-agent-*"

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

  get-environment:
    needs: [dependency-scan]
    uses: ./.github/workflows/get-environment.yml
    with:
      version_file: CMakeLists.txt
      nightly_manual_trigger: ${{ inputs.nightly_manual_trigger || false }}
      labels: ${{ inputs.labels || '' }}

  check-version-consistency:
    runs-on: ubuntu-24.04
    needs: [get-environment]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability == 'testing'
    env:
      version: ${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}
      version_file: .version.centreon-monitoring-agent
      cmake_file: CMakeLists.txt
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Check ${{ env.version_file }}
        uses: ./.github/actions/check-version-consistency
        with:
          file: ${{ env.version_file }}
          version: ${{ env.version }}

      - name: Check ${{ env.cmake_file }}
        uses: ./.github/actions/check-version-consistency
        with:
          file: ${{ env.cmake_file }}
          version: ${{ env.version }}

  changes:
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable'
    needs: [get-environment]
    uses: ./.github/workflows/get-changes-triggers.yml
    with:
      production_paths: |
        ".version"
        ".version.centreon-monitoring-agent"
        "agent/**"
        "common/**"
        "custom-triplets/**"
        "packaging/centreon-monitoring-agent/**"
        "cmake.sh"
        "cmake-vcpkg.sh"
        "CMakeLists.txt"
        "CMakeListsLinux.txt"
        "CMakeListsWindows.txt"
        "vcpkg.json"
        "overlays/**"
        "overlays-windows/**"
        "selinux/centreon-monitoring-agent/**"
        "!**/test/**"
        "!**/tests/**"
      unit_test_paths: |
        "agent/test/**"
      e2e_test_paths: |
        "resources/**"
        "tests/**/cma.robot"
      is_nightly: ${{ needs.get-environment.outputs.is_nightly }}
      base_sha: ${{ needs.get-environment.outputs.previous_release_bundle_tag }}
      labels: ${{ needs.get-environment.outputs.labels }}

  build-and-test-agent-windows:
    needs: [get-environment, changes, check-version-consistency]
    if: |
      needs.changes.outputs.trigger_unit_testing == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    runs-on: windows-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.COLLECT_S3_SECRET_KEY }}
      AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
      AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
      AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}

    steps:
      # in order to have the same checksum between windows-agent and windows-agent-robot-test
      - name: No crlf conversion
        run: git config --system core.autocrlf false

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

      # Install Inno Setup (ISCC) and expose its path
      # To remove later when innosetup version will be stable on runners
      - name: Install Inno Setup
        shell: powershell
        run: |
          choco install innosetup --allow-downgrade -y --no-progress --version 6.6.1

      - name: Compile Agent only
        run: .github/scripts/windows-agent-compile.ps1
        shell: powershell

      - name: Common test
        run: |
          cd build_windows
          tests/ut_common

      - name: Agent test
        run: |
          cd build_windows
          tests/ut_agent

      - name: Sign agent
        if: |
          ( inputs.force_signing == true ||
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability)) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        uses: azure/trusted-signing-action@bb15ca63eb5548cc306f4f335c5617bb414abcad # v0.5.9
        with:
          azure-tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          azure-client-id: ${{ secrets.AZURE_CLIENT_ID }}
          azure-client-secret: ${{ secrets.AZURE_CLIENT_SECRET }}
          endpoint: https://weu.codesigning.azure.net/
          trusted-signing-account-name: Centreon-signature-RD
          certificate-profile-name: Cert-Signature-RD
          files-folder: build_windows\agent\Release
          files-folder-filter: centagent.exe
          files-folder-recurse: false
          file-digest: SHA256
          timestamp-rfc3161: http://timestamp.acs.microsoft.com
          timestamp-digest: SHA256

      - name: Build modifier
        run: |
          cmake -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=On -DWINDOWS=On -DBUILD_FROM_CACHE=On -S. -DVCPKG_CRT_LINKAGE=dynamic -DBUILD_SHARED_LIBS=OFF -DWITH_BUILD_AGENT_INSTALLER=OFF -DWITH_BUILD_AGENT_MODIFIER=On  -Bbuild_windows
          cmake --build build_windows --config Release

      - name: Sign modifier
        if: |
          ( inputs.force_signing == true ||
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability)) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        uses: azure/trusted-signing-action@bb15ca63eb5548cc306f4f335c5617bb414abcad # v0.5.9
        with:
          azure-tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          azure-client-id: ${{ secrets.AZURE_CLIENT_ID }}
          azure-client-secret: ${{ secrets.AZURE_CLIENT_SECRET }}
          endpoint: https://weu.codesigning.azure.net/
          trusted-signing-account-name: Centreon-signature-RD
          certificate-profile-name: Cert-Signature-RD
          files-folder: agent\installer
          files-folder-filter: centreon-monitoring-agent-modify.exe
          file-digest: SHA256
          timestamp-rfc3161: http://timestamp.acs.microsoft.com
          timestamp-digest: SHA256

      - name: Build installer and sign
        run: |
          cmake -DCMAKE_BUILD_TYPE=Release -DWITH_TESTING=On -DWINDOWS=On -DBUILD_FROM_CACHE=On -S. -DVCPKG_CRT_LINKAGE=dynamic -DBUILD_SHARED_LIBS=OFF -DWITH_BUILD_AGENT_INSTALLER=On -DWITH_INSTALLER_SIGNING=${{ (inputs.force_signing == true || contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability)) && 'ON' || 'OFF' }} -Bbuild_windows
          cmake --build build_windows --config Release

      - name: Installer test
        run: .github/scripts/agent_installer_test.ps1
        shell: powershell

      - name: Cache monitoring agent
        uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
        with:
          path: agent\installer\centreon-monitoring-agent.exe
          key: ${{ github.run_id }}-${{ github.sha }}-exe-centreon-monitoring-agent-${{ github.head_ref || github.ref_name }}

      - name: Upload package artifacts
        if: |
          contains(needs.get-environment.outputs.labels, 'upload-artifacts') ||
          (
            github.event_name != 'workflow_dispatch' &&
            contains(fromJson('["stable"]'), needs.get-environment.outputs.stability
          ) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled'))
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: packages-centreon-monitoring-agent-windows
          path: |
            agent\installer\centreon-monitoring-agent.exe
            build_windows\agent\Release\centagent.exe
            build_windows\agent\Release\centagent.pdb

  unit-test-linux:
    needs: [get-environment, changes, check-version-consistency]
    if: |
      needs.changes.outputs.trigger_unit_testing == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.get-environment.outputs.collect_matrix) }}
    runs-on: centreon-collect
    env:
      SCCACHE_PATH: "/usr/bin/sccache"
      SCCACHE_BUCKET: "centreon-github-sccache"
      SCCACHE_REGION: "eu-west-1"
      AWS_ACCESS_KEY_ID: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.COLLECT_S3_SECRET_KEY }}

    container:
      image: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/packaging-centreon-collect-vcpkg-${{ matrix.operating_system }}:${{ needs.get-environment.outputs.packaging_img_version }}
      credentials:
        username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
        password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}
      options: --cpus 8 --ulimit core=-1 --privileged

    name: unit test ${{ matrix.operating_system }} ${{ matrix.arch }}

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

      - name: Install sccache
        run: |
          wget https://github.com/mozilla/sccache/releases/download/v0.12.0/sccache-v0.12.0-x86_64-unknown-linux-musl.tar.gz
          tar xzf sccache-v0.12.0-x86_64-unknown-linux-musl.tar.gz
          mv sccache-v0.12.0-x86_64-unknown-linux-musl/sccache /usr/bin/

          ${SCCACHE_PATH} --start-server
        shell: bash

      - name: Compile centreon-collect
        run: |
          CMAKE="cmake"
          if [[ "${{ matrix.operating_system }}" == "alma8" || "${{ matrix.operating_system }}" == "alma9" ]]; then
            CMAKE="cmake3"
          fi

          export VCPKG_ROOT=/vcpkg
          export PATH=$VCPKG_ROOT:$PATH

          mv /root/.cache /github/home/

          CXXFLAGS="-Wall -Wextra" $CMAKE \
                  -B build \
                  -DVCPKG_OVERLAY_TRIPLETS=/custom-triplets \
                  -DVCPKG_TARGET_TRIPLET=x64-linux-release \
                  -DVCPKG_OVERLAY_PORTS=/overlays \
                  -GNinja \
                  -DDEBUG_ROBOT=OFF \
                  -DCMAKE_EXPORT_COMPILE_COMMANDS=On \
                  -DCMAKE_BUILD_TYPE=Debug \
                  -DWITH_PREFIX=/usr \
                  -DWITH_PREFIX_BIN=/usr/sbin \
                  -DWITH_USER_BROKER=centreon-broker \
                  -DWITH_USER_ENGINE=centreon-engine \
                  -DWITH_GROUP_BROKER=centreon-broker \
                  -DWITH_GROUP_ENGINE=centreon-engine \
                  -DWITH_TESTING=On \
                  -DWITH_PREFIX_MODULES=/usr/share/centreon/lib/centreon-broker \
                  -DWITH_PREFIX_CONF_BROKER=/etc/centreon-broker \
                  -DWITH_PREFIX_LIB_BROKER=/usr/lib64/nagios \
                  -DWITH_PREFIX_CONF_ENGINE=/etc/centreon-engine \
                  -DWITH_PREFIX_LIB_ENGINE=/usr/lib64/centreon-engine \
                  -DWITH_PREFIX_LIB_CLIB=/usr/lib64/ \
                  -DWITH_RW_DIR=/var/lib/centreon-engine/rw \
                  -DWITH_VAR_DIR=/var/log/centreon-engine \
                  -DWITH_MODULE_SIMU=On \
                  -DCMAKE_C_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DCMAKE_CXX_COMPILER_LAUNCHER=${SCCACHE_PATH} \
                  -DLEGACY_ENGINE=Off \
                  -DONLY_ROBOT_CMA=ON \
                  -S .

          ninja -Cbuild
          ninja -Cbuild install
        shell: bash

      - name: Cache statistics
        run: ${SCCACHE_PATH} --show-stats
        shell: bash

      - name: Stop sccache server
        run: ${SCCACHE_PATH} --stop-server
        shell: bash

      - name: Run agent common unit tests
        run: |
          .github/scripts/collect-ut.sh common
          .github/scripts/collect-ut.sh agent
        working-directory: .
        shell: bash

      - name: Upload test results
        if: ${{ failure() }}
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: unit-test-${{ matrix.operating_system }}-reports
          path: "build/ut_*"
          retention-days: 1

  package-linux:
    needs: [get-environment, changes, check-version-consistency]
    if: |
      needs.changes.outputs.trigger_packaging == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.get-environment.outputs.cma_matrix) }}

    name: package ${{ matrix.operating_system }} ${{ matrix.arch }}

    uses: ./.github/workflows/package-collect.yml

    with:
      major_version: ${{ needs.get-environment.outputs.major_version }}
      minor_version: ${{ needs.get-environment.outputs.minor_version }}
      cma_major_version: ${{ needs.get-environment.outputs.cma_major_version }}
      cma_minor_version: ${{ needs.get-environment.outputs.cma_minor_version }}
      img_version: ${{ needs.get-environment.outputs.packaging_img_version }}
      release: ${{ needs.get-environment.outputs.release }}
      commit_hash: ${{ github.sha }}
      stability: ${{ needs.get-environment.outputs.stability }}
      legacy_engine: "OFF"
      build_debug_packages: ${{ needs.get-environment.outputs.stability == 'testing' || contains(github.event.pull_request.labels.*.name, 'build-debug-packages') }}
      packages_in_artifact: ${{ needs.get-environment.outputs.is_nightly == 'true' || contains(needs.get-environment.outputs.labels, 'upload-artifacts') }}
      image: ${{ format('packaging-centreon-collect-vcpkg-{0}{1}', matrix.operating_system, matrix.arch == 'arm64' && '-arm64' || '') }}
      distrib: ${{ matrix.operating_system }}
      arch: ${{ matrix.arch }}
      is_nightly: ${{ needs.get-environment.outputs.is_nightly }}
      only_robot_cma: true
    secrets:
      collect_s3_access_key: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      collect_s3_secret_key: ${{ secrets.COLLECT_S3_SECRET_KEY }}
      registry_username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
      registry_password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}
      rpm_gpg_key: ${{ secrets.RPM_GPG_SIGNING_KEY }}
      rpm_gpg_signing_key_id: ${{ secrets.RPM_GPG_SIGNING_KEY_ID }}
      rpm_gpg_signing_passphrase: ${{ secrets.RPM_GPG_SIGNING_PASSPHRASE }}
      jira_base_url: ${{ secrets.JIRA_BASE_URL }}
      jira_user_email: ${{ secrets.XRAY_JIRA_USER_EMAIL }}
      jira_api_token: ${{ secrets.XRAY_JIRA_TOKEN }}

  robot-test:
    needs: [get-environment, changes, package-linux]
    if: |
      needs.changes.outputs.trigger_e2e_testing == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.get-environment.outputs.cma_robot_matrix) }}

    name: robot-test ${{ matrix.operating_system }} ${{ matrix.database_type }} ${{ matrix.arch }} ${{ contains(matrix.tests_params, 'grpc') && 'grpc' || '' }}

    uses: ./.github/workflows/robot-test.yml
    with:
      distrib: ${{ matrix.operating_system }}
      arch: ${{ matrix.arch }}
      test_img_version: ${{ needs.get-environment.outputs.test_img_version }}
      database_type: ${{ matrix.database_type }}
      tests_params: ${{ matrix.tests_params }}
      is_nightly: ${{ needs.get-environment.outputs.is_nightly }}
      features_pattern: 'cma.*\.robot'
      store_docker_image_in_cache: false
    secrets:
      registry_username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
      registry_password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}
      collect_s3_access_key: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      collect_s3_secret_key: ${{ secrets.COLLECT_S3_SECRET_KEY }}
      xray_client_id: ${{ secrets.XRAY_CLIENT_ID }}
      xray_client_secret: ${{ secrets.XRAY_CLIENT_SECRET }}
      jira_base_url: ${{ secrets.JIRA_BASE_URL }}
      jira_user_email: ${{ secrets.XRAY_JIRA_USER_EMAIL }}
      jira_api_token: ${{ secrets.XRAY_JIRA_TOKEN }}

  deliver-rpm:
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      ((needs.get-environment.outputs.delivery_type == 'feature' && contains(github.event.pull_request.labels.*.name, 'delivery-feature')) ||
      contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability)) &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    needs: [get-environment, build-and-test-agent-windows, robot-test]
    runs-on: centreon-ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - distrib: el8
            arch: amd64
          - distrib: el9
            arch: amd64
          - distrib: el10
            arch: amd64

    name: deliver ${{ matrix.distrib }}

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

      - name: Publish RPM packages
        uses: ./.github/actions/rpm-delivery
        with:
          module_name: monitoring-agent
          distrib: ${{ matrix.distrib }}
          version: ${{ needs.get-environment.outputs.major_version }}
          artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
          cache_key: ${{ github.run_id }}-${{ github.sha }}-rpm-centreon-monitoring-agent-${{ matrix.distrib }}-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}
          stability: ${{ needs.get-environment.outputs.stability }}
          release_type: ${{ needs.get-environment.outputs.release_type }}
          is_cloud: ${{ needs.get-environment.outputs.is_cloud }}
          delivery_type: ${{ needs.get-environment.outputs.delivery_type }}
          centreon_technique_pat: ${{ secrets.CENTREON_TECHNIQUE_PAT }}

  deliver-deb:
    if: |
      needs.changes.outputs.trigger_delivery == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    needs: [get-environment, changes, build-and-test-agent-windows, robot-test]
    runs-on: centreon-ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - distrib: bullseye
            arch: amd64
          - distrib: bullseye
            arch: arm64
          - distrib: bookworm
            arch: amd64
          - distrib: bookworm
            arch: arm64
          - distrib: trixie
            arch: amd64
          - distrib: trixie
            arch: arm64
          - distrib: jammy
            arch: amd64
          - distrib: jammy
            arch: arm64
          - distrib: noble
            arch: amd64
          - distrib: noble
            arch: arm64

    name: deliver ${{ matrix.distrib }} ${{ matrix.arch }}

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

      - name: Publish DEB packages
        uses: ./.github/actions/deb-delivery
        with:
          module_name: monitoring-agent
          distrib: ${{ matrix.distrib }}
          version: ${{ needs.get-environment.outputs.major_version }}
          artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
          cache_key: ${{ github.run_id }}-${{ github.sha }}-deb-centreon-monitoring-agent-${{ matrix.distrib }}-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}
          stability: ${{ needs.get-environment.outputs.stability }}
          release_type: ${{ needs.get-environment.outputs.release_type }}
          is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

  deliver-windows:
    if: |
      needs.changes.outputs.trigger_delivery == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    needs: [get-environment, changes, build-and-test-agent-windows, robot-test]
    runs-on: windows-latest

    steps:
      - uses: jfrog/setup-jfrog-cli@ff5cb544114ffc152db9cea1cd3d5978d5074946 # v4.5.11
        env:
          JF_URL: https://packages.centreon.com
          JF_ACCESS_TOKEN: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}

      - name: Restore cache monitoring agent
        uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
        with:
          path: agent\installer\centreon-monitoring-agent.exe
          key: ${{ github.run_id }}-${{ github.sha }}-exe-centreon-monitoring-agent-${{ github.head_ref || github.ref_name }}
          fail-on-cache-miss: true

      - name: Deliver
        run: |
          Write-Host "[DEBUG] deliver to testing - Major version: ${{ needs.get-environment.outputs.cma_major_version }}"
          Write-Host "[DEBUG] deliver to testing - Minor version: ${{ needs.get-environment.outputs.cma_minor_version }}"

          $VERSION = "${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}"
          $STABILITY = "${{ needs.get-environment.outputs.stability }}"

          $TARGET_PATH = "installers/monitoring-agent/${{ needs.get-environment.outputs.cma_major_version }}/$STABILITY/windows/"

          $VERSION_EXE = "centreon-monitoring-agent-${VERSION}.exe"

          Copy-Item -Path "agent\installer\centreon-monitoring-agent.exe" -Destination "${VERSION_EXE}"
          Write-Host "[DEBUG] deliver testing to - Target path: ${TARGET_PATH}"
          jf rt upload $VERSION_EXE "${TARGET_PATH}" --sync-deletes="${TARGET_PATH}"
        shell: powershell

  promote-linux:
    needs: [get-environment, deliver-rpm, deliver-deb]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      (contains(fromJson('["stable", "testing"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    runs-on: centreon-ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        distrib: [el8, el9, el10, bullseye, bookworm, trixie, jammy, noble]

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

      - name: Promote ${{ matrix.distrib }} to stable
        uses: ./.github/actions/promote-to-stable
        with:
          artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
          module_name: monitoring-agent
          distrib: ${{ matrix.distrib }}
          major_version: ${{ needs.get-environment.outputs.major_version }}
          minor_version: ${{ needs.get-environment.outputs.minor_version }}
          stability: ${{ needs.get-environment.outputs.stability }}
          github_ref_name: ${{ github.ref_name }}
          release_type: ${{ needs.get-environment.outputs.release_type }}
          is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

      - name: Parse distrib name
        id: parse-distrib
        uses: ./.github/actions/parse-distrib
        with:
          distrib: ${{ matrix.distrib }}

      - name: Deliver monitoring agent to github release
        if: ${{ needs.get-environment.outputs.is_cloud == 'false' }}
        uses: ./.github/actions/deliver-github-asset
        with:
          file_pattern: /tmp/promoted-packages/centreon-monitoring-agent${{ steps.parse-distrib.outputs.package_version_separator }}${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}*.${{ steps.parse-distrib.outputs.package_extension }}
          github_release: centreon-monitoring-agent-${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}
          token_download_centreon_com: ${{ secrets.TOKEN_DOWNLOAD_CENTREON_COM }}

  promote-windows:
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      (contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect'

    needs: [get-environment, deliver-windows]
    runs-on: windows-latest

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

      - uses: jfrog/setup-jfrog-cli@ff5cb544114ffc152db9cea1cd3d5978d5074946 # v4.5.11
        env:
          JF_URL: https://packages.centreon.com
          JF_ACCESS_TOKEN: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}

      - name: Promote testing to stable
        run: |
          Write-Host "[DEBUG] promote to stable - Major version: ${{ needs.get-environment.outputs.cma_major_version }}"
          Write-Host "[DEBUG] promote to stable - Minor version: ${{ needs.get-environment.outputs.cma_minor_version }}"

          $VERSION= "${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}"
          $MODULE_NAME= "monitoring-agent-${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}"
          $STABILITY= "${{ needs.get-environment.outputs.stability }}"

          $SRC_PATH = "installers/monitoring-agent/${{ needs.get-environment.outputs.major_version }}/testing/windows/"
          $TARGET_PATH = "installers/monitoring-agent/${{ needs.get-environment.outputs.major_version }}/$STABILITY/windows/"

          $VERSION_EXE = "centreon-monitoring-agent-${VERSION}.exe"

          Write-Host "[DEBUG] promote to stable from: ${SRC_PATH}${VERSION_EXE}"

          jf rt download "${SRC_PATH}${VERSION_EXE}" --flat --fail-no-op

          Write-Host "[DEBUG] promote to stable ${VERSION_EXE} to path: ${TARGET_PATH}"

          jf rt upload $VERSION_EXE "${TARGET_PATH}"
        shell: powershell

      - name: Deliver monitoring agent to github release
        if: ${{ needs.get-environment.outputs.is_cloud == 'false' }}
        uses: ./.github/actions/deliver-github-asset
        with:
          file_pattern: ./centreon-monitoring-agent-${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}*.exe
          github_release: centreon-monitoring-agent-${{ needs.get-environment.outputs.cma_major_version }}.${{ needs.get-environment.outputs.cma_minor_version }}
          token_download_centreon_com: ${{ secrets.TOKEN_DOWNLOAD_CENTREON_COM }}

  set-skip-label:
    needs:
      [
        get-environment,
        deliver-rpm,
        deliver-deb,
        deliver-windows,
        promote-linux,
        promote-windows,
      ]
    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
