Search code examples
jenkinsjenkins-pluginsslackslack-apislack-commands

Is there an option in Jenkins to find a particular value from a console output by passing a key and post it in slack channel?


I am currently using
Slack Notification Plugin(2.18) and integrated with jenkins and it works well with the default functionalities.

But, I am trying to find a particular value from console output of a job by passing a key. For example key is "Unique_ID" and I need to get its value from console output and post it in slack channel.

console output will look like:

00:23:53 Started by user user1 00:24:23 INFO: Unique_ID: 12uy87tg Entity: com.net.qa.rest.domain

Basically, I want to get the value 12uy87tg in slack channel when the build succeeded/failed by passing Unique_ID in the job config.

Can someone help me on this how to do it or let me know if there is an option in jenkins to post it in slack channel.

Tried the below code in Groovy Postbuild:

import java.io.BufferedReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Define the regex to extract the value you want
pattern = Pattern.compile("^.*Unique_ID: (.*) Entity.*");

uniqueId = null;
reader = null;

// Open the log of the build in a buffer
try{
  reader = new BufferedReader(manager.build.getLogReader())

  // Read the buffer and look for a match of the regex
  while ((line = reader.readLine()) != null) {
    Matcher matcher = pattern.matcher(line);
    if (matcher.find()) {
       // If there is a match, the value is stored and the research is over
       uniqueId = matcher.group(1);
       break;
    }
  }
}
finally{
  // Close the buffer
  if (reader != null) {
    reader.close();
  }
}
// If a value has been found, it is passed to the slack plugin
if (uniqueId != null){
  // Get the instance of the slack plugin
def slack = manager.build.project.publishers.find{ k, v -> v.class.name == 'jenkins.plugins.slack.SlackNotifier' }?.value
  // Set the custom message in Slack
 slack.setIncludeCustomMessage(true)
 slack.setCustomMessage("Unique_ID: "+uniqueId)
}

But the above code gave the below error

16:36:36 ERROR: Failed to evaluate groovy script.
16:36:36 groovy.lang.MissingMethodException: No signature of method: Script1$_run_closure1.call() is applicable for argument types: (org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder) values: [org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder@5df675b1]
16:36:36 Possible solutions: any(), any(), any(groovy.lang.Closure), each(groovy.lang.Closure), any(groovy.lang.Closure), each(groovy.lang.Closure)
16:36:36    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:286)
16:36:36    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
16:36:36    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
16:36:36    at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:46)
16:36:36    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
16:36:36    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
16:36:36    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
16:36:36    at org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker.invoke(BooleanReturningMethodInvoker.java:51)
16:36:36    at org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper.call(BooleanClosureWrapper.java:53)
16:36:36    at org.codehaus.groovy.runtime.DefaultGroovyMethods.find(DefaultGroovyMethods.java:3934)
16:36:36    at org.codehaus.groovy.runtime.dgm$193.invoke(Unknown Source)
16:36:36    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
16:36:36    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
16:36:36    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
16:36:36    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
16:36:36    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
16:36:36    at Script1.run(Script1.groovy:35)
16:36:36    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
16:36:36    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
16:36:36    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
16:36:36    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:350)
16:36:36    at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:380)
16:36:36    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
16:36:36    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
16:36:36    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
16:36:36    at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
16:36:36    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
16:36:36    at hudson.model.Run.execute(Run.java:1844)
16:36:36    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
16:36:36    at hudson.model.ResourceController.execute(ResourceController.java:97)
16:36:36    at hudson.model.Executor.run(Executor.java:429)
16:36:41 [Slack Notifications] found #8905 as previous completed, non-aborted build
16:36:41 [Slack Notifications] will send OnSuccessNotification because build matches and user preferences allow it
16:36:41 Finished: SUCCESS

Solution

  • You can do it with a groovy script.

    First, download and install the Groovy plugin for Jenkins, and then add a step "Execute system Groovy script" in your job.

    The script will be something like this:

    import java.io.BufferedReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    // Define the regex to extract the value you want
    pattern = Pattern.compile("^.*Unique_ID: (.*) Entity.*");
    
    uniqueId = null;
    reader = null;
    
    // Open the log of the build in a buffer
    try{
      reader = new BufferedReader(build.getLogReader())
    
      // Read the buffer and look for a match of the regex
      while ((line = reader.readLine()) != null) {
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
           // If there is a match, the value is stored and the research is over
           uniqueId = matcher.group(1);
           break;
        }
      }
    }
    finally{
      // Close the buffer
      if (reader != null) {
        reader.close();
      }
    }
    
    // If a value has been found, it is passed to the slack plugin
    if (uniqueId != null){
      // Get the instance of the slack plugin
      def slack = build.project.publishers.find{ k, v -> v.class.name == 'jenkins.plugins.slack.SlackNotifier' }?.value
      // Set the custom message in Slack
      slack.setIncludeCustomMessage(true)
      slack.setCustomMessage("Unique_ID: "+uniqueId)
    }