Jenkins 中是否有一个选项可以通过传递一个键并在松弛通道中 post 从控制台输出中找到特定值?

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?

我目前正在使用
Slack Notification Plugin(2.18) 并与 jenkins 集成,它与默认功能配合得很好。

但是,我试图通过传递一个键从作业的控制台输出中找到一个特定的值。例如键是 "Unique_ID" ,我需要从控制台输出中获取它的值,并在松弛通道中 post 它。

控制台输出如下所示:

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

基本上,我想在构建 succeeded/failed 时通过在作业配置中传递 Unique_ID 来获取松弛通道中的值 12uy87tg

谁能帮我解决这个问题,或者让我知道 jenkins 中是否有一个选项可以在 slack 频道中 post 它。

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

但是上面的代码给出了下面的错误

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.dgm3.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.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

您可以使用 groovy 脚本来完成。

首先,下载并安装 Groovy plugin for Jenkins,然后在作业中添加一个步骤 "Execute system Groovy script"。

脚本将是这样的:

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