Why Jenkins is failing after executing an external windows command line? - jenkins

I have to create a job in my local Jenkins installation where it executes a SonarQube analysis and then calls a command-line program which searches for duplicated lines of code. However, when I execute the latter command (cpd), it runs okay since it outputs correctly in a external file, but Jenkins still points out it as an error like this:
E:\BASE_DIR>cpd --minimum-tokens 100 --files "E:\BASE_DIR\Project_Folder" --language java 1>>"E:\BASE_DIR\Project_Folder\CPD_CLIG.txt"
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I've tried to create another script which calls that command but I've got the same result.
Any suggestions on how to handle this? Any work-around would be very helpful.

Simple and short answer to your question is
Please add following line into your "Execute shell" Build step.
"#!/bin/sh"
Now let me explain you the reason why we require this line for "Execute Shell" build job.
By default Jenkins take "/bin/sh -xe" and this means -x will print each and every command.And the other option -e, which causes shell to stop running a script immediately when any command exits with non-zero(when any command fails) exit code.
So by adding the "#!/bin/sh" will allow you to execute with no option.
please refer : How/When does Execute Shell mark a build as failure in Jenkins?

I didn't find a proper solution for my issue but I realized that I can use a Jenkins plugin for static code analysis (DRY Plugin and PMD Plugin) to adress my problem.
Thanks you all.

Related

Need to run a command if the jenkins build stuck

In the Jenkins "Abort the build if it's stuck" feature, if a time out happens I want to run a powershell command, instead of aborting or failing the build. Is it possible to accomplish this?
under system configuration-> configure system, found below option:
once enabled this option, "perform build step option" will be available under "time-out action" where we can execute any script or cmd.
Perhaps you could set a "Time-out variable" in the "Abort the build..." menu. Then run a post-build script using the PostBuildScript plugin that checks that env var and takes action accordingly. Haven't tried it.

Execute shell giving error in Jenkins Freestyle job

Whenever I am trying to do execute shell command in freestyle job in Jenkins, however when I try to do same thing while from pipeline, it is running successful.
Following is the error which I am getting in freestyle job:
It seems that the drive is full and there is no space left to execute this shell command. See this log:
So what you should do is to make sure that there is enough space on the drive where you run the script/command is available. Maybe you could switch to another partition or delete temporary or unused files.

How to start Owasp zap server(exe or jar) from jenkins

Actually, the main issue is if I start the server then my next commond will never trigger as it always running as zap server in listening mode.
Can I run two command line in Jenkins. I have added 2 "Execute Windows batch command" still nothing works. I have added the image in same thread
I have tried by creating a batch file
cd /
cd C:\Program Files\OWASP\Zed Attack Proxy
start java -jar zap-2.6.0.jar
I am getting error as below after using above batch file
Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
https://wiki.jenkins.io/display/JENKINS/Spawning+processes+from+build
I have also use command line arugument directly in "Execute window batch command" like:-
java -jar zap-2.6.0.jar
But the UI of zap is not starting
I have also tried "Windows Exe Runner Plugin"
https://wiki.jenkins.io/display/JENKINS/Windows+Exe+Runner+Plugin
But jenkins not allowing me to put an exe name in configuration. Looks like a bug of jenkins.
I have also tried by adding zap in environmental variable but that also not working.
Now I am out of idea.
The issue is if I am triggering zap.bat it will do not allow another command to run forward as below which is in my batch:-
Additionally, UI of zap is not open as it is open after direct clicking on zap.bat file
I have added 2 "Execute Windows batch command" still nothing works
Any suggestions will be welcome
Simple - dont start it from the jar!
Start it using the zap.sh or zap.bat scripts we provide as part of the installation :) You'll also probably want to use the -daemon flag.
Or you can use the official ZAP jenkins plugin: https://wiki.jenkins.io/display/JENKINS/zap+plugin
I have resolve this issue by creating two jobs in jenkins.
The main job trigger the first job.
Follow the steps :-
Go to the configure section of main job
Now add "Trigger/call builds on other projects" from Build option
Add the project name of zombie job
Note :- uncheck the checkbox of "Block until the triggered projects finish their builds".

How can I add a Jenkins post action to execute two different scripts once the build successed or failed exclusively?

I want to execute a shell scripts if the Jenkins job build successful or another scripts if the Jenkins job build failed.
I added the post build task plugin, but it seems only can execute a shell in all status or just successful status, cannot specify another shell script to be run once build failed, the two scripts should be run exclusively.
are there anyone can help me on this?
use Post Build Task plugin for conditional steps
I use Conditional BuildStep Plugin and Text Finder Run Condition Plugin to execute steps when the build fails.
But there's a limitation with this method: You have to find a text that is displayed ONLY when your build fails.
See the pictures below:
I add exit 0 at the end of the script to force the build to be successful so that the program can execute the next step, which is Conditional steps(multiple).
You will see Execute script after a failed build. being displayed on console output when the job fails.
This is the execution result.
You will also need to find a text that is displayed ONLY when the first step succeeds so that you can trigger another script.

YSlow Phantomjs and Jenkins jobs failing, but analysis successful

I'm going through the tutorial on YSlow and Phantom js in Jenkins here: http://yslow.org/phantomjs/
Everything appears to be working great except the Jenkins builds are failing. I think this is due to the violations that YSlow is finding (6 for the particular site I am measuring). I'd rather have the build be successful (or unstable) vs. failed though
Is that possible with this or will I have to resort to something like the postgroovy or text finder plugin?
This is the console output:
phantomjs.exe yslow.js -i grade -t 50 --format junit http://www.somesite.com 1>yslow.xml
D:\Apps\Jenkins\workspace\YSlow_Test>exit 6
Build step 'Execute Windows batch command' marked build as failure
Thanks
Any non-zero exit code at the end of your Execute Windows batch command build step will result in build step being marked as failure.
To have the build step marked as success, you need an exit code of 0. I don't know anything about "yslow" or "phantomjs" and why they are giving you exit code of non-zero, but from "batch" side of things, you need only write exit 0 at the end of your build step if you want to overwrite the exit code of your phantomjs command.
You can then use Text Finder plugin to parse the console log and mark build as unstable when certain conditions are met.
Reading over this answer, Configuring yslow on Jenkins looks like you need TAP plugin to have the functionality of unit testing marking the build as unstable automatically

Resources