Publish NuGet packages with GitLab and Nexus Repository Manager

Mayank Patel
Jun 1 · 5 min read

Reusable components in software development is a very important concept. This post presents the deployment of Nexus Repository Package Manager, GitLab Pipeline, and creation of DOTNET core NuGet package.

NuGet packages/reusable base libraries are core of Microservices architecture.

Prerequisite

  • Docker: 18.09.1 or above
  • Docker Compose: 1.24.0-rc1 or above
  • Docker Swarm enabled (docker swarm init)
  • Docker APP
  • Dotnet CLI
  • Editor of your choice (Visual Studio, Visual Studio Code, etc.)

Nexus Repository Manager

Docker Compose and settings files for Nexus.

GitHub Repository

nexus.dockerapp/docker-compose.yml

version: "3.7"

services:
nexus:
image: ${nexus.image.repository}:${nexus.image.tag}
environment:
NEXUS_CONTEXT: ${nexus.context}
volumes:
- type: volume
source: nexus-data
target: /nexus-data
deploy:
replicas: ${nexus.replicas}
labels:
traefik.protocol: http
traefik.port: '${nexus.port}'
traefik.frontend.entryPoints: http,https
traefik.frontend.rule: "Host:${ingress.host};PathPrefixStrip:${ingress.path}"
traefik.docker.network: ${ingress.network}
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 10s
update_config:
parallelism: 1
failure_action: rollback
delay: 15s
networks:
- app
- ingress

volumes:
nexus-data:

networks:
app:
ingress:
name: ${ingress.network}
external: true

nexus.dockerapp/settings.yml

nexus:
image:
repository: sonatype/nexus3
tag: latest
volume:
size: 10
context: nexus
port: 8081
replicas: 1

ingress:
host: test.company.com
path: /
network: traefik

nexus.dockerapp/metadata.yml

name: nexus

version: 0.1.0
description:

maintainers:
- name: Mayank Patel
email: test@xyz.com

Deploy Docker Application Package

docker-app deploy

Check the status of Docker services

docker service ls

Access Package manager on http://test.company.com/nexus

Default credential for Nexus Package Manager is admin/admin123

NOTE: Please make sure to change the default credentials.

Once logged in with admin credential navigate to NuGet API Key which will be required to publish packages. We will be configuring NuGet API Key for Gitlab Pipeline usage.

We also need to find URL for nuget-hosted repository to publish new packages through GitLab. Repository URL will be configured as a GitLab pipeline variable.


.Net Core

Create a new project using CLI

dotnet new classlib -o testlib

Package Structure

Use Dotnet standards and guidelines for namespace and Naming convention as displayed below for Model, Enum, Repository and Service classes.

Model

namespace Test.Hello.Modelspublic class Hello : Model

Enum

namespace Test.Hello.Modelspublic enum HelloTypes

Repository

namespace Test.Hello.Repositoriespublic class HelloRepository : IHelloRepository

Service

namespace Test.Hello.Servicespublic class HelloService : IHelloService

Base Library consideration

  • Avoid unnecessary dependency chaining
  • Keep base library classes generic
  • Apply single repository principal
  • Make sure you have good test coverage for the base library to avoid breaking multiple API’s
  • Make sure appropriate logging and error handling is implemented for troubleshooting purpose
  • Use standard versioning scheme like Semantic Versioning
  • Document usage of the library in README file
  • Use CONTRIBUTING guidelines
  • Generate CHANGELOG for library releases
  • Setup CI/CD pipeline
  • Auto increment versioning

CI/CD Pipeline

  • Compile code
  • Run test coverage
  • Generate Package with version scheme
  • Publish Package to Package Manager

Example .gitlab-ci.yml

Update variables for your usage

variables:
NEXUS_REPOSITORY: nuget-hosted
NEXUS_SERVER: localhost:8081/nexus
NEXUS_USERNAME: username
NEXUS_PASSWORD: password
NEXUS_NUGET_KEY: APIKEY
VERSION: 1.0.0
ARTIFACT_PATH: /sln/artifacts

stages:
- package

build-publish:
image: mcr.microsoft.com/dotnet/core/sdk:2.2
stage: package
script:
- echo " RESTORING NUGET PACKAGES "
- apt-get update && apt-get install -y nuget
- nuget sources Add -Name Nexus -Source https://${NEXUS_SERVER}/repository/${NEXUS_REPOSITORY}/ -username ${NEXUS_USERNAME} -password ${NEXUS_PASSWORD}
- nuget setapikey ${NEXUS_NUGET_KEY} -source http://${NEXUS_SERVER}/repository/${NEXUS_REPOSITORY}/
- dotnet restore
- echo " BUILDING SOLUTION "
- dotnet build /p:PackageVersion=${VERSION} --no-restore -c Release
- echo " EXECUTE UNIT TESTS "
- dotnet test
- echo " GENERATING NUGET PACKAGE "
- dotnet pack -c Release --no-restore --no-build /p:PackageVersion=${VERSION} -o ${ARTIFACT_PATH}
- echo " PUBLISHING NUGET PACKAGE "
- dotnet nuget push ${ARTIFACT_PATH}/*.nupkg --source http://${NEXUS_SERVER}/repository/${NEXUS_REPOSITORY}/ --api-key ${NEXUS_NUGET_KEY}

Above .gitlab-ci.yml file will help build code, generate package and publishing to Nexus package manager.


Verify Published NuGet package to Nexus Repository Package Manager


Configure Visual Studio

  • Open Visual Studio and Preferences
  • From side nav bar click NuGetSourcesAdd
  • Click Add Source and click OK
  • Open api.csproj file and add PackageReference under ItemGroup as shown below
<ItemGroup>
<PackageReference Include="testlib" Version="1.0.0" />
</ItemGroup>

Restore Packages from Visual Studio

Restore Packages using CLI

dotnet restore

Resources

Nexus package manager also can be used as NuGet Proxy Repositories

The NuGet Gallery is the common repository used by all package authors and consumers. To reduce duplicate downloads and improve download speeds for your developers and CI servers, you should proxy the NuGet Gallery with the repository manager.


I hope this post has helped you. If you enjoyed this article, please don’t forget to clap 👏, comment and share! I would love to know what you think and would appreciate your thoughts on this topic. You can also follow me on Medium, GitHub, and Twitter for more updates.

Mayank Patel

Written by

System Architect at Drillinginfo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade