Creating a Dotnet Core NuGet Package
Rich Minchuk -
My name is Rich Minchuk. I’m developing an open source project called GHK. It’s based around a few simple concepts:
- Develop a tool that bloggers can use, right out of the box, to grow their personal brand.
- Use brand new tech to show off what I’ve learned while building it.
- Make it unbelievably simple to install.
- Rely on mostly free tools to bring exceptional value to users.
I’m opting for a streamlined approach to this blog and future posts, so let’s dive right in.
Set up a GitHub account. If you don’t know Git, I’m going to point you to my read me markdown file which contains a number of commands that may help get you started.
Once you have a GitHub account, you will need to create a repository in which your open source project can live. Once your repo is set up and you can push commits to it, we can move on.
Next, let's get set up in dotnet core if you aren’t already. I’m on a Mac so the commands and steps might be a little different depending on your OS flavor. These steps were written and tested using .net core version 1.4 (IE: after they went back to .csproj files):
Install Visual Studio Code and its C# extension.
(MacOS only) Install Homebrew from VSCode's terminal. Brew is a Package Manager for MacOS that can install neat and necessary things.
(MacOS only) Go to Microsoft for explicit additional brew commands for set up (.net core 1.x). Dotnet Core 1.x requires OpenSSL to work on MacOS. You can pretty much forget about Brew now.
Navigate to your project folder:
Make a directory for the NuGet package source of your project, i.e.
Open the folder we just created (src/) in VSCode.
File > Open Folder
Control + `to open the terminal.
Exec the following in your VSCode terminal:
dotnet new classlib --name [yourUniqueClassLibName]
Great. You've created a blank class library, and it’s definitely something someone else could totally use. Let’s
git commit -m "my class library initial commit"
Pack and Push
Onto the meaty part: getting your commands right for packing your NuGet package and pushing it up to nuget.org.
cd into your projects src/ directory
cd src/ dotnet restore dotnet build -c release dotnet pack -c release ls bin/release/
Neat. Ya packed it. Dotnet makes this all so very simple. NuGet is obviously a big part of the backbone to Dotnet Core so they probably wanted to make it this easy. Next, go to nuget.org and create an account, which is actually a Microsoft account. Once you’re all logged in, go to your profile, scroll to the bottom, and find the API Keys section. Click the button to generate a new API key. This is where it’ll ask you to confirm your account. Once you've confirmed it, then get back to the same screen, and click to generate a new API key.
- It will prompt you to add a descriptive name for the key
the key I use for my awesome new NuGet project
- Check both
Glob patternfield, 'cause hey, let’s be safe.
- Click the icon to copy the key. This is your last chance to do this, so don't close the window yet.
Ok, now we have the key we need to push your NuGet package to nuget.org. Open the terminal in vscode.
cd ../ then
pwd to confirm you’re in your projects root directory. Then execute
Open up the file we just created in vscode, paste your key into it, and hit save. Go back to your terminal and execute:
Open your .gitignore file and add the line
git add *; git add .gitignore; git commit -m "don’t show anyone my key not never!"
Cool. I think we’re ready for the last and best steps. Navigate to your release bin folder in your terminal,
bsh cd src/bin/Release then execute
dotnet nuget push *.nupkg --api-key [guid] --source "https://www.nuget.org/api/v2/package"
BAM! You should be done. You created some code that other people can
dotnet add package [your package name]
Super simple. Now you’re ready to change the way people code with an awesome new open source project!