Jenkins PowerShell Plugin parsing $env:BUILD_NUMBER

926 views Asked by At

I am trying to use the PowerShell Plugin on Jenkins, to build my cake script. And i want to parse the $env:BUILD_NUMBER into the Cake script - and that is working fine in the PowerShell Window on Windows using:

.\build.ps1 -buildNumber=123

But when using the "same" (Ok when parsing 123) in the PowerShell Plugin on Jenkins - it Fails. It is not parsing the value of the $env:BUILD_NUMBER to Cake. I am using this:

$bn = $env:BUILD_NUMBER;
cd D:\_Builds\Al.Common.Std.Interface\SolutionItems\
write-host $bn              //Just at test writing 123 OK
.\build.ps1 -buildNumber=$bn

In the cake script i use the $bn-value as NuGet version Number. But i get the following error:

C:\Program Files\dotnet\sdk\2.0.0\NuGet.targets(102,5): error : '1.0.0.$bn' is not a valid version string.

Any ideas are very welcome

1

There are 1 answers

0
devlead On

When it comes to environment variables, probably the best solution is to just read them using the EnvironmentVariable alias provided out of the box with Cake.

var buildNumber = EnvironmentVariable("BUILD_NUMBER");

That said for acquiring the build number from Jenkins, Cake actually already provides a way of doing that out of the box using the BuildSystem property alias which is globally available, it provides a Jenkins property which wraps all kind of information about your Jenkins environment including build number as a typed integer.

Information("Running on Jenkins {0}, build number: {1}.",
    BuildSystem.Jenkins.IsRunningOnJenkins ? "Yes" : "No",
    BuildSystem.Jenkins.Environment.Build.BuildNumber
);

Above will output something like Running on Jenkins Yes, build number: 1. if you're running on jenkins and Running on Jenkins No, build number: 0. if you aren't.

Now if you still want to pass as an argument to the issue is that PowerShell arguments work a bit differently than other shells and we've only wrapped the most common ones. For additional arguments to the build.ps1 bootstrapper script using the ScriptArgs parameter like this:

.\build.ps1 -ScriptArgs "--buildNumber=""$env:BUILD_NUMBER""" 

And then obtaining that parameter could look something like this

var buildNumber = Argument<int?>("buildNumber", 0);

Information("BuildNumber: {0} ({1})",
    buildNumber, buildNumber.HasValue);

And will output BuildNumber: [NULL] (False) if an invalid value is specified and something like BuildNumber: 1 (True) if a valid argument value is present (and it'll default to 0 if no value is specified).