Jenkins pass build parameters to email-ext template
Asked Answered
A

3

7

How can I pass the build parameters to the email-ext jelly template? Before making some builds I would like to put a different custom message/some announcement/non-code changes, decisions etc. I'm thinking of putting this as a build parameter so that I'm presented with a screen prompting me to enter the message if I'm going to build manually.

Edit: I found a solution, the build params are available as Env variables, I have this code to access Env variables:

   <j:set var="buildenv" value="${build.getEnvironment(listener)}"/>
   <j:set var="customMsg" value="${buildenv.get('customMsg')}"/>
   ${customMsg}
Argueta answered 27/6, 2013 at 0:33 Comment(1)
If you found a solution, create a new answer and accept it.Medium
P
3

Switch to the email-ext groovy email templates.

Then, copy the sample groovy-html.template and customize it by adding the following script -

<%
  def parametersAction = build.getAction(ParametersAction.class)

  if (parametersAction != null)
  {
    for (p in parametersAction.parameters)
    {
      %><%=p.name%>=<%=p.value%><br/><%
    }                                               
  }
%>

Take a look at the hudson.model.ParametersAction class.

Peasant answered 28/6, 2013 at 0:43 Comment(1)
Thanks: I found a different way, the build params are available as Env variables, I have this code to access Env variables: '<j:set var="buildenv" value="${build.getEnvironment(listener)}"/> <j:set var="customMsg" value="${buildenv.get('customMsg')}"/> ${customMsg}'Argueta
C
2

The only what worked for me in my email template is:

<%
    import hudson.model.*

    def YOUR_VARIABLE= build.getEnvVars()["SOME_BUILD_PARAMETER"];
%>

Then you can use

${YOUR_VARIABLE}
Creight answered 19/5, 2018 at 10:12 Comment(0)
S
0

The only way that worked for me for variables defined inside my pipeline script was the accepted answer of this question:

Access variable in email ext template using Jenkins pipeline

I added a answer that variables needed to be defined like

env.MY_VARIABLE="Some value"

inside the pipeline script.

Salisbarry answered 3/3, 2022 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.