How do I access $BUILD_LOG in a Jenkins pipeline, or is there a better way of getting the log output?
Going off of this answer, I've been trying to access the $BUILD_LOG environment variable, but when I try
echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"
the build errors out:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: unexpected token: BUILD_LOG # line 11, column 29.
echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"
And if I try
echo "$BUILD_LOG"
I get this error:
groovy.lang.MissingPropertyException: No such property: BUILD_LOG for class: groovy.lang.Binding
What am I doing wrong? Or is this the wrong way to access the printed output?
I had the same problem with declarative pipelines and a step like:
emailext(
subject: "foo",
to: "bar",
body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Console output (last 250 lines):<hr><pre>${BUILD_LOG}</pre></p>"""
I had email ext plugin installed.
The solution was to escape the $-sign of the the macro that should be expanded by the plugin, like so:
...
<p>Console output (last 250 lines):<hr><pre>\${BUILD_LOG}</pre></p>"""
...
And be sure to use double quotes.
This way groovy (?) will first expand all the environment variables, and leaves the escaped variables to be dealt with by email ext plugin.
Still haven't found a solution to use BUILD_LOG parameter in a pipeline job with emailext plugin.
As a small solace, I found a workaround to access build log in another way:
currentBuild.rawBuild.getLog(15)
Where 15 is a number of last log lines I want to show.
The example is:
emailext attachLog: true,
body: "Build failed" +
"<br> See attached log or URL:<br>${env.BUILD_URL}" +
"<br><br> <b>The end of build log is:</b> <br>" +
currentBuild.rawBuild.getLog(15).join("<br>"),
mimeType: 'text/html',
subject: "Build failed",
to: 'myemail#somedomain.com'
Note that you have to approve a few script signatures at the In-Process Script Approval in order to allow usage of this function.
From the answer you linked the BUILD_LOG variable is set by the email-extension plugin. Do you have this configured correctly as this may be your issue.
Related
After running jenkins job, in console I am facing warning message as,
"Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure"
My code looks like,
steps. withCredentials([
steps.string(
credentialsld: 'git-oauth-token-read',
variable: 'GitOauthToken'
)
})
{
steps.sh(returnStdout: false,
script:
"git remote add mainline https://xxx:${new Jobs().getToken()}#github.com/;"
)
}
Here,
new Jobs().getToken() - returns the env.GitOauthToken
I tried using different combinations of single and double quotes to the script, but I am not able to remove this warning.
Any help would be appreciated.
Thank you!
Upgrade the Jenkins to 2.278, after upgrade, if build
error, the content of email show:
$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
Check console output at $BUILD_URL to view the results.
It will not display the real value, but I can find them
in the 'Environment variables ', like:
BUILD_CAUSE MANUALTRIGGER
BUILD_CAUSE_MANUALTRIGGER true
BUILD_DISPLAY_NAME #35
BUILD_ID 35
BUILD_NUMBER 35
BUILD_TAG jenkins-trigger-test-35
BUILD_URL http://192.168.10.99:8080/job/trigger-test/35/
...
then I upgrade the Email Extension Plugin to 2.80, but
still with no lucky.
Does anyone know what's wrong?
Thanks a lot.
The solution is to update Token Macro to 2.14 or later
I have a Jenkins job, which do Git syncs and build the source code.
I added and created a "Post build task" option.
In 'post build task', I am searching for keyword "TIMEOUT:" in console output (this part is done) and want to declare job as Failed and Aborted if keyword matches.
How can I raise / declare the Job as Aborted from batch script if keyword matches. Something like echo ABORT?
It is easier if you want mark it as "FAIL"
Just exit 1 will do that.
It is tricky to achieve "Abort" from post build task plugin, it is much easier to use Groovy post build plugin.
The groovy post build provide rich functions to help you.
Such as match function:
def matcher = manager.getLogMatcher(".*Total time: (.*)\$")
if(matcher?.matches()) {
manager.addShortText(matcher.group(1), "grey", "white", "0px", "white")
}
Abort function:
def executor = build.executor ?: build.oneOffExecutor;
if (executor != null){
executor.interrupt(Result.ABORTED)
}
Br,
Tim
you can simply exit the flow and raise the error code that you want:
echo "Timeout detected!"
exit 1
Jenkins should detect the error and set-up the build as failed.
The error code must be between 1 and 255. You can chose whatever your want, just be aware that some code are reserved:
http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
You can also consider using the time-out plugin:
https://wiki.jenkins.io/display/JENKINS/Build-timeout+Plugin
And another option is to build a query to BUILD ID URL/stop. Which is exactly what is done when you manually abort a build.
echo "Timeout detected!"
curl yourjenkins/job_name/11/stop
Given the following pipeline:
stages {
stage ("Checkout SCM") {
steps {
checkout scm
sh "echo ${CHANGE_AUTHOR_EMAIL}"
sh "echo ${CHANGE_ID}"
}
}
}
Why do these variables fail to resolve and provide a value?
Eventually I want to use these environment variables to send an email and merge a pull request:
post {
failure {
emailext (
attachLog: true,
subject: '[Jenkins] $PROJECT_NAME :: Build #$BUILD_NUMBER :: build failure',
to: '$CHANGE_AUTHOR_EMAIL',
replyTo: 'iadar#...',
body: '''<p>You are receiving this email because your pull request was involved in a failed build. Check the attached log file, or the console output at: $BUILD_URL to view the build results.</p>'''
)
}
}
and
sh "curl -X PUT -d '{\'commit_title\': \'Merge pull request\'}' <git url>/pulls/${CHANGE_ID}/merge?access_token=<token>"
Oddly enough, $PROJECT_NAME, $BUILD_NUMBER, $BUILD_URL do work...
Update: this may be an open bug... https://issues.jenkins-ci.org/browse/JENKINS-40486 :-(
Is there any workaround to get these values?
You need to be careful about how you refer to environment variables depending on whether it is shell or Groovy code, and how you are quoting.
When you do sh "echo ${CHANGE_ID}", what actually happens is that Groovy will interpolate the string first, by replacing ${CHANGE_ID} with the Groovy property CHANGE_ID, and that's where your error message is from. In Groovy, the environment variables are wrapped in env.
If you want to refer to the environment variables directly from your shell script, you either have to interpolate with env, use single quotes, or escape the dollar sign. All of the following should work:
sh 'echo $CHANGE_ID'
sh "echo \$CHANGE_ID"
sh "echo ${env.CHANGE_ID}"
For anyone who may come across this, these variables are available only if the checkbox for Build origin PRs (merged with base branch) was checked (this is in a multi-branch job).
See more in this other Jenkins issue: https://issues.jenkins-ci.org/browse/JENKINS-39838
I am running an Automated test suite on jenkins and I am able to get the status of the job using Email ext plugin. I want to include only some part of build log in email is there any way that I can do that using groovy syntax? Any help would be appreciated!
def notifyFailed() {
emailext (
subject: "FAILED : Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: '''<p><font size="8" color="red">Build Failure!</font></p>
<p>Check console output at "<a href='${BUILD_URL}consoleText'>${JOB_NAME} [${BUILD_NUMBER}]</a>"</p>
${BUILD_LOG_REGEX, regex="^.*?*****.*?$", linesBefore=0, linesAfter=999, maxMatches=10, showTruncatedLines=false,escapeHtml=false}''',