Jenkins execute shell output mystery - macos

I'm tyring to run some build script in MacOS 10.11 but get no information about errors occurred during it's running. Even I try
<hudson.tasks.Shell>
<command>echo "fucking Jenkins!!!"
#!/bin/bash -l
echo "fucking Jenkins!!!"
/bin/sh -e
echo "fucking Jenkins!!!"
./build.sh $BUILD_NUMBER "Jenkins test build"
echo "fucking Jenkins!!!"</command>
</hudson.tasks.Shell>
I get in the console output
git rev-list ...
and then failure or success depending I set -e choice or not. Even echo command writes nothing. But the fact of failure doesn't help at all if there's no more information. No more options I could find over the Web. What is the secret of the execute shell plugin to make it write something to the console output.

Related

Jenkins build passes when the shell script has execution errors in it

I have a shell script that executes multiple sql files that updates to the database. I am calling the shell script from jenkins- build- execute shell. The jenkins console shows success at all times irrespective of the errors from the sql files. I want Jenkins to fail the build, if there is an error or any of the sql file failed executing and send the console output to the developer, if fails.
I tried echo $? in the shell script but it shows 0.
#!/bin/bash
walk_dir () {
shopt -s nullglob dotglob
for pathname in "$1"/*; do
if [ -d "$pathname" ]; then
walk_dir "$pathname"
else
case "$pathname" in
*.sql|*.SQL)
printf '%s\n Executing SQL File:' "$pathname"
sudo -u postgres psql <DBName> -f $pathname
rm $pathname
esac
fi
done
}
DOWNLOADING_DIR=/home/jenkins/DB/
walk_dir "$DOWNLOADING_DIR"
Jenkins Console results
ALTER TABLE
ERROR: cannot change return type of existing
DETAIL: Row type defined by OUT parameters is different.
CREATE FUNCTION
ALTER FUNCTION
CREATE FUNCTION
ALTER FUNCTION
Finished: SUCCESS
Expected Results: Failed from Jenkins (if any of the sql files failed executing from shell script) but it is showing as passed in Jenkins
Thanks for all the inputs. I was able to fix this issue. I installed 'log parser plugin' in Jenkins which will parse the keywords like /Error/ in the console output and make the build to fail.
This S/O answer will probably address your scenario: Automatic exit from bash shell script on error
This duplicate answer also provides useful guidance:Stop on first error [duplicate]
Essentially use set -e or #!/bin/bash -e.
If you don't trap every potential error, then the next step in the script will execute and the return code will be that of the last command in the script.
Direct link to www.davidpashley.com - Writing Robust Bash Shell Scripts
** This also assumes any external commands (eg: psql) also properly trap and return status codes.
Below code may help you. This is how i sorted the issue with mine.
Instead of sudo -u postgres psql <DBName> -f $pathname
Use below code:
OUTPUT=$(psql -U postgres -d <DBName> -c "\i $pathname;")
echo $OUTPUT | grep ERROR
if [[ $? -eq 0 ]]
then
echo "Error while running sql file $pathname"
exit 2
else
echo "$pathname - SQL file Executed, successfully"
fi
Jenkins will not provide you the SQL files error code. Jenkins just checks if your shell script is executed or not and based on that it will return a status code which generally is zero as it is executing the shell script successfully.

bash -x /var/lib/cloud/instance/user-data.txt runs but user-data from terraform gives error

I am trying to run a script using terraform. The content of the user-data is as follows:
.
.
cat <<EOH | java -jar ./jenkins-cli.jar -s $JENKINS_URL -auth admin:$PASSWORD create-credentials-by-xml system::system::jenkins _
<com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey plugin="ssh-credentials#1.16">
<scope>GLOBAL</scope>
<id>$CRED_ID</id>
<description>$SLAVE_IP pem file</description>
<username>ec2-user</username>
<privateKeySource class="com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey\$DirectEntryPrivateKeySource">
<privateKey>${worker_pem}</privateKey>
</privateKeySource>
</com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey>
EOH
.
.
When it executes as part of user-data it gives out an error as No such command create-credentials-by-xml.
But when I log in to the instance and execute bash -x /var/lib/cloud/instance/user-data.txt it runs as expected.
Can anyone please tell what is the reason for it and how to fix it? Thanks!
I have tried #cloudhook and separating the lines as well, but didn't work.
Answering my question:
Where was the problem:
The issue was not with bash but with jenkins-cli.jar itself.
The error message showed No such command create-credentials-by-xml which made me think, it was bash error but in reality, it was jar file error all along.
Reason:
The reason it was failing at user-data execution and not at bash execution was that it was unable to load configuration of plugins in a short amount of time.
Solution:
From the given reason, it is obvious that it needs time so I gave it sleep 25 to confirm it works or not and yes it does work but it was not an ideal solution.
Optimized Solution:
To make it better I listed the plugins before executing any jar commands and if the list came empty re run the commnd
# Creating CMD utility for jenkins-cli commands
jenkins_cmd="java -jar /opt/jenkins-cli.jar -s $JENKINS_URL -auth admin:$PASSWORD"
# Waiting for Jenkins to load all plugins
while (( 1 )); do
count=$($jenkins_cmd list-plugins 2>/dev/null | wc -l)
ret=$?
echo "count [$count] ret [$ret]"
if (( $count > 0 )); then
break
fi
sleep 30
done

BASH script works flawless in Cygwin, crashes when launched from Jenkinses "Execute shell"

I have a problem with Jenkins. I have a loop like this (embedded in a for...do..done loop)
var=$(head $file | grep ERROR)
if [ ! -z "$var" ]; then
echo "Some error message"
.././converter.sh $file $file.TMP $file.ERR.txt
RESULT=$(expr $RESULT + 10)
fi
This loop is working perfectly when launched from Cygwin, but is crashing when launched on Jenkins... All the information Jenkins spits out of his mouth:
08:55:50 + for file in '*.DATA'
08:55:50 ++ grep ERROR
08:55:50 ++ head TESTFILE_1.DATA
08:55:50 + var=
08:55:50 Build step 'Execute shell' marked build as failure
08:55:50 Archiving artifacts
When run under Jenkins with Execute Shell (from the help document of the Execute Shell build step):
By default, the shell will be invoked with the "-ex" option. So all of the commands are printed before being executed, and the build is considered a failure if any of the commands exits with a non-zero exit code. Again, add the #!/bin/... line to change this behavior.
To reiterate, this means that any errors (such as your grep exiting with an error code) will cause the script to exit prematurely with an error. Normally, when you execute this script, it probably succeeds because set -e is not the default.
Any easy test to determine whether this is indeed the problem is to set the first line of the Execute Shell script to #!/bin/sh -x

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.

Can't output result in bash from an ant command

I am writing a bash script that modifies some config files, runs "ant ear war" as a different user, outputs the return, exits back to the root to continue with the rest of the script. The issue is that the script does not continue after exiting and I don't get an output from "ant ear war".
Thank you for the help.
here is an example
#When running the bash script i don't see the output. Maybe it's because I run it as root and switched to another_user. So I tried to outputing result into a variable and into a text file. Both failed
su another_user
cd /usr/empi/MMEMPIV741/
echo $(ant ear war) >> /tmp/empi_install.txt
varant="$?"
echo 'if zero it's success otherwise it's a failure'
cp /usr/accessmgr/AMV741/bin/am/JBoss/AccessManager.war /usr/jboss/jboss-eap-4.3/jboss-as/server/default/deploy/
cp /usr/empi/MMEMPIV741/person_project/working-dir/dist/* /usr/jboss/jboss-eap-4.3/jboss-as/server/default/deploy/
exit
#By this time above is exited from another_user and should return to root
echo $varant
echo "http://`hostname`:21080/PersonMasterIndexDQM/flex/login.jsp#"
Put the commands you want to run in a different user context into a separate script and run that script via
su another_user -c /path/to/other.sh

Resources