Jenkins - Add a dynamic label to a build

378 views
Skip to first unread message

Veena Thomas

unread,
Nov 28, 2018, 10:30:40 AM11/28/18
to Jenkins Users

I am trying to achieve the following task in Jenkins: 1) Build a maven project 2) When running the test cases I print certain messages to the console output 3) Parse the console output of the build and determine if certain patterns exist in the output 4) If the pattern exists I want to label the build with a specific string

I have achieved steps 1-3. I am not able to create a dynamic label and tie it to a build. I have a Groovy script that parses the console output and determines if the pattern exists in the build's output.


Bamboo provides this feature to label a build based on regular expressions present in the build's console output. Link - https://confluence.atlassian.com/bamboo0606/using-bamboo/jobs-and-tasks/configuring-jobs/configuring-miscellaneous-settings-for-a-job/configuring-automatic-labeling-of-job-build-results


I have gone through various existing Jenkins plugins but have not been successful in achieving this functionality. Is there a plugin to achieve this functionality or can I add additional lines in the Groovy script to create a dynamic build label. Any help is appreciated.

Suresh Parimi

unread,
Nov 28, 2018, 11:55:23 AM11/28/18
to jenkins...@googlegroups.com
Veena,

Interestingly I am also looking for the same solution since this evening. Do you mind sharing how you achieved step 3. How did you parse the jenkins cosole output. 

I am haing robot framework to execute test cases using jenkins. 

my task is: label build as smoke if ghe tag= Smoke tests passed to the script execution command.

Anyone have any ideas/experience with this?

Thank you,
Suresh

--
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/aa8c942b-c370-48dc-b087-23dafff17b42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam von Nieda

unread,
Nov 28, 2018, 2:29:41 PM11/28/18
to jenkins...@googlegroups.com

   Suresh, can’t help you with #4, but for #3, look at “Log Parser” plugin. It’s specifically for parsing the build log. 

--
Adam vonNieda




Björn Pedersen

unread,
Nov 29, 2018, 12:40:52 AM11/29/18
to Jenkins Users
Hi,

you probably should take a look at the Badge Plugin (together with Groovy Post Build plugin probably) for adding the labels. Org use the build Name Setter plugin to modify the build name if that matches your build better.

Björn

Veena Thomas

unread,
Nov 29, 2018, 5:19:01 AM11/29/18
to Jenkins Users
Suresh,
I added the Groovy plugin and included a Groovy script as a part of the Build Post Steps.
The groovy script goes through the console output and checks if a particular string is present.

Example of the script:

import jenkins.model.*
JOB_NAME = "Maven Demo"
CHECK_STRING = "IllegalArgumentException"

def job = Jenkins.instance.items.find { it.name == JOB_NAME }
def currentBuild = Thread.currentThread().executable
  def log = currentBuild.log
  if (log.contains(CHECK_STRING)) {
    println "${job.name}:"
    println currentBuild
  }

Right now this script just prints the job name and current build name to the console.

Veena Thomas

unread,
Nov 29, 2018, 11:32:42 AM11/29/18
to Jenkins Users
Hi Björn,
Thank you for your input. I am now able to add badges. Any idea if it is possible to link similar badges. If I click on a badge can I get the list of all builds that contain the selected badge?

Harry G.

unread,
Nov 30, 2018, 11:13:55 AM11/30/18
to Jenkins Users
Hi Veena,

good news: it doesn't have to be as complex as your example.
You don't need to search for the build object. It is already predefined.
In System Groovy build step it is 'build'.
In Grovy Postbuild plugin there's a 'manager' object and the current build run is 'manager.build'
You should have a deeper look at the docs of the plugins.

Here are some examples from one of my Jenkins talks:
2 of them use badges, so they work with Groovy Postbuild only.
See comments in the examples.

Have fun,

Harald
Reply all
Reply to author
Forward
0 new messages