且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jenkins:在电子邮件正文中报告中间[windows batch]构建步骤的结果

更新时间:2022-12-03 12:47:46

在正常"情况下,在构建步骤中设置的环境变量不会传播到外部.您可以执行以下操作:

  • 安装 EnvInject插件
  • 设置环境变量并将其保存为格式为VAR=VALUE
  • 的属性文件
  • 在下一步构建步骤中,使用EnvInject插件导入文件(安装该插件后,您将在构建步骤选项中看到注入环境变量").这些变量将在整个构建过程中可用.

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

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.