Include jenkins environment variables in groovy template for email notification

I have several environment variables defined for the jenkins project, among which I want to include some of the email notifications sent before and after the build was successful.

But groovy.text.Template does not seem to accept these environment variables.

I also used "Injection environment variables for build help for reference: the function" Embed environment variables in the build process "" and defined my variable as follows

 BUILD_NAME=${BUILD_NAME} 

where BUILD_NAME taken as a parameter and I am building.

Please help me with this.

+4
source share
4 answers

If you use the Jenkins email-ext plugin to send emails, there is a β€œbuild” object that contains a lot of information about your Jenkins work.

I would do the following:

  • Print the properties inside this object by adding the following code to your template:

    <% println build.properties.collect{it}.join('<br />') %>

  • View email template output. You can use Jenkins' Email Template Checker feature or just get your work done. You should see a whole bunch of "KEY = VALUE" printouts.

  • Look at the environment variable you are interested in. If the environment variable is specified in environment={BUILD_NUMBER=45, BUILD_DISPLAY_NAME=#45...} , you can simply call build.environment['BUILD_NUMBER'] to get 45.

  • If you want to add custom environment variables, I would suggest installing Env Inject Plugin , defining your variables through this plugin, and then looking for the variable in build.envVars

+4
source

It seems you can try this

 template.make(build.environment) 

Source

+1
source

I hope I understand your question, but in order to read this parameter in the template, you need to call it like this (if BUILD_NAME is a parameter in the jenkins job:

 ${ENV, var="BUILD_NAME"} 

This will return the value of this parameter.

+1
source

Just do:

 <% def env = build.environment %> <% def name = env.build_name %> <% if (name != "") { %> Name: <%=name %> <% } %> 
-one
source

All Articles