Publishing NuGet package to GitHub Packages Using GitHub Actions

616 views Asked by At

I'm trying to create a NuGet package of my .NET library and publish it to GitHub Packages NuGet registry using GitHub actions.

I'm stuck at the publish part. I'm using the following command:

dotnet nuget push "bin/Release/MyApp.${{ steps.get-version.outputs.version }}.nupkg" source --username MY_GITHUB_USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/MY_COMPANY_GITHUB_ACCOUNT/index.json"

The error I'm getting is:

Unrecognized option '--username'

As you may have noticed, I'm getting the version number I set in .csproj file in order to figure out the nuget file name.

What command do I need to issue to push my NuGet package into my private GitHub Packages registry after building it?

Here's the full release.yml file:

# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: Publish MyApp NuGet to GitHub Packages

on:
  pull_request:
    branches: [ "master" ]

jobs:
  build-pack-n-ship:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Create NuGet package
      run: dotnet pack --configuration Release
    - name: Get version
      uses: kzrnm/get-net-sdk-project-versions-action@v1
      id: get-version
      with:
        proj-path: MyApp/MyApp.csproj
    - name: Add GitHub Packages as Source
      run: dotnet nuget add source --username MY_GITHUB_USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/MY_GITHUB_COMPANY_ACCOUNT/index.json"
    - name: Publish to GitHub Packages
      run: dotnet nuget push "bin/Release/MyApp.${{ steps.get-version.outputs.version }}.nupkg" --source github
2

There are 2 answers

2
pregmatch On

I usually just add source and then when push I just set --source.

- name: Add nuget source
  run: dotnet nuget add source --username YOURSERNAME --password ${{ secrets.NUGET_API_KEY }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/YOURSERNAME/index.json"
- name: Publish MyApp
  run: |
      dotnet pack -p:Version=$GITVERSION_SEMVER --configuration Release MyApp.csproj
      dotnet nuget push MyApp/bin/Release/MyApp.*.nupkg --source "github"
0
Pieterjan On

Here's the workflow I use:

name: publish-release

on:
  push:
    branches:
      - 'release/**'

jobs:
  build:

    name: publish-release
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2
      
    - name: Setup .NET Core
      uses: actions/[email protected]
      with:
        dotnet-version: 6.0.301
        # Authenticates packages to push to GPR
        source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
      env:
        NUGET_AUTH_TOKEN: '%NUGET_AUTH_TOKEN%'
    
    - name: Install dependencies
      run: dotnet restore
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}
        
    - name: Build
      run: dotnet build --configuration Release --no-restore
      
    - name: Test
      run: dotnet test --no-restore --verbosity normal
      
    - name: Pack
      run: dotnet pack --no-build --configuration Release
      
    - name: PushNuget
      run: dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate
      
    - name: PushGithub
      run: dotnet nuget push **/*.nupkg --no-symbols --skip-duplicate
      env:
        NUGET_AUTH_TOKEN: ${{ github.token }}