View Source

{hudson-plugin-info:pluginId=groovy-postbuild}
{excerpt}This plugin executes a groovy script in the Hudson JVM. Typically, the script checks some conditions and changes accordingly the build result, puts badges next to the build in the build history and/or displays information on the build summary page.{excerpt}

h1. Usage

The groovy script can use the variable _manager_, which provides access to the following objects and methods:
* hudson - the current Hudson instance (see [hudson.model.Hudson|https://hudson.dev.java.net/nonav/javadoc/index.html?hudson/model/Hudson.html]).
* build - the current build (see [hudson.model.AbstractBuild|https://hudson.dev.java.net/nonav/javadoc/index.html?hudson/model/AbstractBuild.html]).
* listener - the build listener (see [hudson.model.BuildListener|https://hudson.dev.java.net/nonav/javadoc/index.html?hudson/model/BuildListener.html]).

* contains(file, regexp) - returns true if the given file contains a line matching _regexp_.
* logContains(regexp) - returns true if the build log file contains a line matching _regexp_.
* getMatcher(file, regexp) - returns a java.util.regex.Matcher for the first occurrence of _regexp_ in the given file.
* getLogMatcher(regexp) - returns a java.util.regex.Matcher for the first occurrence of _regexp_ in the build log file.

* setBuildNumber(number) - sets the build with the given number as current build. The current build is the target of all methods that add or remove badges and summaries or change the build result.

* addShortText(text) - puts a badge with a short text, using the default format.
* addShortText(text, color, background, border, borderColor) - puts a badge with a short text, using the specified format.
* addBadge(icon, text) - puts a badge with the given icon and text. In addition to the 16x16 icons offered by Hudson, groovy-postbuild provides the following icons:
{quote} !completed.gif! \- completed.gif
!db_in.gif! \- db_in.gif
!db_out.gif! \- db_out.gif
!delete.gif! \- delete.gif
!error.gif! \- error.gif
!folder.gif! \- folder.gif
!green.gif! \- green.gif
!info.gif! \- info.gif
!red.gif! \- red.gif
!save.gif! \- save.gif
!success.gif! \- success.gif
!text.gif! \- text.gif
!warning.gif! \- warning.gif
!yellow.gif! \- yellow.gif
{quote}

* addInfoBadge(text) - puts a badge with !info.gif! info icon and the given text.
* addWarningBadge(text) - puts a badge with !warning.gif! warning icon and the given text.
* addErrorBadge(text) - puts a badge with !error.gif! error icon and the given text.
* removeBadges() - removes all badges from the current build.
* removeBadge(index) - removes the badge with the given index.

* createSummary(icon) - creates an entry in the build summary page and returns a _summary_ object corresponding to this entry. The icon must be one of the 48x48 icons offered by Hudson. You can append text to the _summary_ object by calling its _appendText_ methods:
** appendText(text, escapeHtml)
** appendText(text, escapeHtml, bold, italic, color)
* removeSummaries() - removes all summaries from the current build.
* removeSummary(index) - removes the summary with the given index.

* buildUnstable() - sets the build result to _UNSTABLE_.
* buildFailure() - sets the build result to _FAILURE_.
* buildSuccess() - sets the build result to _SUCCESS_.


It is also possible to delete badges and summaries by using the following links:
* remove all badges:
** {{http{}}}{{://{color:navy}{_}your-hudson{_}{color}/job/{color:navy}{_}your-job{_}{color}/{color:navy}{_}build-number{_}{color}/parent/parent/plugin/groovy-postbuild/removeBadges}}

* remove all summaries:
** {{http{}}}{{://{color:navy}{_}your-hudson{_}{color}/job/{color:navy}{_}your-job{_}{color}/{color:navy}{_}build-number{_}{color}/parent/parent/plugin/groovy-postbuild/removeSummaries}}

* remove the badge with the given index:
** {{http{}}}{{://{color:navy}{_}your-hudson{_}{color}/job/{color:navy}{_}your-job{_}{color}/{color:navy}{_}build-number{_}{color}/parent/parent/plugin/groovy-postbuild/removeBadge?index={color:navy}{_}idx{_}{color}}}

* remove the summary with the given index:
** {{http{}}}{{://{color:navy}{_}your-hudson{_}{color}/job/{color:navy}{_}your-job{_}{color}/{color:navy}{_}build-number{_}{color}/parent/parent/plugin/groovy-postbuild/removeSummary?index={color:navy}{_}idx{_}{color}}}


h4. Example 1

The script below puts a warning badge and mark the build as unstable if it detects that deprecated methods were used.
{code:borderStyle=solid}if(manager.logContains(".*uses or overrides a deprecated API.*")) {
manager.addWarningBadge("Thou shalt not use deprecated methods.")
manager.createSummary("warning.gif").appendText("<h1>You have been warned!</h1>", false, false, false, "red")
manager.buildUnstable()
}

{code}
|| !example1.GIF! || !example1s.GIF! ||

h4. Example 2

Suppose we have a parameterized build, which uses the boolean parameter _storeToDB_ in order to instruct the build to store some artifacts into the database. The script below puts a badge next to the builds for which this parameter is set.
{code:borderStyle=solid}if("true".equals(manager.build.buildVariables.get("storeToDB"))) {
manager.addBadge("db_in.gif", "Stored to DB")
}{code} !example2.GIF!

h4. Example 3

Suppose we have a parameterized build, which uses the string parameter _version_. The script below puts a short text indicating the value of this parameter next to successful and unstable builds.
{code:borderStyle=solid}if(manager.build.result.isBetterOrEqualTo(hudson.model.Result.UNSTABLE)) {
manager.addShortText("v${manager.build.buildVariables.get('version')}")
}{code} !example3.GIF!

h4. Example 4

The script below determines how long it took to build the project and displays the corresponding value as a short text next to each build.
{code:borderStyle=solid}def matcher = manager.getMatcher(manager.build.logFile, "^Total time: (.*)\$")
if(matcher?.matches()) {
manager.addShortText(matcher.group(1), "grey", "white", "0px", "white")
}{code} !example4.GIF!

h4. Example 5

For badges, you can choose from the 16x16 icons provided by Hudson or from the icons provided by this plugin. Some of the names of groovy-postbuild icons conflict with the names of Hudson icons (e.g. yellow.gif). For these icons, you have to provide the complete path (e.g. /images/16x16/yellow.gif), in case you want a badge with the Hudson icon. You can also use icons provided by other plugins (e.g. [Green Balls|http://wiki.hudson-ci.org/display/HUDSON/Green+Balls]), by specifying the complete path to the icon.
{code:borderStyle=solid}manager.addBadge("star-gold.gif", "icon from Hudson")
manager.addBadge("yellow.gif", "icon from groovy-postbuild plugin")
manager.addBadge("/images/16x16/yellow.gif", "icon from Hudson")
manager.addBadge("/plugin/greenballs/16x16/green.gif", "icon from greenballs plugin"){code} !example5.GIF!


h4. Example 6

The script below displays on the build summary page all classes that use Sun proprietary API.
{code:borderStyle=solid}pattern = ~/.*src\/main\/java\/(.*)\.java:[^ ]* (.*) is Sun proprietary API and may be removed in a future release.*/
def map = [:]
manager.build.logFile.eachLine { line ->
matcher = pattern.matcher(line)
if(matcher.matches()) {
ownClass = matcher.group(1).replaceAll("/", ".")
sunClass = matcher.group(2)
map[ownClass] = sunClass
}
}
if(map.size() > 0) {
summary = manager.createSummary("warning.gif")
summary.appendText("Classes using Sun proprietary API:<ul>", false)
map.each {
summary.appendText("<li><b>$it.key</b> - uses $it.value</li>", false)
}
summary.appendText("</ul>", false)
}{code} !example6.GIF!

h4. Example 7

The script below removes all badges and summaries from previous builds.
{code:borderStyle=solid}currentBuildNumber = manager.build.number
for(i=1; i<currentBuildNumber; i++) {
if(manager.setBuildNumber(i)) {
manager.removeBadges()
manager.removeSummaries()
}
}{code}

h4. Example 8

The script below marks the running build as deployed and the previous build as undeployed.
{code:borderStyle=solid}manager.addShortText("deployed")
manager.createSummary("gear2.gif").appendText("<h2>Successfully deployed</h2>", false)

currentBuildNumber = manager.build.number
if(manager.setBuildNumber(currentBuildNumber - 1)) {
actions = manager.build.actions
actions.each { action ->
if (action.metaClass.hasProperty(action, "text") && action.text.contains("deployed")) {
actions.remove(action)
}
}
currDate = new Date().dateTimeString
manager.addShortText("undeployed: $currDate", "grey", "white", "0px", "white")
manager.createSummary("gear2.gif").appendText("<h2>Undeployed: $currDate</h2>", false, false, false, "grey")
}{code} !example8.GIF!

h4. Example 9 (thanks to Ken Bertelson)

The script below changes the description of the first failed test.
{code:borderStyle=solid}def tr = manager.build.testResultAction.result
def cr = tr.failedTests.get(0)
cr.description = "My CaseResult desc"
{code}