Setting the description of a failed test in pipeline groovy

405 views
Skip to first unread message

Vincent Massol

unread,
Feb 6, 2017, 3:25:15 AM2/6/17
to Jenkins Users
Hi Jenkins masters,

I had a groovy postbuild script that I'm trying to migrate to pipeline groovy. 

It's setting the description of failed test (in order to embed the screenshot taken by our selenium tests in the jenkins failed test UI - For example: http://ci.xwiki.org/job/xwiki-enterprise-test-ui-7.4.x/org.xwiki.enterprise$xwiki-enterprise-test-ui/350/testReport/junit/org.xwiki.test.ui.appwithinminutes/WizardTest/testCreateApplication/ ).

I've googled a lot but couldn't find any starting point to convert this script:

 /**
   * This script attaches the screenshot of a failing Selenium test to the failed test's description.
   * The screenshot is preserved after the workspace gets cleared by a new build.
   */

 
def attachScreenshotToFailingTests() {
   
def channel = manager.build.workspace.channel;
   
def workspace = manager.build.workspace.toString();

   
def testResults = manager.build.testResultAction;
   
if (testResults == null) {
     
// No tests were run in this build, nothing left to do.
     
return;
   
}
   
   
// Go through each failed test in the current build.
   
def failedTests = testResults.getFailedTests();
   
for (def failedTest : failedTests) {
     
// Compute the test's screenshot file name.
     
def testClass = failedTest.getClassName();
     
def testSimpleClass = failedTest.getSimpleName();
     
def testExample = failedTest.getName();

     
def suiteResultFile = failedTest.getSuiteResult().getFile();
     
if (suiteResultFile == null) {
       
// No results available. Go to the next test.
       
continue;
     
}
     
     
// Compute the screenshot's location on the build agent.
     
def targetFolderPath = new hudson.FilePath(channel, suiteResultFile).getParent().getParent();
     
// The screenshot can have 2 possible file names and locations, we have to look for both.
     
// Selenium 1 test screenshots.
     
def imageAbsolutePath1 = new hudson.FilePath(targetFolderPath, "selenium-screenshots/${testClass}-${testExample}.png");
     
// Selenium 2 test screenshots.
     
def imageAbsolutePath2 = new hudson.FilePath(targetFolderPath, "screenshots/${testSimpleClass}-${testExample}.png");
     
// Determine which one exists, if any.
     
def imageAbsolutePath = imageAbsolutePath1.exists() ? imageAbsolutePath1 : (imageAbsolutePath2.exists() ? imageAbsolutePath2 : null);

     
// If the screenshot exists...
     
if (imageAbsolutePath != null) {
       
// Build a base64 string of the image's content.
       
def imageDataStream = imageAbsolutePath.read();
       
byte[] imageData = IOUtils.toByteArray(imageDataStream);
       
def imageDataString = "data:image/png;base64," + DatatypeConverter.printBase64Binary(imageData);

       
def testResultAction = failedTest.getParentAction();
       
def testResult = testResultAction.getResult();

       
// Build a description HTML to be set for the failing test that includes the image in Data URI format.
       
def description = "<h3>Screenshot</h3>";
        description
+= "<a href=\"" + imageDataString + "\"><img style=\"width: 800px\" src=\"" + imageDataString + "\" /></a>";

       
// Set the description to the failing test and save it to disk.
        testResultAction
.setDescription(failedTest, description);
       
// Note: the owner field is marked as deprecated and it might go away. It should be replaced by an instance of Run in the future,
       
// but FTM, I do not know how to access it. For the future, in case descriptions start not getting stored, this might cause it.
        testResultAction
.owner.save();
     
}
   
}
 
}

Any pointer would be much appreciated.

Thanks
-Vincent


Richard Ginga

unread,
Feb 6, 2017, 8:31:19 AM2/6/17
to jenkins...@googlegroups.com
Vincent, I am not a Jenkins master, but, you need to replace the "manager" variable. that is defined by groovy-postbuild. I "think" all you need is to use "currentBuild" as the variable to access build methods and "Jenkins.instance." to access FilePath.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dick Ginga
Build Engineer

Vincent Massol

unread,
Feb 6, 2017, 8:34:41 AM2/6/17
to jenkins...@googlegroups.com
Hi Richard,

> On 6 Feb 2017, at 14:31, Richard Ginga <rgi...@disruptorbeam.com> wrote:
>
> Vincent, I am not a Jenkins master, but, you need to replace the "manager" variable. that is defined by groovy-postbuild. I "think" all you need is to use "currentBuild" as the variable to access build methods and "Jenkins.instance." to access FilePath.

I’ve tried to use currentBuild but it’s wrapped and only offers a few methods. For example it doesn’t allow calling getWorkspace() nor getTestResultAction().

So right now I’m trying to find a way to get access to a https://github.com/jenkinsci/junit-plugin/blob/master/src/main/java/hudson/tasks/junit/TestResultAction.java instance.

(Previously it was manager.build.testResultAction)

Thanks
-Vincent
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Dick Ginga
> Build Engineer
> rgi...@disruptorbeam.com
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaUykt14Nrg1GuhYqy7H0Aaq%3D4QL42YO3%2B%3DQZq--%2B4H4iA%40mail.gmail.com.

Richard Ginga

unread,
Feb 6, 2017, 8:39:54 AM2/6/17
to jenkins...@googlegroups.com
ok, if you need to step through to build object, I use this code to eventually get the log of some other other job:

for (item in Jenkins.instance.getAllItems()) {
//
// find the correct job
//
if (item.getFullName() == SERVER_BUILD_NAME) {
 def job = item.getAllJobs() 
 //
 // get the last successful build object
 //
 def build = job[0].getLastSuccessfulBuild()
 //
 // get the build log from that build
 //
 def log = build.getLog(300)


> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Dick Ginga
> Build Engineer
> rgi...@disruptorbeam.com
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/D35787B8-4085-4EDA-94CE-BBE145D577CA%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

Vincent Massol

unread,
Feb 6, 2017, 8:52:38 AM2/6/17
to jenkins...@googlegroups.com
Thanks Richard,

That looks a bit expensive though since all I need is to find the test results for the **current** job only. I wonder if there isn’t a simpler way.

Thanks
-Vincent
> > To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> >
> > --
> > Dick Ginga
> > Build Engineer
> > rgi...@disruptorbeam.com
> >
> >
> > --
> > You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> > To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaUykt14Nrg1GuhYqy7H0Aaq%3D4QL42YO3%2B%3DQZq--%2B4H4iA%40mail.gmail.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/D35787B8-4085-4EDA-94CE-BBE145D577CA%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Dick Ginga
> Build Engineer
> rgi...@disruptorbeam.com
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaWcZqJeHQ%2B5eGWid4KHM1QYUxT66Vzwx9C0o4ZJgvVgmg%40mail.gmail.com.

Baptiste Mathus

unread,
Feb 16, 2017, 2:45:43 AM2/16/17
to jenkins...@googlegroups.com
Hi Vincent,
I think the first step to convert that kind of thing is probably to externalize it in a @NonCPS function to avoid potential issues while refactoring.
Also, probably disable temporarily sandbox while converting it.
There's access to the raw build from the wrapped one (currentBuild.rawBuild or so IIRC).

My 2 cents

> > To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> >
> > --
> > Dick Ginga
> > Build Engineer
> > rgi...@disruptorbeam.com
> >
> >
> > --
> > You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> > To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaUykt14Nrg1GuhYqy7H0Aaq%3D4QL42YO3%2B%3DQZq--%2B4H4iA%40mail.gmail.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/D35787B8-4085-4EDA-94CE-BBE145D577CA%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> Dick Ginga
> Build Engineer
> rgi...@disruptorbeam.com
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/D55E2C5F-29A8-4654-B984-A7079262D6FB%40gmail.com.

Vincent Massol

unread,
Feb 21, 2017, 7:38:52 AM2/21/17
to jenkins...@googlegroups.com
Hi Baptiste,

> On 16 Feb 2017, at 08:45, Baptiste Mathus <m...@batmat.net> wrote:
>
> Hi Vincent,
> I think the first step to convert that kind of thing is probably to externalize it in a @NonCPS function to avoid potential issues while refactoring.
> Also, probably disable temporarily sandbox while converting it.

Not sure I understand this. If I re-enable sandbox later on, why would the script continue to work?

> There's access to the raw build from the wrapped one (currentBuild.rawBuild or so IIRC).

Yes I noticed the access to the raw build but it’s clearly mentioned that this should not be done. And the mention is pretty adamant about it :)

So I was asking for what is the official and recommended way of accessing the “manager” variable.

Maybe the answer is that there’s no other option than using currentBuild.rawBuild?

Thanks
-Vincent
> > > To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> > > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> > >
> > >
> > > --
> > > Dick Ginga
> > > Build Engineer
> > > rgi...@disruptorbeam.com
> > >
> > >
> > > --
> > > You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> > > To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> > > To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> > > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaUykt14Nrg1GuhYqy7H0Aaq%3D4QL42YO3%2B%3DQZq--%2B4H4iA%40mail.gmail.com.
> > > For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/D35787B8-4085-4EDA-94CE-BBE145D577CA%40gmail.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> >
> > --
> > Dick Ginga
> > Build Engineer
> > rgi...@disruptorbeam.com
> >
> >
> > --
> > You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> > To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAL3PpaWcZqJeHQ%2B5eGWid4KHM1QYUxT66Vzwx9C0o4ZJgvVgmg%40mail.gmail.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> --
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/IJYJx6hXhBc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4-sm7S0YVTOpYUEWuLs4vbC-xq21zCMpneX6Qzq%2ButxQ%40mail.gmail.com.

jer...@bodycad.com

unread,
Feb 21, 2017, 11:49:22 AM2/21/17
to Jenkins Users
Add description on the side:

manager.addShortText("${APPLY_TAG}")

You can find a lot of property inside you syntax help page (change MY_HOST, MY_PIPELINE_JOB):

http://MY_HOST/Jenkins/job/MY_PIPELINE_JOB/pipeline-syntax/globals

this should with the properties of env/manager/currentBuild. the hudson/build object is inside the manager for example. As what is available on them after, you need to check the javadoc and try to make sense of it.

Hope this help,
Jerome

Vincent Massol

unread,
Feb 22, 2017, 8:43:37 AM2/22/17
to jenkins...@googlegroups.com
Hi Jerome,
I’m 100% sure that there was no “manager” variable that I could use in my pipeline script (I had tested that). But yesterday I’ve upgraded my jenkins plugins and when trying today (thanks to your email!), it worked this time! :) So this is probably a recent addition.

This is cool.

Thanks
-Vincent

> Hope this help,
> Jerome

Vincent Massol

unread,
May 31, 2017, 12:04:41 PM5/31/17
to jenkins...@googlegroups.com
For those wondering, the “manager” variable is added by relatively recent versions of the “Groovy Postbuild” plugin.

Thanks
-Vincent
Reply all
Reply to author
Forward
0 new messages