Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Created November 22, 2019 15:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Mattamorphic/4e2ff8f62f8e2bc7f04f9d2a91bc722d to your computer and use it in GitHub Desktop.
Save Mattamorphic/4e2ff8f62f8e2bc7f04f9d2a91bc722d to your computer and use it in GitHub Desktop.
Example of publishing GitHub Packages using the DotNet CLI

dotnet GitHub Packages Example

This is an example of creating a new project, packing it, adding credentials, pushing it to GitHub Packages, and consuming from GitHub Packages.

create a new project

dotnet new console --name PACKAGE_NAME

fill in .csproj file

<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

dotnet pack --configuration Release

add credentials

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

dotnet nuget push "bin/Release/PACKAGE_NAME.1.0.0.nupkg" --source "github"

adding a package

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.

@john-cheesman
Copy link

john-cheesman commented Feb 18, 2020

This only works for me using the gpr tool, not using dotnet nuget push.

dotnet tool install gpr -g
gpr push "bin/Release/PACKAGE_NAME.1.0.0.nupkg"

@Happypig375
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment