Use Environment variable inside HTML body in Jenkins Pipeline

1.9k views Asked by At

I have a Jenkins Pipeline which sends email to the recipients using Jenkins email-ext plugin. In the emailext body, I have added HTML code to send the mail in a specific format. However, when I try to use the environment variable, it does not return the value of the variable.

In my pipeline, I have set

SERVICE_NAME = "Service Variable"

emailext body:
 ''' <html>
     <body>
    
    <div style="padding-left: 30px; padding-bottom: 15px;" color="blue">
    <font color="navy" size="5"> <b> ${SERVICE_NAME} </b> </font> <br>
    <b> Build </b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp; <font color="blue"># ${BUILD_NUMBER} </font><br>
    <b> Git Branch </b> &nbsp; : &nbsp;&nbsp;&nbsp; <font color="gray"> ${BRANCH_NAME} </font> <br>
    <b> Status </b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp; <font color="red"> ${BUILD_STATUS} </font><br>
    <b> Cause </b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp; <font color="gray"> ${BUILD_CAUSE} </font>
    </div>
    <div style="padding-left: 30px; padding-bottom: 15px;">
    ${CHANGES, showPaths=true, format="<div><b>Author</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp; <b> %a </b><br><b>Commit-Id</b> &nbsp; : &nbsp;&nbsp;&nbsp;  %r <br>  </div><div style=\\"padding-left:30px;\\"> <b> Message </b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp; &#8220;<em>%m</em>&#8221;</div>", pathFormat="</div><div style=\\"padding-left:30px;\\">%p"}
    </div>
    </body>
    </html>''' ,       
                        mimeType: 'text/html',
                        subject: "Some subject ",
                        to: 'mail.com'
                

OUTPUT:- ${SERVICE_NAME}

Instead it should return "Service Variable" in the mail.

Any help regarding this please? Thanks!

2

There are 2 answers

1
MaratC On

Your issue is about string interpolation.

As said in Groovy Syntax:

Any Groovy expression can be interpolated in all string literals, apart from single and triple-single-quoted strings.

As you provide triple-single-quoted string, you are specifically asking Groovy not to interpolate any variables in it.

To fix your issue and make string interpolation work, change triple-single-quoted into a triple-double-quoted string:

emailext body: """
    <html>
    <body>
    <div style="padding-left: 30px; padding-bottom: 15px;" color="blue">
    <font color="navy" size="3"> <b> ${SERVICE_NAME} </b> </font> <br>
    </body></html> """
0
Dnyaneshwar Navle On

You can use string concatenation in groovy.

Like: body: ''' some text ''' + " ${var} " + ''' remaining text.'''

emailext body:''' <html>
 <body>
<div style="padding-left: 30px; padding-bottom: 15px;" color="blue"> <font color="navy" size="5"> <b> ''' + "${SERVICE_NAME}" + ''' </b> </font> <br>
<b> Build </b> #''' + "${BUILD_NUMBER}" + '''<br>
</body>
</html>''',