name: release-publish

on:
  release:
    types: [published]

permissions:
  contents: read

jobs:
  powershell-script:
    runs-on: windows-2022
    steps:

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302
            
      - name: Test the PowerShell module instructions from README.md
        shell: powershell
        run: |
          mkdir testapp
          cd testapp
          dotnet new console
          dotnet publish -f net9.0 -c Release
          $module_url = "https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/OpenTelemetry.DotNet.Auto.psm1"
          $dl_path = Join-Path $env:temp "OpenTelemetry.DotNet.Auto.psm1"
          $log_path = "C:\ProgramData\OpenTelemetry .NET AutoInstrumentation\logs\*"
          Invoke-WebRequest -Uri $module_url -OutFile $dl_path
          Import-Module $dl_path
          Install-OpenTelemetryCore
          $install_dir = Get-OpenTelemetryInstallDirectory
          Register-OpenTelemetryForCurrentSession -OTelServiceName "MyServiceDisplayName"
          ./bin/Release/net9.0/publish/testapp
          if (-not $?) { throw "dotnet help returned exit code: $LASTEXITCODE" }
          if (-not (Test-Path $log_path)) { throw "Log file does not exist. Instrumentation test failed." }
          Remove-Item $log_path
          Unregister-OpenTelemetryForCurrentSession
          ./bin/Release/net9.0/publish/testapp
          if (-not $?) { throw "dotnet help returned exit code: $LASTEXITCODE" }
          if (Test-Path $log_path) { throw "Log file exists. Instrumentation unregister failed." }
          Uninstall-OpenTelemetryCore
          if (Test-Path $install_dir) { throw "Core files exist. Core uninstall failed." }

  shell-scripts:
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: windows-2022
            log-dir: "/c/ProgramData/OpenTelemetry .NET AutoInstrumentation/logs"
          - machine: windows-2025
            log-dir: "/c/ProgramData/OpenTelemetry .NET AutoInstrumentation/logs"
          - machine: ubuntu-22.04
            log-dir: "/var/log/opentelemetry/dotnet"
          - machine: macos-13
            log-dir: "/var/log/opentelemetry/dotnet"
          - machine: ubuntu-22.04-arm
            log-dir: "/var/log/opentelemetry/dotnet"
    runs-on: ${{ matrix.machine }}
    steps:

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

      - name: Install MacOS CoreUtils
        if: ${{ runner.os == 'macOS' }}
        run: brew install coreutils

      - name: Create test directory
        if: ${{ runner.os != 'Windows' }}
        run: sudo mkdir -p ${{ matrix.log-dir }} && sudo chmod a+rwx ${{ matrix.log-dir }}  # the instrumented process has no permissions to create log dir

      - name: Test the Shell scripts from README.md
        shell: bash
        run: |
          set -e
          mkdir testapp
          cd testapp
          dotnet new console
          dotnet publish -f net9.0 -c Release
          curl -sSfL https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/otel-dotnet-auto-install.sh -O
          sh ./otel-dotnet-auto-install.sh
            test "$(ls -A "$HOME/.otel-dotnet-auto")"
          curl -sSfL https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/instrument.sh -O
          . ./instrument.sh
          ./bin/Release/net9.0/publish/testapp
            test "$(ls -A '${{ matrix.log-dir }}' )"

  shell-scripts-container:
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: ubuntu-22.04
            base-image: alpine
            net-version: net9.0
          - machine: ubuntu-22.04-arm
            base-image: alpine
            net-version: net9.0
    runs-on: ${{ matrix.machine }}
    steps:

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2

      - name: Test the Shell scripts from README.md in Docker container
        run: |
          set -e
          docker build -t mybuildimage -f "./docker/${{ matrix.base-image }}.dockerfile" .
          docker run --rm mybuildimage /bin/sh -c '
            set -e
            mkdir testapp
            cd testapp
            dotnet new console
            dotnet publish -f ${{ matrix.net-version }} -c Release
            curl -sSfL https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/otel-dotnet-auto-install.sh -O
            sh ./otel-dotnet-auto-install.sh
              test "$(ls -A "$HOME/.otel-dotnet-auto")"
            curl -sSfL https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/instrument.sh -O
            . ./instrument.sh
            ./bin/Release/${{ matrix.net-version }}/publish/testapp
              test "$(ls -A /var/log/opentelemetry/dotnet )"
          '
