name: Nuget

on:
  release:
    types: [ released ]
    
jobs:
  nuget:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v5
      with:
        fetch-depth: 0

    - name: Setup dotnet
      uses: actions/setup-dotnet@v5
      with:
        dotnet-version: |
          8.0.x
          9.0.x

    - name: dotnet restore
      run: dotnet restore --verbosity minimal --configfile nuget.config

    - name: dotnet test
      run: dotnet test

    - name: dotnet pack
      run: dotnet pack -c Release src/nuget.proj -o pkg --include-symbols

    - name: dotnet nuget push
      run: |
        dotnet nuget push pkg\*.nupkg -s https://nuget.pkg.github.com/$env:GITHUB_REPOSITORY_OWNER -k ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
        dotnet nuget push pkg\*.nupkg -s https://www.nuget.org/ -k ${{ secrets.nuget_api_key }} --skip-duplicate
        
        
  ## Remove old versions of NuGet packages form github NuGet feed
  nuget-delete-old-packages:
    name: "Delete Old NuGet"
    needs: [nuget]
    strategy:
      matrix:
        nuget-package: 
        - "KubernetesClient"
        - "KubernetesClient.Classic"
    runs-on: ubuntu-latest
    permissions:
      packages: write
    
    steps:
    - name: Delete old NuGet packages
      uses: actions/delete-package-versions@v5
      with:
        owner: ${{ env.GITHUB_REPOSITORY_OWNER }}
        token: ${{ secrets.GITHUB_TOKEN }}
        package-name: ${{ matrix.nuget-package }}
        package-type: nuget
        min-versions-to-keep: 10
