Cannot pull private NuGet package from GitLab package registry
Summary
When trying to pull a private NuGet package from a GitLab NuGet package registry, the botnet restore process fails and cannot load the package.
Steps to reproduce
- Have a project containing a NuGet package you depend on. In my case I created a basic .NET class library. Through my .gitlab-ci.yml I created a NuGet package and pushed it to my project using this config:
variables:
GIT_CLEAN_FLAGS: none
DOCKER_VERSION: 19.03.6
DOCKER_TLS_CERTDIR: "/certs"
stages:
- package
workflow:
rules:
- if: $CI_COMMIT_TAG
build-nuget-package:
image: mcr.microsoft.com/dotnet/sdk:7.0
stage: package
before_script:
- cd NuGet.Dependency
script:
- dotnet pack -c Release -p:Version=$CI_COMMIT_TAG
- dotnet nuget add source "$CI_SERVER_URL/api/v4/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 "bin/Release/*.nupkg" --source gitlab
only:
- tags
tags:
- docker
- In a second project (a simple .NET class library as well), add the NuGet source of the first project and add the NuGet package you just created as a dependency to the csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Dependency" Version="0.0.1" />
</ItemGroup>
</Project>
- Setup your .NET project to also be built using GitLab CI. Here is my .gitlab-ci.yml: (make sure you enter the correct project id for the NuGet source)
variables:
GIT_CLEAN_FLAGS: none
DOCKER_VERSION: 19.03.6
DOCKER_TLS_CERTDIR: "/certs"
stages:
- package
build:
image: mcr.microsoft.com/dotnet/sdk:7.0
stage: package
script:
- dotnet nuget add source "$CI_SERVER_URL/api/v4/projects/<your-first-project-id>/packages/nuget/index.json" --name dependencies --username gitlab-ci-token --password $CI_JOB_TOKEN --store-password-in-clear-text
- dotnet restore "DotNet.Project/DotNet.Project.csproj"
Example Project
https://gitlab.com/simon.grossmann/dotnet-project
What is the current bug behavior?
If you take a look at the logs of the CI job, you'll see that dotnet is unable to download the NuGet package.
[0KRunning with gitlab-runner 16.1.0~beta.59.g83c66823 (83c66823)[0;m
[0K on blue-4.saas-linux-small-amd64.runners-manager.gitlab.com/default J2nyww-s, system ID: s_cf1798852952[0;m
[0K feature flags: FF_USE_IMPROVED_URL_MASKING:true[0;m
section_start:1689958812:prepare_executor
[0K[0K[36;1mPreparing the "docker+machine" executor[0;m[0;m
[0KUsing Docker executor with image mcr.microsoft.com/dotnet/sdk:7.0 ...[0;m
[0KPulling docker image mcr.microsoft.com/dotnet/sdk:7.0 ...[0;m
[0KUsing docker image sha256:7765d946d8e285472ef4f665ad50428d1a8ac2cf559c462355714e67af5094c9 for mcr.microsoft.com/dotnet/sdk:7.0 with digest mcr.microsoft.com/dotnet/sdk@sha256:24f22e1142432dea3a34d350686be77be5e454ccb9fab7c00d67cd03c6e000a6 ...[0;m
section_end:1689958827:prepare_executor
[0Ksection_start:1689958827:prepare_script
[0K[0K[36;1mPreparing environment[0;m[0;m
Running on runner-j2nyww-s-project-47919001-concurrent-0 via runner-j2nyww-s-s-l-s-amd64-1689958616-1c66b152...
section_end:1689958833:prepare_script
[0Ksection_start:1689958833:get_sources
[0K[0K[36;1mGetting source from Git repository[0;m[0;m
[32;1mFetching changes with git depth set to 20...[0;m
Initialized empty Git repository in /builds/simon.grossmann/dotnet-project/.git/
[32;1mCreated fresh repository.[0;m
[32;1mChecking out fcca7a8b as detached HEAD (ref is master)...[0;m
[32;1mSkipping Git submodules setup[0;m
[32;1m$ git remote set-url origin "${CI_REPOSITORY_URL}"[0;m
section_end:1689958834:get_sources
[0Ksection_start:1689958834:step_script
[0K[0K[36;1mExecuting "step_script" stage of the job script[0;m[0;m
[0KUsing docker image sha256:7765d946d8e285472ef4f665ad50428d1a8ac2cf559c462355714e67af5094c9 for mcr.microsoft.com/dotnet/sdk:7.0 with digest mcr.microsoft.com/dotnet/sdk@sha256:24f22e1142432dea3a34d350686be77be5e454ccb9fab7c00d67cd03c6e000a6 ...[0;m
[32;1m$ dotnet nuget add source "$CI_SERVER_URL/api/v4/projects/47918992/packages/nuget/index.json" --name dependencies --username gitlab-ci-token --password $CI_JOB_TOKEN --store-password-in-clear-text[0;m
Package source with Name: dependencies added successfully.
[32;1m$ dotnet restore "DotNet.Project/DotNet.Project.csproj"[0;m
Determining projects to restore...
/builds/simon.grossmann/dotnet-project/DotNet.Project/DotNet.Project.csproj : error NU1101: Unable to find package NuGet.Dependency. No packages exist with this id in source(s): dependencies, nuget.org
Failed to restore /builds/simon.grossmann/dotnet-project/DotNet.Project/DotNet.Project.csproj (in 604 ms).
section_end:1689958836:step_script
[0Ksection_start:1689958836:cleanup_file_variables
[0K[0K[36;1mCleaning up project directory and file based variables[0;m[0;m
section_end:1689958837:cleanup_file_variables
[0K[31;1mERROR: Job failed: exit code 1
[0;m
If you try to download the file from the link manually, it works just fine (as long as you are authenticated). This is the content of the file from https://gitlab.com/api/v4/projects/47918992/packages/nuget/download/nuget.dependency/index.json:
{"versions":["0.0.1"]}
Here is the link to the job log: https://gitlab.com/simon.grossmann/dotnet-project/-/jobs/4710121563
Here is the link to a job log with dotnet diagnose outputs: https://gitlab.com/simon.grossmann/dotnet-project/-/jobs/4710201840
What is the expected correct behavior?
dotnet restore should work just fine when running in the pipeline. Locally, it works just fine.