Groovy Postbuild Plugin

Skip to end of metadata
Go to start of metadata

Plugin Information

Plugin ID groovy-postbuild Changes In Latest Release
Since Latest Release
Latest Release
Latest Release Date
Required Core
Dependencies
1.8
Aug 22, 2012
1.466
Source Code
Issue Tracking
Maintainer(s)
GitHub
Open Issues
Stefan Wolf (id: wolfs)
Serban Iordache (id: beryx)
Usage Installations 2013-Apr 1793
2013-May 1881
2013-Jun 1956
2013-Jul 2071
2013-Aug 2150
2013-Sep 2233
2013-Oct 2406
2013-Nov 2408
2013-Dec 2486
2014-Jan 2649
2014-Feb 2860
2014-Mar 3014

This plugin executes a groovy script in the Jenkins 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.

Usage

The groovy script can use the variable manager, which provides access to the following objects and methods:

  • 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.  For Colors supported, Google "html color names".
  • addBadge(icon, text) - puts a badge with the given icon and text. In addition to the 16x16 icons offered by Jenkins, groovy-postbuild provides the following icons:

    - completed.gif
    - db_in.gif
    - db_out.gif
    - delete.gif
    - error.gif
    - folder.gif
    - green.gif
    - info.gif
    - red.gif
    - save.gif
    - success.gif
    - text.gif
    - warning.gif
    - yellow.gif

  • addBadge(icon, text, link) - like addBadge(icon, text), but the Badge icon then actually links to the given link (since 1.8)
  • addInfoBadge(text) - puts a badge with info icon and the given text.
  • addWarningBadge(text) - puts a badge with warning icon and the given text.
  • addErrorBadge(text) - puts a badge with 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 Jenkins. 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://your-jenkins/job/your-job/build-number/parent/parent/plugin/groovy-postbuild/removeBadges
  • remove all summaries:
    • http://your-jenkins/job/your-job/build-number/parent/parent/plugin/groovy-postbuild/removeSummaries
  • remove the badge with the given index:
    • http://your-jenkins/job/your-job/build-number/parent/parent/plugin/groovy-postbuild/removeBadge?index=idx
  • remove the summary with the given index:
    • http://your-jenkins/job/your-job/build-number/parent/parent/plugin/groovy-postbuild/removeSummary?index=idx

Example 1

The script below puts a warning badge and mark the build as unstable if it detects that deprecated methods were used.

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()
}

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.

if("true".equals(manager.build.buildVariables.get("storeToDB"))) {
    manager.addBadge("db_in.gif", "Stored to DB")
}

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.

if(manager.build.result.isBetterOrEqualTo(hudson.model.Result.UNSTABLE)) {
    manager.addShortText("v${manager.build.buildVariables.get('version')}")
}

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.

def matcher = manager.getLogMatcher(".*Total time: (.*)\$")
if(matcher?.matches()) {
    manager.addShortText(matcher.group(1), "grey", "white", "0px", "white")
}

Example 5

For badges, you can choose from the 16x16 icons provided by Jenkins or from the icons provided by this plugin. Some of the names of groovy-postbuild icons conflict with the names of Jenkins 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 Jenkins icon. You can also use icons provided by other plugins (e.g. Green Balls), by specifying the complete path to the icon.

manager.addBadge("star-gold.gif", "icon from Jenkins")
manager.addBadge("yellow.gif", "icon from groovy-postbuild plugin")
manager.addBadge("/images/16x16/yellow.gif", "icon from Jenkins")
manager.addBadge("/plugin/greenballs/16x16/green.gif", "icon from greenballs plugin")

Example 6

The script below displays on the build summary page all classes that use Sun proprietary API.

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)
}

Example 7

The script below removes all badges and summaries from previous builds.

currentBuildNumber = manager.build.number
for(i=1; i<currentBuildNumber; i++) {
    if(manager.setBuildNumber(i)) {
        manager.removeBadges()
        manager.removeSummaries()
    }
}

Example 8

The script below marks the running build as deployed and the previous build as undeployed.

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")
}

Example 9 (thanks to Ken Bertelson)

The script below changes the description of the first failed test.

def tr = manager.build.testResultAction.result
def cr = tr.failedTests.get(0)
cr.description = "My CaseResult desc"

Changelog

Version 1.8 (August 22, 2012)

  • JENKINS-13024 Error in log indicating a missing descriptor
  • added clickable badge
  • Changed required Jenkins version to 1.466

Version 1.7 (May 2, 2012)

Version 1.6

  • JENKINS-9383 security - restrict access to internal objects

Can use now additional classpath for groovy postbuild scripts to have them in a central location. Scriptler Plugin?

Version 1.5 (November 5, 2011)

  • Remove a html tag from the config page which causes an error on IE 7 (JENKINS-10079)

Labels

Edit
plugin-post-build plugin-post-build Delete
plugin-groovy-related plugin-groovy-related Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Add Comment