name: ci

on:
  push:
    branches: [ main ]
  pull_request:
  merge_group:
  workflow_dispatch:

env:
  NUGET_PACKAGES: ${{ github.workspace }}/packages
  DOTNET_CLI_TELEMETRY_OPTOUT: 1

permissions:
  contents: read

jobs:
  build-container:
    uses: ./.github/workflows/build-container.yml

  build-ubuntu1604-native:
    uses: ./.github/workflows/build-ubuntu1604-native-container.yml

  build:
    needs: build-ubuntu1604-native
    uses: ./.github/workflows/build.yml

  build-nuget-packages:
    needs: [ build, build-container ]
    uses: ./.github/workflows/build-nuget-packages.yml

  test-build-managed:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        test-tfm: [ net9.0, net8.0, net462 ]
        machine: [ windows-2022, windows-2025, ubuntu-22.04, macos-13, ubuntu-22.04-arm ]
        exclude:
          - test-tfm: net462
            machine: macos-13
          - test-tfm: net462
            machine: ubuntu-22.04
          - test-tfm: net462
            machine: ubuntu-22.04-arm
        include:
          - machine: windows-2022
            containers: none
            artifact-name: windows-2022
          - machine: windows-2025
            containers: none
            artifact-name: windows-2022
          - machine: ubuntu-22.04
            containers: linux
            artifact-name: ubuntu-22.04
          - machine: macos-13
            containers: none
            artifact-name: macos-13
          - machine: ubuntu-22.04-arm
            containers: linux
            artifact-name: ubuntu-22.04-arm
    runs-on: ${{ matrix.machine }}
    steps:

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
        with:
          fetch-depth: 0 # fetching all, needed to correctly calculate version

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302

      - name: Check for NuGet packages cache
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # tag: v4.2.3
        id: nuget-cache
        with:
          key: ${{ hashFiles('**/Directory.packages.props', './build/LibraryVersions.g.cs', '**/packages.config' ) }}
          path: ${{ env.NUGET_PACKAGES }}

      - name: Download Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-${{ matrix.artifact-name }}
          path: bin/tracer-home

      - name: Install SQL Server (localdb)
        if: ${{ runner.os == 'Windows' }}
        uses: potatoqualitee/mssqlsuite@605af2310e2e22978ebf2c74d5995ba102094b8a # tag: v1.8
        with: 
          install: localdb

      - name: Run TestWorkflow
        run: ./build.cmd --skip NativeTests --target TestWorkflow --test-target-framework ${{ matrix.test-tfm }} --containers ${{ matrix.containers }}

      - name: Upload test logs
        if: always()
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # tag: v4.6.2
        with:
          name: test-logs-${{ matrix.machine }}-${{ matrix.test-tfm }}-containers-${{ matrix.containers }}
          path: test-artifacts/

      - name: Delete SQL Server MSI
        if: ${{ runner.os == 'Windows' }}
        shell: bash
        run: rm SqlLocalDB.msi

  test-build-native:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: windows-2022
            artifact-name: windows-2022
          - machine: windows-2025
            artifact-name: windows-2022
    runs-on: ${{ matrix.machine }}
    steps:

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
        with:
          fetch-depth: 0 # fetching all, needed to correctly calculate version

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302

      - name: Check for NuGet packages cache
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # tag: v4.2.3
        id: nuget-cache
        with:
          key: ${{ hashFiles('**/Directory.packages.props', './build/LibraryVersions.g.cs', '**/packages.config' ) }}
          path: ${{ env.NUGET_PACKAGES }}

      - name: Download Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-${{ matrix.artifact-name }}
          path: bin/tracer-home

      - name: Run NativeTests
        run: ./build.cmd NativeTests

  test-build-windows-container-tests:
    needs: build
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: windows-2022
            containers: windows-only
            artifact-name: windows-2022
          - machine: windows-2025
            containers: windows-only
            artifact-name: windows-2022
    runs-on: ${{ matrix.machine }}
    steps:

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
        with:
          fetch-depth: 0 # fetching all, needed to correctly calculate version

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302

      - name: Check for NuGet packages cache
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # tag: v4.2.3
        id: nuget-cache
        with:
          key: ${{ hashFiles('**/Directory.packages.props', './build/LibraryVersions.g.cs', '**/packages.config' ) }}
          path: ${{ env.NUGET_PACKAGES }}

      - name: Download Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-${{ matrix.artifact-name }}
          path: bin/tracer-home

      - name: Build artifacts required for the test (no native tests)
        run: ./build.cmd --skip RunManagedTests --target ManagedTests --containers ${{ matrix.containers }}

      - name: Run the integration tests
        run: ./build.cmd --target RunManagedIntegrationTests --containers ${{ matrix.containers }} --test-target-framework net462

      - name: Upload test logs
        if: always()
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # tag: v4.6.2
        with:
          name: test-logs-${{ matrix.machine }}-containers-${{ matrix.containers }}
          path: test-artifacts/

  test-build-container:
    needs: [build, build-container]
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: ubuntu-22.04
            base-image: alpine
            build-source: alpine-x64
            os-type: linux-musl
          - machine: ubuntu-22.04
            base-image: debian
            build-source: ubuntu-22.04
            os-type: linux-glibc
          - machine: ubuntu-22.04
            base-image: centos-stream9
            build-source: ubuntu-22.04
            os-type: linux-glibc
          - machine: ubuntu-22.04-arm
            base-image: alpine
            build-source: alpine-arm64
            os-type: linux-musl
          - machine: ubuntu-22.04-arm
            base-image: debian-arm64
            build-source: ubuntu-22.04-arm
            os-type: linux-glibc
    runs-on: ${{ matrix.machine }}
    timeout-minutes: 60
    steps:

    - name: Checkout
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
      with:
        fetch-depth: 0 # fetching all, needed to correctly calculate version

    - name: Download Artifacts from build job
      uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
      with:
        name: bin-${{ matrix.build-source }}
        path: bin/tracer-home

    - name: Build in Docker container
      run: |
        set -e
        docker build -t mybuildimage -f "./docker/${{ matrix.base-image }}.dockerfile" .
        docker run -e OS_TYPE=${{ matrix.os-type }} --rm --mount type=bind,source="${GITHUB_WORKSPACE}",target=/project mybuildimage \
          /bin/sh -c 'git config --global --add safe.directory /project && ./build.sh TestWorkflow --containers none'

  test-nuget-packages:
    needs: build-nuget-packages
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: ubuntu-22.04
          - machine: macos-13
          - machine: windows-2022
          - machine: windows-2025
          - machine: ubuntu-22.04-arm
    runs-on: ${{ matrix.machine }}
    steps:
      
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
        with:
          fetch-depth: 0 # fetching all, needed to correctly calculate version

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302

      - name: Check for NuGet packages cache
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # tag: v4.2.3
        id: nuget-cache
        with:
          key: ${{ hashFiles('**/Directory.packages.props', './build/LibraryVersions.g.cs', '**/packages.config' ) }}
          path: ${{ env.NUGET_PACKAGES }}

      - name: Download NuGet Artifacts from build-nuget-packages job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-nuget-packages
          path: bin/nuget-artifacts/

      - name: Test NuGet Packages
        run: ./build.cmd TestNuGetPackages

      - name: Upload test logs
        if: always()
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # tag: v4.6.2
        with:
          name: test-logs-nuget-packages-${{ matrix.machine }}
          path: test-artifacts/

  test-jobs:
    runs-on: ubuntu-22.04
    needs:
      - test-build-managed
      - test-build-native
      - test-build-container
      - test-nuget-packages
      - test-build-windows-container-tests
    steps:
      
      - name: Test if test jobs passed
        run: |
          echo ${{ needs.test-build-managed.result }}
          test ${{ needs.test-build-managed.result }} == "success"
          echo ${{ needs.test-build-native.result }}
          test ${{ needs.test-build-native.result }} == "success"
          echo ${{ needs.test-build-container.result }}
          test ${{ needs.test-build-container.result }} == "success"
          echo ${{ needs.test-nuget-packages.result }}
          test ${{ needs.test-nuget-packages.result }} == "success"
          echo ${{ needs.test-build-windows-container-tests.result }}
          test ${{ needs.test-build-windows-container-tests.result }} == "success"
