# OpenMaui Linux Release - Publish to Package Registries
name: Release

on:
  push:
    tags:
      - 'v*'

env:
  DOTNET_NOLOGO: true
  DOTNET_CLI_TELEMETRY_OPTOUT: true

jobs:
  release:
    name: Build and Publish
    runs-on: linux-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Get version from tag
        id: version
        run: |
          TAG="${{ gitea.ref_name }}"
          VERSION="${TAG#v}"
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Building version: $VERSION"

      - name: Restore dependencies
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Run tests
        run: dotnet test --configuration Release --no-build --verbosity normal
        continue-on-error: true

      - name: Pack NuGet package
        run: dotnet pack --configuration Release --no-build -o ./nupkg -p:PackageVersion=${{ steps.version.outputs.VERSION }}

      - name: Upload NuGet packages
        uses: actions/upload-artifact@v3
        with:
          name: nuget-packages
          path: ./nupkg/*.nupkg

      - name: Publish to Gitea Packages
        run: |
          for pkg in ./nupkg/*.nupkg; do
            dotnet nuget push "$pkg" \
              --api-key ${{ secrets.GITEATOKEN }} \
              --source https://git.marketally.com/api/packages/${{ gitea.repository_owner }}/nuget/index.json \
              --skip-duplicate
          done

      - name: Publish to NuGet.org
        if: ${{ secrets.NUGET_API_KEY != '' }}
        run: |
          for pkg in ./nupkg/*.nupkg; do
            dotnet nuget push "$pkg" \
              --api-key ${{ secrets.NUGET_API_KEY }} \
              --source https://api.nuget.org/v3/index.json \
              --skip-duplicate
          done
        env:
          NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
