Executing Maven task from shell script and getting error codes - bash

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.

Related

Retry failed xcodebuild test

I am building custom Jenkins build script (sh) for iOS app build/test checks.
But sometimes UI test fails just because of timing issue, so I want it to re run few more times to make sure the issue is real.
for (( ATTEMPT=1; ATTEMPT<=2; ATTEMPT++ ))
do
xcodebuild [flags] test #add_result_saving_mechanism
#if failed, do smth to go to next attempt. Else - break
if SOME_KIND_OF_FAIL_CHECK; then
continue
else
break
fi
fi
I used xcpretty before, so was able to read $PIPESTATUS and react accordingly, but xcpretty is crashing for xcodebuild test for some reason, so looking ways to do without it
xcodebuild [flags] test | xcpretty
STATUS="${PIPESTATUS}"
if [ "$STATUS" != "0" ]; then
FAILURE_MSG="${TARGET} (${BUILD_NAME}) failed UI/Unit testing"
#try next attempt if available
continue
else
break
fi
How can I handle these retries without pipes/xcpretty?
From the jenkins perspective - it always used to bail early if a script encountered an error, so you could try this type of syntax to prevent that (in the jenkins config this is, not in the script itself)
jenkins_build_script.sh || true
# continue with things...
Also, if you're having trouble capturing the failure itself - try piping the xcodebuild output to a log file and then grep-ing for the errors you're anticipating.
Since you said it's a script run by Jenkins you can handle the retry from Jenkins pipeline instead of inside the shell script.
As in the example from the docs
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
}
}
}
}
You can read about it here
Hope this will help good luck.

How to find if a Job fails in jenkins

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.

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.

how to detect a build error from ant/maven via a bash script?

I am writing a bash script to automate the build process. There are two major build blocks, one is an ant task and one is a plain old mvn clean install. I want to do something when there is build error coming from either of this two build processes.
The problem is, these builds will contain test failures or errors from time to time, but the end result is successful. And I believe that the status code ($?) return by these processes should be 0 no matter the build fail or succeed, I could be wrong.
So what is the best way for my script to detect the end result (build fail/succeed) without catching the false info during the mid build (test errors, etc) from them?
mvn clean test
if [[ "$?" -ne 0 ]] ; then
echo 'could not perform tests'; exit $rc
fi
$? is a special shell variable that contains the exit code (whether it terminated successfully, or not) of the most immediate recently executed command.
-ne stands for "not equal". So here we are testing if the exit code from mvn clean is not equal to zero.
There are a few issues against Maven 2 returning incorrect return codes (i.e. always returning 0). Notably MNG-3651 that was fixed in Maven 2.0.9.
In older versions, mvn.bat ended with this line:
exit /B %ERROR_CODE%
From Maven 2.0.9 onwards, the last line was changed to this:
cmd /C exit /B %ERROR_CODE%
So a non-0 return code is returned if the build fails. In the case of a build ERROR the return code is 1. If you are unable to upgrade to 2.0.9+, you could consider modifying mvn.bat as above to return the correct code.
Correct solution for unix/linux:
mvn clean install
rc=$?
if [ $rc -ne 0 ] ; then
echo Could not perform mvn clean install, exit code [$rc]; exit $rc
fi
The "if" statement itself is a command and if it is successful, it will reset the $? variable to 0. Same goes for echo. So, you have to use an intermediary local var, for example $rc to store the return code from "mvn clean install", then it can be passed to the "exit" command as well.
According to the Ant manual:
the ant start up scripts (in their Windows and Unix version) return the return code of the java program. So a successful build returns 0, failed builds return other values.
Maven also returns a non-zero exit code on error. Here's a link showing how to interrogate this status using the Maven Invocation API.
So it seems to me that you should be able to explicitly handle the return codes in your script
. Presumably you can ignore error codes relating to tests etc. if those are not a concern to you.
exec error codes in Ant are operating system-specific. These may help you:
a list of error codes for Linux
a list of error codes for Windows
Here is exactly what I do to get the result you want.
<exec executable="${env.M2_HOME}/bin/mvn" dir="${basedir}"
failonerror="true" osfamily="unix">
<arg value="-DskipTests=${argSkipTests}"/>
<arg value="-Doffline=${argOffline}"/>
<arg line="${projectsLine}"/>
<arg line="${resumeFromLine}"/>
<arg line="${alsoMakeLine}"/>
<arg line="${alsoMakeDependentsLine}"/>
<arg line="${commandsLine}"/>
</exec>
There's a command built-in to bash which performs exactly this.
# exit when any command fails
set -e
I put this at the top of my bash scripts, which I copied from this excellent resource.
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT

Resources