Do manual build fail in Jenkins using shell script - shell

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.

Related

How do I get that a WebSphere application has been installed in Jython?

When I deploy an .ear application in WebSphere I have a problem in installing the shared libraries. I use a workaround to solve my issue like that
[... code to install the application]
&& sleep 60
&& /opt/IBM/WebSphere/AppServer/bin/wsadmin.sh -lang jython -c \
"AdminApp.edit('appname', ['-MapSharedLibForMod', [['.*','.*', 'ibm']]])"
because I need to be sure that the .ear file has been installed before calling AdminApp.edit
How can I get rid of the sleep command? Is there a way to get a signal that the app has been installed?
In my deploy script (bash) I call:
#!/bin/bash
$DM_WAS_HOME/wsadmin.sh -f $SCRIPTS_HOME/application_deploy.jacl $WORKING_DIRECTORY/appServer/$EAR_NAME $dmserver
if [ $? -eq 0 ]
then
$DM_WAS_HOME/wsadmin.sh -lang jython -f $SCRIPTS_HOME/link_shared_lib.jython
if [ $? -ne 0 ]
then
echo "ERROR: could not link libraries."
exit 2
fi
else
echo "ERROR: installation failed, fix it"
exit 1
fi
Anything goes wrong in the wsadmin.sh installation and the exit status is not 0. This way if your install takes more time for some reason, it will not be an issue, since only when the first task is done will you move on.
The application installation jacl sets a bunch of variables and calls:
$AdminApp update $appname app $updateopts
$adminConfig save
foreach nodeName $SyncNode {
puts "Syncing $nodeName"
$AdminControl invoke $nodeName sync
}
So anything does not work correctly in there, the exit status is != 0.
Yes I know I have to rewrite my jacl into jython (still on WAS 7 for this application).

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.

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.

How to stop Xcode build from shell script

I am currently working on an app in Xcode where I've added a Build Phase that runs a shell script.
The script looks for resources from the Desktop and copies them to the app . If the files/folders don't exist, the script should cancel the build of the app.
I have tried various things to stop the build such as xcodebuild clean but I can't quite figure it out. Here is what I have:
if [ -d ~/Desktop/MyFolder ]; then
cp -r ~/Desktop/MyFolder ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MyFolder
else
#Stop the build
fi
Is there a way to have the script tell Xcode to stop the build? If so, how can I do it?
You need to return a non-zero exit code from the script:
exit 1
Another useful technique is for the script to produce an output with an "error: " prefix.
This will cause Xcode to show the error within the build logs, and if you Xcode settings stop building on failure - it will stop right there.
In addition to this, you could also trigger a warning by printing out "warning: " prefix.
Example:
if [ -d ~/Desktop/MyFolder ]; then
cp -r ~/Desktop/MyFolder ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MyFolder
else
echo "error: Copy failed!"
fi

Resources