name: Centreon Monitoring Agent Windows build and packaging

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

on:
  workflow_dispatch:
    inputs:
      installer_in_artifact:
        description: 'Save installer and binary in artifacts'
        required: true
        default: false
        type: boolean

  pull_request:
    paths:
      - agent/**
      - common/**
      - custom-triplets/**
      - CMakeLists.txt
      - CMakeListsWindows.txt
      - vcpkg.json
  push:
    branches:
      - develop
      - dev-[2-9][0-9].[0-9][0-9].x
      - master
      - "[2-9][0-9].[0-9][0-9].x"
    paths:
      - agent/**
      - common/**
      - custom-triplets/**
      - CMakeLists.txt
      - CMakeListsWindows.txt
      - vcpkg.json

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

  build-and-test-agent:
    needs: [get-environment]
    runs-on: windows-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.COLLECT_S3_SECRET_KEY }}

    steps:
      - uses: jfrog/setup-jfrog-cli@26da2259ee7690e63b5410d7451b2938d08ce1f9 # v4.0.0
        env:
          JF_URL: https://centreon.jfrog.io
          JF_ACCESS_TOKEN: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}

      # 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@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0

      - 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: |
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        uses: azure/trusted-signing-action@v0.4.0
        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_MODIFIER=On  -Bbuild_windows
          cmake --build build_windows --config Release

      - name: Sign modifier
        if: |
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        uses: azure/trusted-signing-action@v0.4.0
        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
        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  -Bbuild_windows
          cmake --build build_windows --config Release

      - name: Sign installer
        if: |
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        uses: azure/trusted-signing-action@v0.4.0
        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.exe
          file-digest: SHA256
          timestamp-rfc3161: http://timestamp.acs.microsoft.com
          timestamp-digest: SHA256

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

      - name: Upload package artifacts
        if: |
          inputs.installer_in_artifact == true ||
          (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@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
        with:
          name: packages-centreon-monitoring-agent-windows
          path: |
            agent\installer\centreon-monitoring-agent.exe
            build_windows\agent\Release\centagent.exe

      - name: Deliver
        if: |
          contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
          ! cancelled() &&
          ! contains(needs.*.result, 'failure') &&
          ! contains(needs.*.result, 'cancelled')
        run: |
          Write-Host "[DEBUG] deliver to testing - Major version: ${{ needs.get-environment.outputs.major_version }}"
          Write-Host "[DEBUG] deliver to testing - Minor version: ${{ needs.get-environment.outputs.minor_version }}"

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

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

          $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}"


      - name: Promote testing to stable
        if: |
          needs.get-environment.outputs.stability == 'stable' && github.event_name != 'workflow_dispatch' && ! cancelled()
        run: |
          Write-Host "[DEBUG] promote to stable - Major version: ${{ needs.get-environment.outputs.major_version }}"
          Write-Host "[DEBUG] promote to stable - Minor version: ${{ needs.get-environment.outputs.minor_version }}"

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

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

          $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

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

          jf rt upload $VERSION_EXE "${TARGET_PATH}" --sync-deletes="${TARGET_PATH}"

        shell: powershell
