Member-only story

Static and dynamic conditions in C#/Nuke build pipelines

--

Sometimes you run into a little open-source project that makes you wonder how you ever lived without it. Nuke, the C#-based build pipeline framework, is definitely one of them. If you haven’t seen Nuke yet, check out my article for a in-depth explanation. For now, consider the following excerpt.

class Build : NukeBuild
{
public static int Main() => Execute<Build>(x => x.TargetA);

[Parameter] string AzureToken

Target Prepare => _ => _
.Requires(() => AzureToken)
.Executes(() =>
{
// Do something useful with SomeParameter
});

Target Deploy => _ => _
.DependsOn(Prepare)
.Executes(() =>
{
// Do something else
});

This is a simple example in which target Deploy has a dependency on target Prepare. When you tell Nuke to execute Deploy through build.ps1 (or nuke if you’ve installed it as global tool), it will make sure that Prepare is executed before Deploy. And since Prepare has a required parameter, SomeParameter, it’ll will make sure it has a non-null value. If not, it will either throw, or if you’re running the script from a command prompt, prompt you to enter a value.

Now, imagine that you want to execute a target only when a certain condition is met. For example, when you have another target that uses runtime logic to set a field to a certain value that another target should depend on:

bool HasChanges;

Target DetermineChanges => _ => _
.Executes(() =>
{…

--

--

Dennis "The Continuous Improver" Doomen
Dennis "The Continuous Improver" Doomen

Written by Dennis "The Continuous Improver" Doomen

Dennis is a veteran architect in the .NET space with a special interest in writing clean code, Domain Driven Design, Event Sourcing and everything agile.

No responses yet