This is an example of creating a new project, packing it, adding credentials, pushing it to GitHub Packages, and consuming from GitHub Packages.
dotnet new console --name PACKAGE_NAME
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PackageId>PACKAGE_NAME</PackageId>
<Version>1.0.0</Version>
<Authors>OWNER</Authors>
<Company>company</Company>
<PackageDescription></PackageDescription>
<RepositoryUrl>https://github.com/OWNER/REPO</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
dotnet pack --configuration Release
nuget.exe does this through a nuget sources add
command but the dotnet client does not support this command yet. Instructions for nuget.exe client in our docs here. This command creates a nuget.config
file so we can get around this by creating the file manually in the project directory:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="USER" />
<add key="ClearTextPassword" value="TOKEN" />
</github>
</packageSourceCredentials>
</configuration>
dotnet nuget push "bin/Release/PACKAGE_NAME.1.0.0.nupkg" --source "github"
dotnet new console --name NEW_PACKAGE (creating a random new project to demonstrate)
# Create a nuget.config in NEW_PACKAGE project directory
dotnet add NEW_PACKAGE.csproj package PACKAGE_NAME
This command adds the dependency to the project file:
<ItemGroup>
<PackageReference Include="PACKAGE_NAME" Version="1.0.0" />
</ItemGroup>
Note that this calls restore, which downloads and installs the package.
Another way to do this is editing the .csproj file directly and running dotnet restore
.
This only works for me using the gpr tool, not using
dotnet nuget push
.