Search code examples
c#cakebuild.net-4.8

Cake runner for .NET Framework (4.8) doesn't pick up environment variables


I'm using the Cake runner for .NET Framework to build a .NET Framework 4.8 console application. I got build.ps1 using:

Invoke-WebRequest https://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1

No changes of my own in that file. And my build.cake looks like this:

var configuration = "Release";
var buildPath = $"CakeEnvDemo/bin/{configuration}";
var buildDir = Directory(buildPath);

var solution = "CakeEnvDemo.sln";

Task("Clean")
    .Does(() => 
    {
        Information("Running Clean ...");
        CleanDirectory(buildDir);
    });

Task("NuGetRestore")
    .IsDependentOn("Clean")
    .Does(() => 
    {
        Information("Running NuGetRestore ...");
        NuGetRestore(solution);
    });


Task("Build")
    .IsDependentOn("NuGetRestore")
    .Does(() => 
    {
        MSBuild(solution, settings => settings.SetConfiguration(configuration));
    });

Task("AfterBuild")
    .IsDependentOn("Build")
    .Does(() => 
    {
        Information("Running After Build ...");

        var cakeEnv = EnvironmentVariable("cake_env", "default value");
        
        Information($"Got value of cake_env from environment: {cakeEnv}");

    });


RunTarget("AfterBuild");

Take a look at the "AfterBuild" Task. Basically, I want to read an environment variable after the build is complete and do something with it. However, not matter what I do, cake doesn't seem to be able to pick it up and instead always uses the default value I provide.

Based on the docs here, I'm assuming that something like this:

$cake_env="actual env var value"
.\build.ps1

should work, but it never does. I always get the default value:

========================================
AfterBuild
========================================
Running After Build ...
Got value of cake_env from environment: default value

I do NOT want to use arguments, so please don't recommend ScriptArgs. I want environment variables.


Solution

  • This:

    $cake_env="actual env var value"
    

    Is setting a local PowerShell variable, not an environment variable.

    You should be able to use:

    $env:cake_env="actual env var value"