name: .NET Build and Publish

on:
  push:
    branches: ["main", "installer-test/**"]
    tags: ["v3.*.*"] # Manual delivery on every 3.x tag

jobs:
  build-connectors:
    runs-on: windows-latest # Keeping on windows for now, for cross platform building of exe projects, we need to use dotnet publish
    env:
      SEMVER: "unset"
      FILE_VERSION: "unset"
    outputs:
      semver: ${{ steps.set-version.outputs.semver }}
      file_version: ${{ steps.set-version.outputs.file_version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup .NET
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: 8.0.4xx # Align with global.json (including roll forward rules)

      - name: Cache Nuget
        uses: actions/cache@v5
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}

      - name: ⚒️ Run build and zip connectors
        run: ./build.ps1 zip

      - name: ⬆️ Upload artifacts
        uses: actions/upload-artifact@v7
        with:
          name: output-${{ env.SEMVER }}
          path: output/*.*
          if-no-files-found: error
          retention-days: 1
          compression-level: 0 # no compression

      - id: set-version
        name: Set version to output
        run: |
          echo "semver=${{ env.SEMVER }}" >> "$Env:GITHUB_OUTPUT"
          echo "file_version=${{ env.FILE_VERSION }}" >> "$Env:GITHUB_OUTPUT"

  deploy-installers:
    runs-on: ubuntu-latest
    needs: build-connectors
    env:
      IS_PUBLIC_RELEASE: ${{ github.ref_type == 'tag' }}
    steps:
      - name: 🔫 Trigger Build Installers
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: Build Installers
          repo: specklesystems/connector-installers
          token: ${{ secrets.CONNECTORS_GH_TOKEN }}
          inputs: '{
            "run_id": "${{ github.run_id }}",
            "semver": "${{ needs.build-connectors.outputs.semver }}",
            "file_version": "${{ needs.build-connectors.outputs.file_version }}",
            "repo": "${{ github.repository }}",
            "is_public_release": ${{ env.IS_PUBLIC_RELEASE }}
            }'
          ref: main
          wait-for-completion: true
          sync-status: true
        timeout-minutes: 15

      # Allows us to inspect the artifacts of failed builds, since this below step will be skipped if the above step fails
      - uses: geekyeggo/delete-artifact@v6
        with:
          name: output-*
