How to find if a Job fails in jenkins - shell

I am really new to Jenkins and shell scripts, I just want to know if I have a set of commands which are configured in a build, is there any simple way to know if the job failed or the set of commands terminated to due some reason?
For example as under configure job if we have a set of shell commands as below can i embed them in on if statement as below :
if [ ${jenkinsHome}/doThis.sh > andThis.sh
${jenkinsHome}/nowDOThis.sh > ${workpsace}/commit.sh
${workpsace}/test.sh test
]
then
echo "build passed"
else
echo "failed"
fi
Can i do something like the above. please suggest.

Related

mvn command is not getting executed in cron

I want to execute maven command (mvn) through a shell script to be executed through cron.
My shell script
echo "setting the variables"
export M2_HOME=/Users/<XXXXX>/Downloads/apache-maven-3.5.2
export M2=/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre
export PATH=$PATH:/Users/<XXXXX>/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/<XXXXX>/Documents/Software/neo-java-web-sdk-3.54.23/tools:/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin:/usr/local/go/bin:/Applications/Privileges.app/Contents/Resources:JAVA_HOME/bin
echo "variables set"
{
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/bin/java -version > /Users/<XXXXX>/git/SimpleTests/org.saurav.simpletests/java.log
} || {
echo "java command failed"
}
{
/Users/<XXXXX>/Downloads/apache-maven-3.5.2/bin/mvn -version
} || {
echo "mvn command failed"
}
echo "Tests Executed"
output.log always print
setting the variables
variables set
mvn command failed
Tests Executed
So, it seems the mvn command execution fails.
Output of java command execution has been redirected to java.log but that prints empty. But it seems the java command execution is happening since the fallback echo statement is not printed here.
Best Regards,
Saurav
It seems the mvn file execution is a protected file execution.
With Mac Mojave the programs accessing the protected files have to be given full disk access
Please check here
https://apple.stackexchange.com/questions/378553/crontab-operation-not-permitted
https://support.intego.com/hc/en-us/articles/360016683471-Enable-Full-Disk-Access-in-macOS
After i followed the steps mentioned in the link 1, it started working.
Best Regards,
Saurav

Do manual build fail in Jenkins using shell script

I would like to mark a Jenkins build to fail on one scenario for example:
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
#Do Jenkins Build Fail
fi
Is it possible via Shell Script?
Answer: If we exit with integer 1, Jenkins build will be marked as failed. So I replaced the comment with exit 1 to resolve this.
All you need to do is exit 1.
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
exit 1
fi
To fail a Jenkins build from .NET, you can use Environment.Exit(1).
Also, see How do I specify the exit code of a console application in .NET?.
To fail a Jenkins build from Windows PowerShell, you can use Steve's tip for C# as follows:
$theReturnCode = 1
[System.Environment]::Exit( $theReturnCode )
Notes;
1.Windows recycles exit codes higher than 65535. ( https://stackoverflow.com/a/31881959/242110 )
2.The Jenkins REST API has a '/stop' command, but that will cancel the build instead of actually failing it. ( https://jenkinsapi.readthedocs.org/en/latest/build.html )
3.Jenkins can also mark a build 'unstable', and this post ( https://stackoverflow.com/a/8822743/242110 ) has details on that solution.

how to to run a script and get a result in boolean in shell script

I needs to run a CLI script to deploy a application and I should save the output of the script as a boolean variable to know whether the deployment is successful or not ?
Can you please help me to do the above scenario :
This is my CLI script :
/applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh -c --controller=localhost:9999 --commands="deploy /applic/jboss/Project/deploy/sample.ear --force, deployment-info --name=sample.ear, quit"
It will give an output like below :
NAME RUNTIME-NAME PERSISTENT ENABLED STATUS
sample.ear sample.ear true true OK
So, I needs to run a script which should give me the output like, whether my deployment is successful or not ?
I have created one shell script to do that. But, no luck.
#!/bin/bash
AA="[ -e /applic/jboss/Project/script/new_deploy.sh ]"
if $AA
then
echo "deployment successful"
else
echo "deployment unsuccessful"
fi
echo "done"
Can anyone let me know, how to modify the script to display the output?
Does /applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh exit with a non-zero status if the deployment somehow fails? That would be the easiest method:
if /applic/jboss/jboss-eap-6.1/bin/jboss-cli.sh -c --controller=localhost:9999 --commands="deploy /applic/jboss/Project/deploy/sample.ear --force, deployment-info --name=sample.ear, quit"
then
echo deployment succeeded
else
echo deployment failed
fi
If you have to parse the output to determine the level of success, then things get a bit thornier. But answer the first question first.

Executing Maven task from shell script and getting error codes

I'm executing a Maven deploy task from a bash script however even if the Maven task fails the script will continue and complete without errors.
I have tried the -e flag but that causes the deploy to fail. I also tried the following (pseudo code)
result_code= mvn deploy
if [$result_code -gt 0];then
exit 1
Any suggestions how i can identify if the deploy was successful?
result_code=mvn deploy is not the way to get return status
you can try e.g. :
#!/bin/bash
mvn deploy
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "Deployment Successful"
else
echo "Deployment Failed"
fi
In addition if anyone using Windows 10, here is the example which I use:
mvn deploy
if not %ERROR_CODE%==0 goto error
echo SUCCESS
goto end
:error
echo FAILED
:end
Just one other possible reason a person's mvn task may return 0 despite failing: be careful about piping the output of maven to other programs. For example, I'm using grcat (which grc is build on top of), which will always return exit code 0;
\mvn compile | grcat ~/conf.mvn
I'm not sure how to retain my nice color coding of the output. There is color_maven.sh out there but that has other issues.

Hudson is not exec-ing my bash-based build script

i have a simple "free-style" test job in huson.
it checks out a file from git (it does that part successfully)
it is also supposed to exec a script that appends to that file.
the script looks like:
#!/bin/sh -ex
echo "$0 was run on " `date` >> /tmp/failme.log
#echo "$0 was run on " `date` >> $HUDSON_HOME/failme.log
echo "this should fail"
echo "this went to stderr" >&2
exit -1
I put the 2nd line to test if the script is even run.
/tmp/failme.log is missing after a "successful" build
i can run the script as the hudson user (after allowing it to login) and the script behave properly.
I'm at a loss. I've read several inquiries here and in other forums and blogs about using hudson variables in scripts. none of them talk about anything special that i have to do to get hudson to exec the script.
thanks in advance.
nodnarb (strike that, reverse it)
yes, i'm answering my own question.
I attempted the same configuration with a new job, and the script runs as expected. I have no reason for this. I have attempted to dup the failure above 3 times, and cannot duplicate this issue. So, I am resolving this issue. maybe something "hiccuped" when the original job was created.
Thank you to all who commented.
B

Resources