Skip to main content
Kubernetes Made Easy! Instantly spin up best-practice clusters with GetInfra templates. Browse Now

Connecting to Gitlab's private Nuget registry

Running a nuget registry for your project might be a good solution for a small project, however the setup is not that smooth as expected.

Reading the official documentation can be misleading. As in my case, for example, I use gitlab version 16.x, personal access tokens (PAT) is not longer a valid way to authorise agains nuget registry.

Starting from version 13, the working way is to use Deploy tokens.

Nuget registry is enabled by default on project level and on group level. Reasonable way to work is to push packages at project level and read a nuget "feed" on group level.

Pushing a package to registry

To Push the project add the following step to your .gitlab-ci.yaml:

script:  
 - dotnet build --configuration Release  
 - dotnet pack -p:PackageVersion=${VER} -o $PWD/nuget  
 - dotnet nuget add source "${CI\_API\_V4\_URL}/projects/${CI\_PROJECT\_ID}/packages/nuget/index.json" --name gitlab --username gitlab-ci-token --password $CI\_JOB\_TOKEN --store-password-in-clear-text  
 - dotnet nuget push "$PWD/nuget/\*.nupkg" --source gitlab  

Create a Deploy Token

On the left sidebar find your group
Select Settings > Repository.
Expand Deploy tokens.
Select Add token.
Enter your token details and check "read_package_registry" permission.

Registering you Nuget server as a source

You have two options adding new Nuget source to a project:

Add source with command line

dotnet nuget add source https://gitlab.yourdomain.com/api/v4/projects/${PROJECT_ID}/packages/nuget/index.json" --name gitlab --username nuget_reader --password <Your Deploy Token> 

Add source to nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="gitlab" value="https://gitlab.yourdomain.com/api/v4/groups/{PROJECT_ID}/-/packages/nuget/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <gitlab>
      <add key="Username" value="nuget_reader" />
      <add key="ClearTextPassword" value="Your Deploy Token" />
    </gitlab>
  </packageSourceCredentials>
</configuration>
 

Share this article

About the Author

Alexander Lvovich

Alexander Lvovich

Solution Architect & Software Developer | Automating & Scaling Infrastructure

💡 Working with Kubernetes, Istio, and DevOps. Got questions? Feel free to reach out!


No comments are allowed for this post