Publishing NuGet Packages with GitHub Actions
This is going to be a short post.
Many people asked me how I publish my NuGet packages into the packages section in GitHub.
First things first, what are GitHub Actions.
"Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized workflow." -GitHub Team
How to setup GitHub actions in your organization or personal repo.

On your repo, if you have enough privileges, you will see the actions tab; from there you can create a new workflow.

Since my main focus is .NET, I always choose Continuous Integration (CI) with .NET.
In turn this will create a YAML file .github/workflows folder in your repo.
You can have as many workflows you need for different purposes.
Now for the actual code (YAML) that does the compilation, this is the text I use for most of my projects. Of course you can argue this is simple, but that is the thing, I just need to compile, create the artifacts and publish.
name: Build and Deploy Nuget Packages env: DOTNET_VERSION: '8.0.x' on: push: branches: [ "main" ] workflow_dispatch: permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up .NET Core uses: actions/setup-dotnet@v2 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Set up dependency caching for faster builds uses: actions/cache@v3 with: path: ~/.nuget/packages key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget- - name: Build with dotnet run: dotnet build src/folderx/Solution.sln --configuration Release - name: NuGet push run: dotnet nuget push "**/*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --source https://nuget.pkg.github.com/organization/index.json --skip-duplicate - name: NTFY run: curl -X POST -d 'Finished compiling and publishing nuget packages for YOUR Services' -k https://ntfyserver.com/ntfy/mynotification
Let's dissect the actions.
name: Build and Deploy Nuget Packages env: DOTNET_VERSION: '8.0.x' on: push: branches: [ "main" ] workflow_dispatch: permissions: contents: read
- Name the workflow.
- Set the .NET version to use for compilation.
- Use the main branch for checkout.
- Only read only access.
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up .NET Core uses: actions/setup-dotnet@v2 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Set up dependency caching for faster builds uses: actions/cache@v3 with: path: ~/.nuget/packages key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
- Use an ubuntu worker to compile and publish
- Checkout the code and use the main branch.
- Setup .NET core SDK in the worker.
- Cache the NuGet packages that your solution is using while restoring (this speeds up your build time).
- name: Build with dotnet run: dotnet build src/folderx/Solution.sln --configuration Release - name: NuGet push run: dotnet nuget push "**/*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --source https://nuget.pkg.github.com/organization/index.json --skip-duplicate - name: NTFY run: curl -X POST -d 'Finished compiling and publishing nuget packages for YOUR Services' -k https://ntfyserver.com/ntfy/mynotification
- Build the solution.
- Push the NuGet packages to the packages in your organization (probably another post to explain this). The NuGet API key is set on the secrets section.
- Notify thru push notification that the publishing finished.
Thats it, simple.
Now wait a minute, NTFY, what is that???
It is an amazing tool I use to send push notifications to my phone; it is free and configurable. I will create another post on how to install and run NTFY.