在Jenkins中是否有一个选项可以通过传递一个键并将其发布到slack通道来从控制台输出中查找特定值?

在Jenkins中是否有一个选项可以通过传递一个键并将其发布到slack通道来从控制台输出中查找特定值?,jenkins,jenkins-plugins,slack,slack-api,slack-commands,Jenkins,Jenkins Plugins,Slack,Slack Api,Slack Commands,我目前正在使用 Slack Notification插件(2.18)并与jenkins集成,可与默认功能配合使用 但是,我试图通过传递一个键从作业的控制台输出中找到一个特定的值。例如,键是“Unique_ID”,我需要从控制台输出获取它的值,并将其发布到slack通道中 控制台输出如下所示: 00:23:53由用户user1启动 00:24:23信息:唯一_ID:12uy87tg实体:com.net.qa.rest.domain 基本上,当构建通过传递作业配置中的Unique_ID成功/失败时,

我目前正在使用
Slack Notification插件(2.18)并与jenkins集成,可与默认功能配合使用

但是,我试图通过传递一个键从作业的控制台输出中找到一个特定的值。例如,键是“Unique_ID”,我需要从控制台输出获取它的值,并将其发布到slack通道中

控制台输出如下所示:

00:23:53由用户user1启动
00:24:23信息:唯一_ID:12uy87tg实体:com.net.qa.rest.domain

基本上,当构建通过传递作业配置中的
Unique_ID
成功/失败时,我希望在slack通道中获取值
12uy87tg

有人可以帮助我在这方面如何做,或者让我知道,如果有一个选项在詹金斯张贴在slack频道

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

您可以使用groovy脚本来完成

首先,下载并安装,然后在作业中添加步骤“执行系统Groovy脚本”

脚本如下所示:

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

谢谢@SmartTom,我可以在Groovy Postbuild中的post-build操作部分中执行吗?@Syed是的,您可以将此脚本与一起使用。但是您需要将
build.getLogReader()
替换为
manager.build.getLogReader()
并将
build.project.publisher
替换为
manager.build.project.publisher
。谢谢@SmartTom,在完成所有这些操作后,我收到了这个错误:
15:56:30错误:无法评估groovy脚本。15:56:30 groovy.lang.MissingMethodException:没有方法的签名:Script1$\u run\u closure1.call()适用于参数类型:(org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder)值:[org.jvnet.hudson.plugins.groovypostbuild]。GroovyPostbuildRecorder@6d32604d]15:56:30可能的解决方案:any(),any(),any(groovy.lang.Closure),each(groovy.lang.Closure),any(groovy.lang.Closure),each(groovy.lang.Closure)
您有什么想法吗?即使使用“执行系统groovy脚本”。我得到了同样类型的异常。你能在这里帮忙吗<代码>5:36:26错误:生成步骤失败,出现异常15:36:26 groovy.lang.MissingMethodException:没有方法的签名:Script1$\u run\u closure1.call()适用于参数类型:(jenkins.plugins.slack.SlackNotifier)值:[jenkins.plugins.SlackNotifier]。SlackNotifier@4ee0d9c4]15:36:26可能的解决方案:any(),any(),any(groovy.lang.Closure),each(groovy.lang.Closure)、any(groovy.lang.Closure)、each(groovy.lang.Closure)@Syed好的,根据您上次提供的信息,我已经能够重现您的问题。问题来自maven job和freestyle job在如何处理出版商列表方面的差异。因此,将
build.project.publisher.find
替换为
build.project.publisher.toMap().find
,它将解决您的问题。