How can I tail the console output for a failed pipeline build?

76 views
Skip to first unread message

Dallas Clement

unread,
Aug 31, 2017, 12:25:05 PM8/31/17
to Jenkins Users
I am working with multibranch pipeline builds and I want to be able to tail the end of the console output to include in build failure email notifications.  I can see where the log file is located in the filesystem under 

/var/lib/jenkins/jobs/<name of project>/branches/<name of branch>/builds/5

The build log appears to have the console output embedded in it.  But there appears to be a whole lot more also.  How can I extract / render just the console output?


xwyxw

unread,
Sep 4, 2017, 7:14:56 AM9/4/17
to Jenkins Users
Blueoncean, split your script in  functions and  stages and you will get a good understanding of what is actually happening on every stage (clone, build, fail, etc). Also using groovy and try/catch will help too

Dallas Clement

unread,
Sep 4, 2017, 11:27:52 AM9/4/17
to Jenkins Users
I've done all that.  What I want is to extract the last portion of the actual console output for a build that has failed so that I can embed it in the body of an email notification to indicate where the build has failed.  I'm trying to avoid attaching the whole build log.  I've been able to locate the build log, but I don't know how to extract the raw console output from it.

Dallas Clement

unread,
Sep 5, 2017, 3:42:11 PM9/5/17
to Jenkins Users
Answering my own question in case it may help someone.  I put this in my "failure" post action section of my Jenkinsfile to extract the last 100 lines of the build log.  I also filtered out the post actions output and the leading rubbish on each line of the actual build log.

jobName = "${env.JOB_NAME}"
tokens = jobName.split('/')
jobDir = tokens[0]
logFile = "${env.JENKINS_HOME}/jobs/${jobDir}/branches/${env.JOB_BASE_NAME}/builds/${env.BUILD_NUMBER}/log"
command = $/tail -100 ${logFile} | sed '/Post Actions/q' | sed '/\[Pipeline\]/d' | sed -e 's/^.*\[0m//g'/$
logSnippet = sh(script: command, returnStdout: true)
Reply all
Reply to author
Forward
0 new messages