Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins : Report results of intermediate [windows batch] build steps in email body

Tags:

jenkins

As part of a Jenkins build process we run a couple Windows batch scripts and would like to report some information about these intermediate steps in the email that gets send out by Jenkins at the end.

I thought a simple approach would be to set a Windows environment variable in the batch step and we managed to do so using PowerShell as in:

powershell.exe -Command "& {[Environment]::SetEnvironmentVariable('ThisIsATest', 'TestValue', 'Machine')}"

When executed in a "Execute Windows batch command" build step this correctly sets the environment variable "eventually". By that I mean if I remote into the build server I can verify that the value was created / modified as expected during each build.

However, attempting to use the value in the "Editable Email Notification" step under "Post-build Actions" by adding

Our test value: ${ENV, var="ThisIsATest"}

to the "Default Content" section does not give the correct result. Using

Print full environment: ${ENV}

also shows that the value has ??not yet?? been created.

Even stranger is that multiple runs do not pick up the environment variable in question at all - even though I can clearly see it in the remote session (and track changes to it etc.).

Questions:

  • Would there be other ways of achieving our goal:

    "Report general success/failure when compile/unit tests where OK/failed but also report additional info from intermediate steps in the email body"?

  • Or maybe explain the odd (at least to me) behavior with regards to the email plugin not picking up environment variables that one would expect it to? (To maybe give some new ideas on where to look for workarounds)

Thanks for taking the time to read this. As always, any advice is appreciated.

-T

like image 982
Tobias Avatar asked Jan 16 '23 16:01

Tobias


1 Answers

Environment variables set inside a build step do not propagate outside under the 'normal' circumstances. You can do the following:

  • Install EnvInject Plugin
  • Set environment variables and save them into a property file in the form VAR=VALUE
  • In next build step import the file with EnvInject plugin (after installing the plugin you'll see 'Inject environment variables' among build step options). The variables will be available throughout the build.
like image 92
malenkiy_scot Avatar answered Jun 03 '23 18:06

malenkiy_scot