name: collect
run-name: |
  ${{
    (github.event_name == 'schedule' ||
     (github.event_name == 'workflow_dispatch' &&
      (github.event.inputs.nightly_manual_trigger == 'true' || github.event.inputs.robot_pattern != '^((?!cma).)*$')
     )
    )
    && format('collect nightly {0}', github.ref_name)
    || ''
  }}

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

on:
  workflow_dispatch:
    inputs:
      nightly_manual_trigger:
        description: "Set to true for nightly run"
        required: true
        default: false
        type: boolean
      robot_pattern:
        description: "filter robot files with regex"
        required: true
        default: "^((?!cma).)*$"
        type: string
      labels:
        description: "Labels to add to the run (comma separated)"
        required: false
        default: ""
        type: string

  schedule:
    - cron: "30 0 * * 1-5"
  pull_request:
    paths:
      - ".github/workflows/collect.yml"
      - ".version"
      - ".version.centreon-collect"
      - bbdo/**
      - broker/**
      - ccc/**
      - clib/**
      - connectors/**
      - custom-triplets/**
      - engine/**
      - grpc/**
      - packaging/centreon-collect/**
      - cmake.sh
      - cmake-vcpkg.sh
      - CMakeLists.txt
      - CMakeListsLinux.txt
      - vcpkg.json
      - overlays/**
      - selinux/centreon-broker/**
      - selinux/centreon-engine/**
      - tests/**
  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-collect"
      - bbdo/**
      - broker/**
      - ccc/**
      - clib/**
      - connectors/**
      - custom-triplets/**
      - engine/**
      - grpc/**
      - packaging/centreon-collect/**
      - cmake.sh
      - cmake-vcpkg.sh
      - CMakeLists.txt
      - CMakeListsLinux.txt
      - vcpkg.json
      - overlays/**
      - selinux/centreon-broker/**
      - selinux/centreon-engine/**
      - tests/**
    tags:
      - "centreon-collect-*"

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 || inputs.robot_pattern != '^((?!cma).)*$' || 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.major_version }}.${{ needs.get-environment.outputs.minor_version }}
      version_file: .version.centreon-collect
      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-collect"
        "bbdo/**"
        "broker/**"
        "ccc/**"
        "clib/**"
        "connectors/**"
        "custom-triplets/**"
        "engine/**"
        "grpc/**"
        "packaging/centreon-collect/**"
        "cmake.sh"
        "cmake-vcpkg.sh"
        "CMakeLists.txt"
        "CMakeListsLinux.txt"
        "vcpkg.json"
        "overlays/**"
        "selinux/centreon-broker/**"
        "selinux/centreon-engine/**"
        "!**/test/**"
        "!**/tests/**"
        "!agent/test/**"
      unit_test_paths: |
        "**/test/**"
        "**/tests/**"
        "!tests/**"
        "!gorgone/tests/**"
      e2e_test_paths: |
        "resources/**"
        "tests/**"
        "!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 }}

  unit-test:
    needs: [get-environment, changes]
    if: |
      needs.changes.outputs.trigger_unit_testing == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable'
    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=OFF \
                  -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 unit tests
        run: |
          for test in common broker engine clib connector; do
            echo "::group::Running unit tests for $test"
            .github/scripts/collect-ut.sh $test
            echo "::endgroup::"
          done
        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 }}-${{ matrix.arch }}-reports
          path: "build/ut_*"
          retention-days: 1

  package:
    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.collect_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: false
    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 }}

  dockerize:
    needs: [get-environment, changes, package]
    if: |
      needs.changes.outputs.trigger_packaging == 'true' &&
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      (
        needs.get-environment.outputs.is_nightly == 'true' ||
        contains(needs.get-environment.outputs.labels, 'build-docker-poller')
      ) &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    runs-on: ubuntu-24.04
    env:
      project: centreon-poller
    strategy:
      fail-fast: false
      matrix:
        distrib: [alma9]

    name: dockerize centreon-poller-${{ matrix.distrib }}

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

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

      - name: Restore packages
        id: restore-packages
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: ./*.rpm
          key: ${{ github.run_id }}-${{ github.sha }}-rpm-centreon-collect-${{ steps.parse-distrib.outputs.repo_distrib_name }}-amd64-${{ github.head_ref || github.ref_name }}

      - name: Prepare packages directory
        if: steps.restore-packages.outputs.cache-hit == 'true'
        run: |
          ls -lh *.rpm
          mkdir packages-centreon
          mv centreon-{clib,engine,engine-daemon,engine-opentelemetry,broker,broker-core}-${{ needs.get-environment.outputs.major_version }}*.rpm packages-centreon/
          ls -lh packages-centreon/
        shell: bash

      - name: Login to registry
        if: steps.restore-packages.outputs.cache-hit == 'true'
        uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.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
        if: steps.restore-packages.outputs.cache-hit == 'true'

      - name: Build and push web image
        if: steps.restore-packages.outputs.cache-hit == 'true'
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
        env:
          DOCKER_BUILD_CHECKS_ANNOTATIONS: false
          DOCKER_BUILD_SUMMARY: false
          DOCKER_BUILD_RECORD_UPLOAD: false
        with:
          file: .github/docker/${{ env.project }}/${{ matrix.distrib }}/Dockerfile
          context: .
          build-args: |
            "VERSION=${{ needs.get-environment.outputs.major_version }}"
            "IS_CLOUD=${{ needs.get-environment.outputs.is_cloud }}"
            "STABILITY=${{ needs.get-environment.outputs.stability }}"
          pull: true
          push: true
          load: true
          tags: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ env.project }}-${{ matrix.distrib }}:${{ github.head_ref || github.ref_name }}
          secrets: |
            "ARTIFACTORY_INTERNAL_REPO_USERNAME=${{ secrets.ARTIFACTORY_INTERNAL_REPO_USERNAME }}"
            "ARTIFACTORY_INTERNAL_REPO_PASSWORD=${{ secrets.ARTIFACTORY_INTERNAL_REPO_PASSWORD }}"

  robot-test:
    needs: [get-environment, changes, package]
    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.collect_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: ${{ inputs.robot_pattern }}
    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-sources:
    runs-on: centreon-common
    needs: [get-environment, package]
    if: |
      github.event_name != 'workflow_dispatch' &&
      needs.get-environment.outputs.stability == 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled') &&
      github.repository == 'centreon/centreon-collect' &&
      needs.get-environment.outputs.is_cloud == 'false'

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

      - name: Deliver sources
        uses: ./centreon-collect/.github/actions/release-sources
        with:
          bucket_directory: centreon-collect
          module_directory: centreon-collect
          module_name: centreon-collect
          major_version: ${{ needs.get-environment.outputs.major_version }}
          minor_version: ${{ needs.get-environment.outputs.minor_version }}
          token_download_centreon_com: ${{ secrets.TOKEN_DOWNLOAD_CENTREON_COM }}

  deliver-rpm:
    if: |
      needs.changes.outputs.trigger_delivery == 'true' &&
      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, changes, robot-test]
    runs-on: centreon-common
    strategy:
      fail-fast: false
      matrix:
        include:
          - distrib: el8
            arch: amd64
          - distrib: el9
            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: collect
          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-collect-${{ 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.outputs.is_cloud == 'false' || needs.get-environment.outputs.stability == 'unstable')

    needs: [get-environment, changes, robot-test]
    runs-on: centreon-common
    strategy:
      fail-fast: false
      matrix:
        include:
          - distrib: bookworm
            arch: amd64
          - distrib: bookworm
            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: collect
          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-collect-${{ 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 }}

  promote:
    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-common
    strategy:
      fail-fast: false
      matrix:
        distrib: [el8, el9, bookworm]

    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: collect
          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 }}

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

  clear-branch-cache:
    runs-on: ubuntu-24.04
    needs: [get-environment, deliver-deb, deliver-rpm]
    env:
      GH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
    if: |
      needs.get-environment.outputs.is_nightly == 'true' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    steps:
      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: List and delete cache
        run: |
          mapfile -t cache_keys < <(gh cache list --ref refs/heads/${{ github.ref_name }} --json key -q '.[] | .key')
          echo $cache_keys
          while read row; do
            gh cache delete $row
            echo "Cache $row was deleted."
          done <<< "$cache_keys"
        shell: bash

  create-jira-nightly-ticket:
    runs-on: ubuntu-24.04
    needs: [get-environment, deliver-rpm, deliver-deb]
    if: |
      needs.get-environment.outputs.is_nightly == 'true' && github.run_attempt == 1 &&
      startsWith(github.ref_name, 'dev') &&
      (failure() || cancelled() || contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) &&
      github.repository == 'centreon/centreon-collect'
    steps:
      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Create Jira ticket based on nightly build failure
        uses: ./.github/actions/create-jira-ticket
        with:
          jira_base_url: ${{ secrets.JIRA_BASE_URL }}
          jira_user_email: ${{ secrets.XRAY_JIRA_USER_EMAIL }}
          jira_api_token: ${{ secrets.XRAY_JIRA_TOKEN }}
          module_name: "centreon-collect"
          ticket_reason_for_creation: "Nightly"
