简体   繁体   中英

Jenkins Groovy to extract Regex from the Current build log and call REST API?

I would like to add a build step that summarizes my build, using Groovy to read the build log that was generated to date.

I've seen several other questions on SO about related topics but not all of them run, I'm a bit confused on the API docs, and overall I can't seem to get this exact thing running.

Below is the code/resultant failure I have currently.

I have a few questions, if it is ok to put them all together here;

1.Is it safe to test things in the console window? Or, stated differently, when can it be that something works in the /script Groovy console editor window, but it will fail as a Groovy build step? (I think the API differs for the two but I'm not clear how.)

2.Is there a repo anywhere of Groovy Jenkins script examples?

3.How can I do the following?

  • Read the console log.
  • Parse it with regex for words of interest, eg "step#2 success".
  • Rearrange those words into a nice string with some newlines.
  • Call our internal REST API to submit the results.

thank you so much! Anne

//Groovy command from SO Post#23139654
def log = manager.build.logFile.text
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/

//From there you can extract the matches or as in my case further parse the match:

def total = summary[0] =~ /\d+ tests/

Result includes;

ERROR: Build step failed with exception
groovy.lang.MissingPropertyException: No such property: manager for class: Script1

Here are my answers.

1.Groovy console vs Groovy build step differ as per Jenkins Packages on Groovy Classpath?

2.Examples are available from a user in 2011 and unidentified dates on the wiki: "Jenkins, Groovy System scripts (and Maven) | Code Snippets" https://mriet.wordpress.com/2011/06/23/groovy-jenkins-system-script/ and https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

3.To parse the console log and grep outputs , simply input into the web box provided as input for the Editable Email plugin [4] post-build step. Do NOT use dollar-curlyBrace syntax: use simple dollar-variable or dollar-paren syntax as shown here which is my first crack at the 'Default Content'.

STATUS=$BUILD_STATUS  
$DEFAULT_CONTENT
GIT Changelog , Revision = $GIT_REVISION
$CHANGES 
LOG-SNIPPETS: Regex Hits/Rules for words that give Unit Test Summaries, Error, Failure, etc =
$BUILD_LOG_REGEX( regex="^.*?BUILD FAILED.*?$", linesBefore=0, linesAfter=10, maxMatches=5, showTruncatedLines=false, escapeHtml=true)

3B.To call the REST plugin, that would need to be done in a separate step, so for now I did not do that.

I had not properly understood the Email-Ext (aka "Editable Email Notification") plugin - that is why I was trying to do this directly in Groovy.

4.[] Email-ext plugin - Jenkins - Jenkins Wiki ; ; https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM