0

我目前正在使用
Slack Notification Plugin(2.18) 并与 jenkins 集成,它适用于默认功能。

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

控制台输出将如下所示:

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

基本上,我想通过传入作业配置来获取12uy87tg构建成功/失败时松弛通道中的值。Unique_ID

有人可以帮我解决这个问题,或者让我知道詹金斯是否可以选择将其发布到松弛频道中。

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

1 回答 1

1

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

首先,下载并安装Jenkins 的 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)
}
于 2019-03-11T11:09:27.707 回答