name: copilot-build-backend

on:
  workflow_dispatch:
  pull_request:
    branches: ["main"]
    paths:
      - "webapi/**"
  workflow_call:
    outputs:
      artifact:
        description: "The name of the uploaded web api artifact."
        value: ${{jobs.webapi.outputs.artifact}}

permissions:
  contents: read

jobs:
  webapi:
    strategy:
      fail-fast: false
      matrix:
        include:
          - { dotnet: "8.0", configuration: Release, os: windows-latest }

    runs-on: ${{ matrix.os }}

    env:
      NUGET_CERT_REVOCATION_MODE: offline

    outputs:
      artifact: ${{steps.artifactoutput.outputs.artifactname}}

    steps:
      - uses: actions/checkout@v4
        with:
          clean: true
          fetch-depth: 0

      - name: Install GitVersion
        uses: gittools/actions/gitversion/setup@v2
        with:
          versionSpec: "5.x"

      - name: Determine version
        id: gitversion
        uses: gittools/actions/gitversion/execute@v2

      - name: Set version tag
        id: versiontag
        run: |
          $VERSION_TAG = "${{ steps.gitversion.outputs.Major }}."
          $VERSION_TAG += "${{ steps.gitversion.outputs.Minor }}."
          $VERSION_TAG += "${{ steps.gitversion.outputs.CommitsSinceVersionSource }}"
          echo $VERSION_TAG
          Write-Output "versiontag=$VERSION_TAG" >> $env:GITHUB_OUTPUT

      - name: Package Copilot Chat WebAPI
        run: |
          scripts\deploy\package-webapi.ps1 -Configuration Release -DotnetFramework net8.0 -TargetRuntime win-x64 -OutputDirectory ${{ github.workspace }}\scripts\deploy -Version ${{ steps.versiontag.outputs.versiontag }} -InformationalVersion "Built from commit ${{ steps.gitversion.outputs.ShortSha }} on $(Get-Date -Format 'yyyy-MM-dd')" -SkipFrontendFiles ('${{ github.event_name == 'pull_request' }}' -eq 'true')

      - name: Upload package to artifacts
        uses: actions/upload-artifact@v4
        with:
          name: copilotchat-webapi-${{ steps.versiontag.outputs.versiontag }}
          path: ${{ github.workspace }}\scripts\deploy\out\webapi.zip

      - name: "Set outputs"
        id: artifactoutput
        run: Write-Output "artifactname=copilotchat-webapi-${{ steps.versiontag.outputs.versiontag }}" >> $env:GITHUB_OUTPUT