C# and Infrastructure. Code is everything.

This post was originally published on Red Hat Developers, the community to learn, code, and share faster. To read the original post, click here.

When you build your .NET Core project, NuGet packages are retrieved from nuget.org by default. Sometimes, however, you might want to use a local NuGet repository. For example, you may want to:

use private NuGet packages, but you don’t want anyone except your associates to see them.

cache a NuGet repository at a server near your build servers

leave your build server disconnected from the Internet.

I’ll explain how to set up a private NuGet server on OpenShift and how you can use this NuGet server when building your .NET Core project in OpenShift using s2i-dotnetcore.

Set up nexus server as a private NuGet server

Unfortunately, the OSS Microsoft NuGet.Server only runs on Windows since it requires the .NET Framework. Nexus is a cross-platform repository solution from Sonatype. The free
OSS runs on Linux and in Linux containers. SonaType provides RHEL (and CentOS) based templates for deploying Nexus OSS on OpenShift.

After creating objects, an initial build starts. It may take a long time. When the build is finished, a Nexus image is available on OpenShift. Now, we can deploy this image to OpenShift. This template includes a PVC with readwriteonce access mode and specified size (default 2Gi). If you don’t have available Persistent Storage, please set up following the document.

Once deployment is finished, you can access the nexus server with specified hostname using your browser. You can log in as admin with default password admin123.

You will find two NuGet servers under components, nuget.org-proxy and nuget-hosted.

The nuget.org-proxy is a caching server of NuGet.org server. When the proxy server doesn’t contain the requested NuGet package, it downloads it from NuGet server and stores it. When a stored NuGet package is requested later, the proxy server provides a stored package. No configuration is required to use the nuget.org-proxy server. Let’s use this server with s2i-dotnetcore.

s2i .NET Core with nuget.org-proxy

The OpenShift s2i-dotnetcore builder has a configuration variable for specifying NuGet servers:DOTNET_RESTORE_SOURCES for a build environment variable. You can specify it when creating your app with OpenShift web console:

The build will now use the local NuGet server. When the build is finished, you can find the cached NuGet packages at the nexus console.

Add your private NuGet package

When you want to use nexus server to host your private packages, you can use nuget-hosted server. You have to push your private packages to this NuGet server. To do so, a NuGet APIKey is required. To find your NuGet, click the user icon on the right in the header and select NuGet API Key

You also have to add NuGet API-Key Realm to Realms in the server administrator settings. While logged in as the admin user, click the gear icon in the header, then click Realms and activate it.

Now you can push your package to NuGet server. If your package file is myawesomelibrary.nupkg, the command is as followed.

Conclusion

We learned to setup SonaType Nexus OSS on OpenShift and how to use it as a local NuGet server for the OpenShift s2i-dotnetcore builder. If you use nexus for production, you should need to provide sufficient a storage space for nuget packages and setup user authentication.