Jenkins pipeline as code email ext - jenkins

I am using email ext syntax to send the email in pipeline as code and having an HTML template to send the email.
I need to access the Jenkins environment variable in HTML template. It is working for standard environment variables like BUILD_URL. But it is not working for environment variable set with env.VARIABLE='VALUE'. If I try to access ${VARIABLE} in HTML template it is not working.
Any idea please.

To get environment variables in your template, you should use some scripts to access Jenkins API.
In Jenkins there are two most popular options that I know: Groovy templates and Jelly templates.
Some more info you can find on Email-ext plugin page.
I have the same problem here: Access custom environment variables in jelly template, but I was able to get access to build parameters.
Here is a template how to do that with Jelly:
<?jelly escape-by-default='true'?>
<!DOCTYPE html [
<!ENTITY nbsp "&#38;nbsp;">
]>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<head>
<style>
body table, td, th, p, h1, h2 {
margin:0;
font:normal normal 100% Georgia, Serif;
background-color: #ffffff;
}
</style>
</head>
<body>
<j:set var="buildEnv" value="${build.getEnvironment(listener)}" />
<j:set var="myVar" value="${buildEnv.get('MY_BUILD_PARAMETER')}" />
<table>
<tr>
<td>Variable</td>
<td>
<p>${myVar}</p>
</td>
</tr>
</table>
</div>
</body>
</j:jelly>
So, you just need add some jelly tags, and to get build parameters value in this templates you need call getEnvironment method from build object.
<j:set var="buildEnv" value="${build.getEnvironment(listener)}" />
<j:set var="myVar" value="${buildEnv.get('MY_BUILD_PARAMETER')}" />

Below text in Jenkins File worked for me
env.CURRENT_BRANCH = master
env.PIPELINE_TYPE = mytype
Below lines in Jelly\HTML file
Using the below variable all the environment variables defined in Jenkins can be accessible
${customEnvVar.PIPELINE_TYPE}-${customEnvVar.CURRENT_BRANCH}
Before this code customEnvVar needs to defined
< j:set var="customEnvVar" value="${it.getAction('org.jenkinsci.plugins.workflow.cps.EnvActionImpl').getOverriddenEnvironment()}"/>

Related

Write all files in a directory to .gsp page

Lets say I have a gsp page that I want to load all the scripts in a particular folder:
<html>
<head>
{each file in $path}
<asset:javascript src="file" />
</head>
</html>
assuming $path is a directory path. Most templating languages have a way to do this so I'm sure Grails can accomplish it. But I'm not sure how to make it happen. My end goal is this:
ndex
<html>
<head>
<asset:javascript src="js/util.js" />
<asset:javascript src="js/util2.js" />
<asset:javascript src="js/index.js" />
</head>
</html>
Please help.
You can do something like this:
<html>
<head>
<g:each var="file" in="${(new File(path)).listFiles()*.path}">
<asset:javascript src="${file}" />
</g:each>
</head>
</html>
The GSP g:each tag is how iteration is performed in Grails when using GSP. The in attribute is used to specify the Iterable to iterate through. In this case it's the expression:
(new File(path)).listFiles()*.path
The expression means:
new File(path) - Create a Java File object to represent the directory path.
.listFiles() - Returns a list of all files and directories (excluding sub-directories) each represented by File objects.
*.path - A spread operator expression which returns a list of file path Strings, effectively converting the File objects into Strings. It's the equivalent of .collect { it.path }.

Parsing HTML in Jenkins

I'm using poll-mailbox-trigger-plugin to trigger Jenkins jobs based on incoming emails.
One of the build parameters (pmt_content) contains the body of the email specified in HTML.
Is there a Jenkins plugin that can parse the HTML and retrieve the values of user-specified tags?
Email content example:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8">
<title></title>
</head>
<body style='margin:20px'>
<p>The following user has registered a device, click on the link below to
review the user and make any changes if necessary.</p>
<ul style='list-style-type:none; margin:25px 15px;'>
<li><b>User name:</b> Test User</li>
<li><b>User email:</b> test#abc.com</li>
<li><b>Identifier:</b> abc123def132afd1213afas</li>
<li><b>Description:</b> Tom's iPad</li>
<li><b>Model:</b> iPad 3</li>
<li><b>Platform:</b></li>
<li><b>App:</b> Test app name</li>
<li><b>UserID:</b></li>
</ul>
<p>Review user: https://cirrus.app47.com/users?search=test#abc.com</p>
<hr style='height=2px; color:#aaa'>
<p>We hope you enjoy the app store experience!</p>
<p style='font-size:18px; color:#999'>Powered by App47</p><img alt='' src=
'https://cirrus.app47.com/notifications/562506219ac25b1033000904/img'>
</body>
</html>
Specifically, how could I retrieve the value of the "Identifier:" tag?
I'm sure I could write a script to do it but I'd rather the logic in Jenkins.
Is there a Jenkins plugin that can parse the HTML and retrieve the values of user-specified tags?
Its a one-liner on the shell or few lines in the scripting language of your choice. But seems, thats not what you are looking for.
In general, no, there isn't a plugin for the purpose of parsing HTML and retrieving the value of a tag, see https://wiki.jenkins-ci.org/display/JENKINS/Plugins
How could I retrieve the value of the "Identifier:" tag?
There is a generic plugin called Conditional BuildStep,
which supports regular expressions on parameters.
When the HTML Email content is in pmt_content you could use the following
RegExp
<li><b>Identifier:<\/b>(.*)<\/li> to extract the value abc123def132afd1213afas (or match and exec another command, if found).

Getting syntax errors: JellyException: Could not parse Jelly script : null

I am trying to read set of files ending with .errors extensions and trying to make them a part of the email sent out by jenkins via email-extension plugin. I have taken the basic html.jelly and trying to modify it. I am getting what looks like syntax errors. I don't really know how to debug it. But I went back to original and added stuff in steps. I start getting the errors when I add the ant:fileScanner
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:ant="jelly:ant">
<BODY>
<j:set var="buildenv" value="${build.getEnvironment(listener)}"/>
<j:set var="parentWorkspace" value="${buildenv.get('PARENT_WORKSPACE')}"/>
<ant:fileScanner var="errorFiles">
<ant:fileset dir="${parentWorkspace}">
<ant:include name="*.errors" />
</ant:fileset>
</ant:fileScanner>
<j:forEach var="errorFile" items="$(errorFiles)">
<util:loadText file="${errorFile}" var="errorText"/>
<TR>
<TD>${errorText}</TD>
</TR>
</j:forEach>
<BR/>
</BODY>
</j:jelly>
Any help is much appreciated. I am almost at my wits end.
EDIT: Looks like any ANT task I am trying to invoke in the jelly script is leading to errors. So looks like I am doing something wrong on ANT front. I do have ANT installed on the machine if that makes a difference.

How to get the checked Checkbox values from GSP to java script

Hi I am new to grails and GSP
I have a code like
<g:each var="i" in="${typeList}">
<g:if test="${i != null}">
<tr>
<td><input type="checkbox" name="categoryType" id="categoryTypeCB" class="categoryTypeCB" value="${i}"> ${i}</td>
</tr>
</g:if>
</g:each>
How to get the values of checked check boxes in java script
Try to use jQuery. Since Grails 2.0 it's provided by default, you just have to add in your gsp template at the end of head tag with following line:
<r:require module="jquery" />
Or if you do not use resources plugin, include jQuery with following line:
<g:javascript library='jquery' />
And then in a javascript block go with:
<g:javascript>
var checkedCheckboxes = $('.categoryTypeCB:checked');
$.each(checkedCheckboxes, function(index, checkbox) {
var theValue = checkbox.value;
});
</g:javascript>
The each funciton is a loop so you need to handle somehow 'theValue' each iteration. The checkbox argument contains the input element itself if you need it.
BTW. You shouldn't assign the same id for many inputs. It's incorrect. Id has to be unique for each HTML element among document tree.

Can I configure jenkins to send an email with a static analysis report summary?

I'd like to send an email after a build completes with some data from the reports that are run (PMD, Checkstyle, Findbugs, Cobertura) such as number of issues, new issues, coverage etc.
Is this possible?
I've managed to get some data using the email-ext plugin. You need to include a jelly file in the email sent like this:
${JELLY_SCRIPT, template="html"}
There is a default template (html.jelly) which includes junit and Cobertura results which I've modified by adding something like this:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
...
<j:set var="fb" value="${it.getAction('hudson.plugins.findbugs.FindBugsResultAction')}" />
<table width="100%">
<tr><td colspan="2"><b>Findbugs Result</b></td></tr>
<tr><td>Total:</td><td>${fb.result.numberOfWarnings}</td></tr>
<tr><td>Fixed:</td><td>${fb.result.numberOfFixedWarnings}</td></tr>
<tr><td>New:</td><td>${fb.result.numberOfNewWarnings}</td></tr>
<tr><td colspan="2">View Report</td></tr>
</table>
...
</j:jelly>
For PMD and CheckStyle you can do something similar with:
<j:set var="pmd" value="${it.getAction('hudson.plugins.pmd.PmdResultAction')}" />
<j:set var="cs" value="${it.getAction('hudson.plugins.checkstyle.CheckStyleResultAction')}" />
I've not yet found a way to include the low/medium/high priority figures for the results.
<j:set var="pmd" value="${it.getAction('hudson.plugins.pmd.PmdResultAction')}" />
<j:if test="${pmd.isEmpty()!=true}">
<TABLE width="100%">
<TR><TD colspan="2" class="bg1"><B>PMD Result</B></TD></TR>
<tr><td>Total:</td><td>${pmd.result.numberOfWarnings}</td></tr>
<tr><td>High:</td><td>${pmd.result.getNumberOfAnnotations('HIGH')}</td></tr>
<tr><td>Normal:</td><td>${pmd.result.getNumberOfAnnotations('NORMAL')}</td></tr>
<tr><td>Low:</td><td>${pmd.result.getNumberOfAnnotations('LOW')}</td></tr>
<tr><td>New:</td><td>${pmd.result.numberOfNewWarnings}</td></tr>
<tr><td>Fixed:</td><td>${pmd.result.numberOfFixedWarnings}</td></tr>
<tr><td colspan="2">View Report</td></tr>
</TABLE >
<BR/>
</j:if>
Have a try! More check my gist here: https://gist.github.com/yangboz/9204868
In my case worked https://stackoverflow.com/a/22008267/1688570
For exact Action names look in \Jenkins\jobs\fispp-all-master\builds\${buildNumber}\build.xml file.
I.e. I had to use hudson.plugins.findbugs.FindBugsMavenResultAction to show correct results.

Resources