Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorporate jenkins environment variables into groovy template for email notification

I have several environment variables defined for a project on jenkins, amongst which i want to incorporate some onto the email notification sent before and after build success.

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

I have also used "Inject environment variables to the build process Help for feature: Inject environment variables to the build process" and defined my variable as follows

BUILD_NAME=${BUILD_NAME}

where BUILD_NAME is accepted as a parameter while i execute the build.

Please could someone help me on this.

like image 362
user1891622 Avatar asked Mar 23 '23 08:03

user1891622


1 Answers

If you are using the email-ext Jenkins plugin to send out emails, there is a "build" object that contains a lot of information about your Jenkins job.

I would do the following:

  1. Print out the properties inside this object by adding the following code to your template:

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

  2. View the email template output. You can either use the Jenkins "Email Template Testing" feature or just run your job. You should see a whole bunch of "KEY=VALUE" print outs.

  3. Look for the environment variable you are interested in. If your environment variable is listed in environment={BUILD_NUMBER=45, BUILD_DISPLAY_NAME=#45...}, you can just call build.environment['BUILD_NUMBER'] to get 45.

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

like image 76
oztheozoz Avatar answered Mar 24 '23 22:03

oztheozoz