name: build-and-test-windows
on:
  push:
    branches:
      - 'main'
      - 'releases/**'
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'
  pull_request:
    types: [opened, synchronize, reopened, labeled, unlabeled]
    branches:
      - main
env:
  TEST_RESULTS: testbed/tests/results/junit/results.xml
  # Make sure to exit early if cache segment download times out after 2 minutes.
  # We limit cache download as a whole to 5 minutes.
  SEGMENT_DOWNLOAD_TIMEOUT_MINS: 2

# Do not cancel this workflow on main
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  windows-unittest-matrix:
    strategy:
      matrix:
        group:
          - receiver-0
          - receiver-1
          - processor
          - exporter
          - extension
          - internal
          - pkg
          - cmd
          - other
    runs-on: windows-latest
    if: ${{ github.actor != 'dependabot[bot]' && (contains(github.event.pull_request.labels.*.name, 'Run Windows') || github.event_name == 'push') }}
    env:
      # Limit memory usage via GC environment variables to avoid OOM on GH runners, especially for `cmd/otelcontribcol`,
      # see https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/28682#issuecomment-1802296776
      GOGC: 50
      GOMEMLIMIT: 2GiB
    steps:
      - uses: actions/checkout@v4
      - if: matrix.group == 'receiver-0'
        name: install IIS
        run: Install-WindowsFeature -name Web-Server -IncludeManagementTools
      - uses: actions/setup-go@v5
        with:
          go-version: ~1.20.12
          cache: false
      - name: Cache Go
        id: go-mod-cache
        timeout-minutes: 25
        uses: actions/cache@v3
        with:
          path: |
            ~\go\pkg\mod
            ~\AppData\Local\go-build
          key: go-build-cache-${{ runner.os }}-${{ matrix.group }}-go-${{ hashFiles('**/go.sum') }}
      - if: matrix.group == 'cmd'
        name: Increasing GOTEST_TIMEOUT for group 'cmd'
        run: echo "GOTEST_TIMEOUT=1200s" >> $Env:GITHUB_ENV
      - name: Run Unit tests
        run: make -j2 gotest GROUP=${{ matrix.group }}
  windows-unittest:
    if: ${{ github.actor != 'dependabot[bot]' && (contains(github.event.pull_request.labels.*.name, 'Run Windows') || github.event_name == 'push') }}
    runs-on: windows-latest
    needs: [windows-unittest-matrix]
    steps:
      - name: Print result
        run: echo ${{ needs.windows-unittest-matrix.result }}
      - name: Interpret result
        shell: bash
        run: |
          if [[ success == ${{ needs.windows-unittest-matrix.result }} ]]
          then
            echo "All matrix jobs passed!"
          else
            echo "One or more matrix jobs failed."
            false
          fi