Nuget Operations

Create and publish NuGet packages to nuget.org or private feeds.

Operations

Operation Description
Nuget.EnsureAuthenticated
Ensures NuGet API key is available for publishing. Prompts interactively if NUGET_API_KEY environment variable is not set. Call before Push.
Nuget.Pack
Create a NuGet package from a project. Defaults: Release config, output to bin/Release.
Nuget.Push
Push packages to a feed. Pass a ProjectRef to push from bin/Release/*.nupkg, or a path/glob. Defaults: NuGet.org, skip duplicates (won't fail if version already exists).

Operation Details

Nuget.EnsureAuthenticated

Ensures NuGet API key is available for publishing. Prompts interactively if NUGET_API_KEY environment variable is not set. Call before Push.

var app = Dotnet.Project("./src/MyLib/MyLib.csproj");
Nuget.EnsureAuthenticated();
Nuget.Push(app);

Nuget.Pack

Create a NuGet package from a project. Defaults: Release config, output to bin/Release.

var app = Dotnet.Project("./src/MyLib/MyLib.csproj");
Nuget.Pack(app);
Nuget.Pack(app, o => o.WithVersion("1.0.0"));

Nuget.Push

Push packages to a feed. Pass a ProjectRef to push from bin/Release/*.nupkg, or a path/glob. Defaults: NuGet.org, skip duplicates (won't fail if version already exists).

var app = Dotnet.Project("./src/MyLib/MyLib.csproj");
Nuget.Pack(app);
Nuget.EnsureAuthenticated();
Nuget.Push(app);
Nuget.Push("./packages/MyLib.1.0.0.nupkg");

Authentication

NuGet.org requires an API key for publishing packages. You can generate one at nuget.org/account/apikeys.

For CI/CD, set the NUGET_API_KEY environment variable and access it via Vars["NUGET_API_KEY"].

Example

Build a library and publish to NuGet.org.

build.csando
// Define project reference
var App = Dotnet.Project("./src/MyLib/MyLib.csproj");

// Build the project
Dotnet.Build(App);

// Create NuGet package (outputs to bin/Release)
Nuget.Pack(App);

// Authenticate and push to NuGet.org
// Defaults: nuget.org, skips if version already exists
Nuget.EnsureAuthenticated();
Nuget.Push(App);