Read Jenkins log in shell - jenkins

I have a requirement that I need to parse the Jenkins job after the completion to grep out the error log and send it in email to the team. Could someone of you suggest what plugin/option is available in Jenkins?

If you want to parse out the console output after the build steps, try using Groovy Postbuild Plugin.
You can use the manager.logContains function or the manager.build.logFile variable to get the console output.

Related

How to pass output from UFT test to Jenkins console

Does anybody experience or any idea with pass output from UFT test to Jenkins console?
I started a UFT test through Jenkins and I use *.mtbx files for pass parameters from Jenkins to test but now I need some data (for example order id or phone number) from test pass to Jenkins console.
Is there any option?
Thanks
Only alone option is to write a file in run time and enter your data in it.
At the end of exectution, you should call the same file through jenkins.
Regards,
Prem Padayachi

Jenkins: Access bitbucket payload in shell

According to the documentation of the bitbucket plugin for Jenkins (https://wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin) it should be possible to access the payload infos through the environment varaible $BITBUCKET_PAYLOAD.
However when in my build I run the command printenv, there is no environment variable called $BITBUCKET_PAYLOAD, and nothing related to it.
So it's impossible for me to access informations I need to configure my build.
You can trigger Jenkins with Generic Webhook Trigger instead.
Then you can create a variable everything having the JSONPath $. Then everything will resolve to the entire JSON payload. So you can have a shell script build step like:
echo $everything

How to configure Jenkins to report major errors to the administrator?

e.g. I have the following errors in jenkins.err.log. How can I make Jenkins email this type of errors to me?
caused by: com.microsoft.tfs.core.ws.runtime.exceptions.UnauthorizedException: Authorization failure connecting to 'http://vstsprodapp:8080/Services/v1.0/Registration.asmx' (authenticating as domain\user)
This isn't a feature that's built into Jenkins.
You could use whatever system you use for monitoring your servers, and check the logs on the Jenkins server for error messages.
There is also an idea suggested on the Jenkins wiki, which allows you to use a Groovy hook script to add a further Java logging handler to Jenkins. But again, even if you do this, you would still have to implement a custom solution to send notifications based on the log's contents.
From https://wiki.jenkins-ci.org/display/JENKINS/Logging:
Put the following Groovy script into a file called $JENKINS_HOME/init.groovy.d/extra_logging.groovy:
import java.util.logging.ConsoleHandler
import java.util.logging.LogManager
def logger = LogManager.getLogManager().getLogger("hudson.WebAppMain")
logger.addHandler (new ConsoleHandler())
This will just copy all log records generated by Jenkins and any plugins to the console.

Is it possible to access Jenkins build status in execute shell?

Is there a way to have a script being run from an execute shell step access the build status as well as other information about the build? (e.g. build number, start/end time, etc)
I need to update a wiki page with a script with the information about the build.
Thanks in advance!
Build status information gets saved into simple XML files. Take a look at
{jenkins-directory}/jobs/{job-name}/builds/{build-number}/build.xml. You can use symbolic links like lastSuccessfulBuild instead of specifying a build number if you want.
You'll find all information about the build there: build status, start time, duration, etc. Parse the XML file or just use grep in a bash script.
If you don't want to use the filesystem you can use Jenkins API.
But in any case, once you got a script that can determine build parameters and update your wiki page, you can put it into another Jenkins job and trigger it automatically (for example, with the BuildResultTrigger Plugin).
I'm using jenkins own API to get the build status while the job is running, which works like a charm. Be aware that i'm using JQ To parse the json response.
To get this to work simply add a shell script and execute the following command: BUILD_STATUS=$(curl --silent ${BUILD_URL}api/json | jq -r '.result')
Which results in the following:
While executing a build, Jenkins set environment variables you can use in your script.
also refer to the detailes on how to use it -
%VAR% in batch files and $VAR from inside the Jenkins job configuration page

Access Jenkins build log within build script

How would you go about accessing contents of the build (console) log from within a running build script?
I have a deploy script that runs, logs into a series of servers and runs scripts on those servers. I need to obtain certain output from some of those remote scripts and use them later in the build process and also in the completion email.
You can do something like this in the Post Build section, but I don't think you can do it earlier in the job. With the groovy post build plugin you can get information from the console log:
if(manager.logContains("text to find")) {
do something
}

Resources