name: common

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

on:
  workflow_dispatch:
    inputs:
      labels:
        description: "Labels to add to the run (comma separated)"
        required: false
        default: ""
        type: string
  pull_request:
    paths:
      - ".version"
      - "packaging/centreon-common/**"
      - "selinux/centreon-common/**"
  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"
      - "packaging/centreon-common/**"
      - "selinux/centreon-common/**"
    tags:
      - "centreon-common-*"

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: .version.centreon-common
      labels: ${{ inputs.labels || '' }}

  package:
    needs: [get-environment]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable'

    strategy:
      fail-fast: false
      matrix:
        include:
          - package_extension: rpm
            image: packaging-centreon-collect-alma8
            distrib: el8
          - package_extension: rpm
            image: packaging-centreon-collect-alma9
            distrib: el9
          - package_extension: deb
            image: packaging-centreon-collect-bookworm
            distrib: bookworm

    runs-on: ubuntu-24.04

    container:
      image: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ matrix.image }}:${{ needs.get-environment.outputs.packaging_img_version }}
      credentials:
        username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
        password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}

    name: package ${{ matrix.distrib }}

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

      - name: Generate selinux binaries
        if: ${{ matrix.package_extension == 'rpm' }}
        run: |
          cd selinux/centreon-common
          sed -i "s/@VERSION@/${{ needs.get-environment.outputs.major_version }}.${{ needs.get-environment.outputs.minor_version }}/g" *.te
          make -f /usr/share/selinux/devel/Makefile
        shell: bash

      - name: Remove selinux packaging files on debian
        if: ${{ matrix.package_extension == 'deb' }}
        run: rm -f packaging/centreon-common/*-selinux.yaml
        shell: bash

      - name: Package Centreon
        uses: ./.github/actions/package
        with:
          nfpm_file_pattern: "packaging/centreon-common/*.yaml"
          module_name: "centreon-common"
          distrib: ${{ matrix.distrib }}
          package_extension: ${{ matrix.package_extension }}
          major_version: ${{ needs.get-environment.outputs.major_version }}
          minor_version: ${{ needs.get-environment.outputs.minor_version }}
          release: ${{ needs.get-environment.outputs.release }}
          arch: all
          commit_hash: ${{ github.sha }}
          cache_key: ${{ github.sha }}-${{ github.run_id }}-${{ matrix.package_extension }}-${{ matrix.distrib }}
          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 }}
          stability: ${{ needs.get-environment.outputs.stability }}
          upload_artifacts: ${{ needs.get-environment.outputs.is_nightly == 'true' || contains(needs.get-environment.outputs.labels, 'upload-artifacts') && 'true' || 'false' }}

  deliver-rpm:
    runs-on: centreon-common
    needs: [get-environment, package]
    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')

    strategy:
      matrix:
        distrib: [el8, el9]

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

      - name: Delivery
        uses: ./.github/actions/rpm-delivery
        with:
          module_name: common
          distrib: ${{ matrix.distrib }}
          version: ${{ needs.get-environment.outputs.major_version }}
          artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
          cache_key: ${{ github.sha }}-${{ github.run_id }}-rpm-${{ matrix.distrib }}
          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:
    runs-on: centreon-common
    needs: [get-environment, package]
    if: |
      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') &&
      (needs.get-environment.outputs.is_cloud == 'false' || needs.get-environment.outputs.stability == 'unstable')

    strategy:
      matrix:
        distrib: [bookworm]

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

      - name: Delivery
        uses: ./.github/actions/deb-delivery
        with:
          module_name: common
          distrib: ${{ matrix.distrib }}
          version: ${{ needs.get-environment.outputs.major_version }}
          artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
          cache_key: ${{ github.sha }}-${{ github.run_id }}-deb-${{ matrix.distrib }}
          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-deb, deliver-rpm]
    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:
      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: common
          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
