A collection of extension methods that simplify common build, packaging and release tasks when using Nuke Build.
Add the NuGet package to your build project (the one containing Build.cs):
dotnet add build/_build.csproj package CP.Nuke.BuildTools
Then add:
using CP.BuildTools; // at top of Build.csThe following extension methods are available (defined in build/Extensions.cs):
| Method | Purpose |
|---|---|
PublicNuGetSource() |
Returns the public NuGet v3 index URL. |
UpdateVisualStudio(version) |
Updates VS installation and applies common workloads. |
GetFileFromUrlAsync(url) |
Downloads text content from a URL. Returns empty string on blank URL. |
RestoreProjectWorkload(project) |
Runs dotnet workload restore for a single project. |
RestoreSolutionWorkloads(solution) |
Runs dotnet workload restore for the whole solution. |
GetPackableProjects(solution) |
Returns projects with IsPackable MSBuild property true. |
GetTestProjects(solution) |
Returns projects with IsTestProject MSBuild property true. |
InstallDotNetSdk(params versions) |
Installs SDK channels matching version patterns (e.g. 8.x.x, 9.0.100). |
GetAsset(repoOwner,repoName,assetName,releaseTag) |
Downloads a release asset from GitHub. |
SaveFile(path,bytes) |
Writes a file if it does not already exist. |
SetGithubCredentials(token) |
Sets credentials on Octokit client for later GitHub API calls. |
UploadReleaseAssetToGithub(release,assetPath) |
Uploads (or replaces) a single asset file into a draft release. |
UploadDirectory(release,directory) |
Uploads all root files in a directory as release assets. |
CreateRelease(repo,tag,version,commitSha,isPrerelease) |
Creates a draft GitHub release. |
AppendReleaseNotes(release,repo,maxCommits) |
Appends generated commit list to release body. |
Publish(release,repo) |
Publishes (un-drafts) a release. |
InstallAspNetCore(version) |
Installs ASP.NET Core runtime for a channel (>= 6). |
GenerateReleaseNotes(repo,maxCommits) |
Returns markdown listing recent commits. |
NOTE: Methods that execute external processes (
ProcessTasks.StartShell) or call GitHub API have side-effects and expect required tools (git, pwsh) and environment (GitHub PAT) to be available.
Below are example targets you can integrate into your Build class.
Target Info => _ => _
.Executes(() =>
{
Log.Information("NuGet V3: {Source}", this.PublicNuGetSource());
});Target SetupCI => _ => _
.Executes(async () =>
{
await this.UpdateVisualStudio(); // Enterprise by default
await this.InstallDotNetSdk("8.x.x", "9.x.x"); // install latest matching channels
this.InstallAspNetCore("8.0");
Solution.RestoreSolutionWorkloads();
});Target Pack => _ => _
.DependsOn(Compile)
.Executes(() =>
{
var packable = Solution.GetPackableProjects();
if (packable == null || packable.Count == 0)
{
Log.Warning("No packable projects found.");
return;
}
DotNetPack(s => s
.SetConfiguration(Configuration)
.CombineWith(packable, (ps, p) => ps.SetProject(p)));
});Target InstallSdks => _ => _
.Executes(async () =>
{
// Patterns: Major.Minor.Patch can use 'x' placeholders.
await this.InstallDotNetSdk("6.x.x", "7.x.x", "8.0.x", "9.0.100");
});Requires a GitHub PAT exported as an environment variable (e.g. GITHUB_TOKEN).
[Parameter][Secret] readonly string GitHubToken;
[GitRepository] readonly GitRepository Repository;
Target Release => _ => _
.Requires(() => GitHubToken)
.Executes(() =>
{
this.SetGithubCredentials(GitHubToken);
// Create draft release
var tag = $"v{NerdbankVersioning.NuGetPackageVersion}";
var release = this.CreateRelease(Repository, tag, NerdbankVersioning.NuGetPackageVersion, null, isPrerelease: false);
// Append commit based notes
release = release.AppendReleaseNotes(Repository, maxCommits: 40);
// Upload artifacts (assumes Pack target already produced nupkgs)
var artifactsDir = RootDirectory / "output";
release.UploadDirectory(artifactsDir);
// Publish
release.Publish(Repository);
});Target FetchAsset => _ => _
.Requires(() => GitHubToken)
.Executes(() =>
{
this.SetGithubCredentials(GitHubToken);
var bytes = this.GetAsset("owner", "repo", "mytool.zip", uiReleaseTag: null); // latest release
var dest = RootDirectory / "temp" / "mytool.zip";
this.SaveFile(dest, bytes);
Log.Information("Saved asset to {Path}", dest);
});Target Notes => _ => _
.Executes(() =>
{
var markdown = Repository.GenerateReleaseNotes(25);
File.WriteAllText(RootDirectory / "RELEASE_NOTES.md", markdown);
});Target CloneExample => _ => _
.Executes(() =>
{
var targetDir = RootDirectory / "external";
targetDir.CreateDirectory();
targetDir.Checkout("https://github.com/owner/repo.git");
});InstallDotNetSdkthrows if no matching stable versions are found.InstallAspNetCorethrows if version < 6 or cannot parse.CreateRelease,Publish,AppendReleaseNotesrequire a valid authenticated Octokit client (set viaSetGithubCredentials).- Network dependent methods (
GetFileFromUrlAsync, GitHub operations) should be wrapped with retry logic if used in unstable environments.
A typical pipeline using these utilities:
SetupCI– ensure tooling & SDKs.Restore(standard Nuke restore + workloads if needed).Compile.Pack.Release(optional for main / tagged builds).
MIT – see LICENSE file.