Use GitHub actions to publish NuGet packages
Tags: dotnet, devops, opensource, github
Published: 2020-06-08 21:21:17

---
title: Use GitHub actions to publish NuGet packages
published: true
date: 2020-06-06 12:47:14 UTC
tags: dotnet,devops,opensource,github
canonical_url: https://lukelowrey.com/use-github-actions-to-publish-nuget-packages/
---

 ![Use GitHub actions to publish NuGet packages](https://images.unsplash.com/photo-1590595906931-81f04f0ccebb?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

*Originally posted on [lukelowrey.com](https://lukelowrey.com/use-github-actions-to-publish-nuget-packages/).*

One of the most frustrating aspects of maintaining an open source .NET Core library has been publishing the nuget packages on [nuget.org](https://www.nuget.org/). In the world of devops automation, manually creating and uploading packages felt so old-fashioned (don't get me started on Azure Devops).

Here is how I automated pushing packages to nuget.org for [FluentEmail](https://github.com/lukencode/FluentEmail/).

1. Create a Github Action that runs on push to the master branch
2. Use [Publish Nuget](https://github.com/marketplace/actions/publish-nuget) action to package and publish nuget packages
3. Create a Nuget.org API key and set it as a GitHub secret
4. Nuget packages are published on push to master!

### Create a GitHub Action

[GitHub actions](https://github.com/features/actions) makes it easy to automatically build test and deploy code hosted on GitHub. There are heaps of community built actions that cover the whole #devops spectrum.

Creating a new workflow creates a yaml file in .github/workflows for the repository. Infrastructure as code! The best way to start is create a new workflow using the .NET Core starter action.

![Use GitHub actions to publish NuGet packages](https://lukelowrey.com/content/images/2020/06/image-2.png)

This action sets up a simple dotnet environment. It is a great starting point for running validation builds and tests on new pull requests. For publishing packages running the action on push to the master branch works best.

Here is the full yaml for the workflow I am using. It uses actions to setup .Net Core, restore packages, build and publish nuget.

<!--kg-card-begin: markdown-->
```
name: Publish Packages

on:
  push:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Publish FluentEmail.Core
      uses: brandedoutcast/publish-nuget@v2.5.2
      with:
          PROJECT_FILE_PATH: src/FluentEmail.Core/FluentEmail.Core.csproj
          NUGET_KEY: ${{secrets.NUGET_API_KEY}}

```
<!--kg-card-end: markdown-->
### Publish nuget package action

[brandedoutcast](https://github.com/brandedoutcast)has created a reusable GitHub Action to [Publish Nuget](https://github.com/marketplace/actions/publish-nuget) packages. You can see it referenced in the _"uses:"_ section in the workflow above. This action does the heavy lifting for packaging and publishing nuget packages. whenever the project version is updated. The action (by default) looks for changes to the _<version>1.0</version>_ tag in your csproj file.

There are a couple of variables you can set to get the process working just right. For a basic setup you only really need to set _PROJECT\_FILE\_PATH_ and _NUGET\_KEY_.

The full publish-nuget variable list allows you to tweak the versioning method and format as well as target package sources other than nuget.org.

<!--kg-card-begin: markdown-->
```
# Filepath of the project to be packaged, relative to root of repository
PROJECT_FILE_PATH: YourProject/YourProject.csproj
          
# NuGet package id, used for version detection & defaults to project name
# PACKAGE_NAME: YourProject

# API key to authenticate with NuGet server
NUGET_KEY: ${{secrets.NUGET_API_KEY}}

# NuGet server uri hosting the packages, defaults to https://api.nuget.org
# NUGET_SOURCE: https://api.nuget.org
          
# Filepath with version info, relative to root of repository & defaults to PROJECT_FILE_PATH
# VERSION_FILE_PATH: Directory.Build.props

# Regex pattern to extract version info in a capturing group
# VERSION_REGEX: <Version>(.*)<\/Version>
          
# Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
# VERSION_STATIC: 1.0.0

# Flag to toggle git tagging, enabled by default
# TAG_COMMIT: true

# Format of the git tag, [*] gets replaced with actual version
# TAG_FORMAT: v*

# Flag to toggle pushing symbols along with nuget package to the server, disabled by default
# INCLUDE_SYMBOLS: false
```
<!--kg-card-end: markdown-->
### 

### Create Nuget.org API key as GitHub Action Secret

The workflow above uses the nuget API to automatically push the package. You need to create an API key for your profile on nuget.org.

![Use GitHub actions to publish NuGet packages](https://lukelowrey.com/content/images/2020/06/image-3.png)<figcaption>Create API Keys from your nuget.org profile</figcaption>

You can then enter the key as a[secret in your GitHub repository](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets). The key for the secret must be _NUGET\_API\_KEY_. The API key will then be available to the publish package action workflow.

![Use GitHub actions to publish NuGet packages](https://lukelowrey.com/content/images/2020/06/image-4.png)<figcaption>Setting a GitHub secret</figcaption>

### Results

Now when code is push to the master branch (and the project version changes) the publish packages action run and publishes the nuget package!

![Use GitHub actions to publish NuGet packages](https://lukelowrey.com/content/images/2020/06/image-5.png)<figcaption>Success!</figcaption>

A more advanced branching and action setup would allow you to publish pre-release versions from a beta branch and full release versions from master.

* * *

_If you found this useful, retweet or like the thread on twitter._

> How I use GitHub actions to automatically publish [@nuget](https://twitter.com/nuget?ref_src=twsrc%5Etfw) packages for a .NET core library[https://t.co/xOD5IqklwC](https://t.co/xOD5IqklwC)
> 
> — Luke Lowrey (@lukencode) [June 8, 2020](https://twitter.com/lukencode/status/1270101881885388801?ref_src=twsrc%5Etfw)

<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>