The ability to automate software deployment is a key part of an healthy development process. In this post we will cover how to ship our .NET Core packages from the source hosted in GitHub into NuGet.org by using Travis CI.
I’m already assuming a basic setup of Travis using your GitHub account. If that’s not the case, please read here first.
You will also need…
- A NuGet.org account
- A NuGet API Key that you can generate here if you don’t have one already
- A code repository with your .NET Core source on GitHub
The Travis blueprint file
The Travis blueprint file is a
.travis.yml file in the root of your project’s repository. This will instruct Travis on how to build and deploy your artifacts.
Let’s say we have a nice .NET Standard library, SampleNetLib, that we want to make available on NuGet.org.
The above blueprint will tell Travis the following:
- C# project
- Mono is not required
- Run it on Trusty Tahr Ubuntu Linux
- Build it using .NET Core SDK 2.0
- Execute the steps
This is a simple Travis blueprint that does the basics: build and test execution.
Uploading the NuGet package
First, we will make it manually, and then automate it. We will be using
dotnet pack command that relies on the
csproj package configuration to generate the package. An example of such
csproj would be:
We also need to make sure we have the correct package sources configured. This can be done with a simple
nuget.config file in the project’s root, similar to the following:
Then we need to run the
dotnet pack command.
This will create a file in the output folder with the name
dotnet nuget push will make this package available on NuGet.org.
<API Key> is the key generated at NuGet.org.
And that should be enough to have your package available on NuGet.org. Let’s now revise our Travis blueprint to make it automatic.
condition statements will ensure that we only deploy when a tag is created on GitHub that follows the versioning pattern
M.m.p which is a typical use for GitHub releases.
Notice the variable
$NUGET_API_KEY? That’s because we dont want to expose our API Key on our project’s source code. But now a secret is necessary to store that variable. For that, run the Travis CLI in the project’s root to configure that variable.
The above will generate a secret hash from the API Key and will add it to the
Push your changes and watch it go live.
You may also want to include build and version badges in your README, as follows:
The above markdown include nice badges referencing your project’s build status and latest published version, as shown below:
In this post we covered how to make a .NET Core project hosted on GitHub available to download on NuGet.org through Travis CI.
This automation facilitates the whole development process and save developers a huge amount of time.
subscribe via RSS