Jenkins中是否可以通过传递密钥并将其发布到松弛通道中来从控制台输出中查找特定值?

时间:2019-03-07 19:08:51

标签: jenkins jenkins-plugins slack slack-api slack-commands

我当前正在使用
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

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

在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

0 个答案:

没有答案