Coding with Titans

so breaking things happens constantly, but never on purpose

HowTo: Add NuGet source for TeamCity Agent

Recently I moved away from external NuGet repositories to the one available internally via my Gitea server. Own hosting has always this extra advantage that the space and build-minutes are unlimited. So why not to try this path. Setup was build-in with last release, publishing is similarly easy. Yet I had a small issue, when porting companion TeamCity dependant builds. Suddenly it couldn’t access the NuGet source via System account used on Windows as user running the .NET project referencing a custom package. It wasn’t able to restore the packages:

09:15:58 Step 1/5: Build the project (.NET) 3s
    dotnet build
        Starting: .NET SDK 7.0.202 "C:\Program Files\dotnet\dotnet.exe" build C:\BuildAgent\work\9ffebc7899039e9\src\backend\PortalProject.sln @C:\BuildAgent\temp\agentTmp\1.rsp --no-incremental
             in directory: C:\BuildAgent\work\9ffebc7899039e9
             MSBuild version 17.5.0+6f08c67f3 for .NET
               Identifying packages to restore for current build...
             C:\BuildAgent\work\9ffebc7899039e9\src\backend\Core\Core.csproj : error NU1101: Can't find package CodeTitans.Infrastructure. Package with given identifier doesn't exist in available sources: Microsoft Visual Studio Offline Packages, nuget.org

The solution is pretty easy, the proper source has to be added into the config. Only four obstacles that are important:

  1. proper URL for the self-hosted Gitea
  2. proper user
  3. token of the user to access the resource
  4. proper location of the config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- other sources -->
    ...
    <!-- new Gitea source -->
    <add key="GiteaPackages" value="https://<host>/api/packages/<org>/nuget/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <GiteaPackages>
        <add key="Username" value="<user_name>" />
        <add key="ClearTextPassword" value="<user_token>" />
    </GiteaPackages>
  </packageSourceCredentials>
</configuration>

Now, finally for:

  • System account it needs to be placed inside: C:\Windows\System32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config

  • for any other user inside: C:\Users\$USER\AppData\Roaming\NuGet\NuGet.config

Some additional info could also be found on StackOverflow.com.

Done! Use it wisely!