Arguments And Environment Variables

How you can pass settings to build.cake.

Arguments

Call the Argument() alias in your cake file to get the argument from the command line.

var mySetting = Argument("my_setting", "default value");

For example:

var mySetting = Argument("my_setting", "default value");

Task("Default")
    .Does(() =>
{
    Information("My setting is: " + mySetting);
});

RunTarget("Default");

PowerShell:

.\build.ps1 -ScriptArgs '-my_setting="from PowerShell"'

The output is

My setting is: from PowerShell

Batch File:

powershell -File build.ps1 -ScriptArgs '-my_setting="from a batch file"'

The output is

My setting is: from a batch file

Environment Variables

Call the EnvironmentVariable() alias in your cake file to get the environment variable.

var mySetting = EnvironmentVariable("my_setting") ?? "default value";

For example:

var mySetting = EnvironmentVariable("my_setting") ?? "default value";

Task("Default")
    .Does(() =>
{
    Information("My setting is: " + mySetting);
});

RunTarget("Default");

PowerShell:

$env:my_setting = "from PowerShell"
.\build.ps1

The output is

My setting is: from PowerShell

Batch File:

SET my_setting=from a batch file
powershell -File build.ps1

The output is

My setting is: from a batch file