How to change exit code message of bash - bash

I have a CI CD pipeline where I use a bash task to execute some script.
I want the build to fail if the script fails. So now I implemented an exit code where the bash script needs to fail. All good except for the error message.
I want to use a exit code whereby I can set an error message something like:
exit "script failed because alerts were found"
But I found online that you can only pass integers to your exit code.
Tried to fix it with:
echo "fail message"
exit code 2
But on my pipeline I got the Bash exited with code '2'. message and after i open the task output i can see my echo message.
Don't know if this is a Azure DevOps issue or I can fix this in bash. Anyone has any ideas?
[EDIT] Tried it with trap inside my local machine, and the custom error message works. But the same thing didn't work in Azure DevOps. I might think this is not possible in Azure DevOps -_-. Is there someone who already tried this ?

Perhaps the console in the screenshot shows only the script's standard error?
I would try something like:
echo "fail message" 1>&2
exit code 2

I'd suggest you have a look at "trap" command :
https://www.shellscript.sh/trap.html
and try something like :
trap 'your_function' EXIT

Related

How to debug or see details with bash "exit code 1"?

I have a simple bash script in azure devops which goes wrong, I want to see error details, but I dont know how:
Generating script.
Script contents:
cat pipelines\\backend\\services\\Authorization_Service.json
C:\Windows\system32\bash.exe -c pwd
##[error]The process 'C:\Windows\system32\bash.exe' failed with exit code 1
Finishing: Bash

capture exit code from a script flow

I need help with some scripts I'm writing.
Scenario:
Script A is executed by a scheduling process. This script takes the arguments passed to it, parses them in some way and runs script B feeding it with those arguments;
Script B does sudo -u user ssh user#REMOTEMACHINE, runs some commands (in the remote machine) and finally runs script C (also in the remote machine). I am passing those commands using a HERE DOCUMENT. Also, I'm passing the previous arguments to this script too.
This "flow" runs correctly and the job completes successfully.
My problems are:
Since this "flow" is ran by a scheduling process, I need to tell it if the job completed successfully or not. I'm doing this via exit codes, so what I want is to have a chain of exit codes, returning back from the last script to the first, in case of errors. I'm not able to perform this, because exit codes works correctly for the single scripts (I tried executing them singularly and look for the exit codes), but they are not sended back to the parent script. In my opinion, the problem is that ssh is getting the exit code from the child script, which in fact ended successfully, because there was no error executing it: it's the command inside of it that gone wrong.
While the process works correctly, I still get this line:
ssh: Could not resolve hostname : Name or service not known
But actually the script completes successfully.
I hope you understand what I wrote, I can eventually post my scripts here.
Thanks
O.
EDIT:
This are the scripts. There could be some problem with variable names because I renamed it quikly to upload the files.
Since I can't upload 3 files because of my low reputation, I merged them in a single file
SCRIPT FILE
I managed to solve the problem.
I followed olivier's advice and used the escape char to make the variable expanded by the remote machine.
Also I implemented different exit codes based on where the error occured.
At last, I modified the first script as follows, after launching sudo -u for the second script:
EXITCODEOFTHESECONDSCRIPT=$?
if [ $EXITCODEOFTHESECONDSCRIPT = 0 ]
then
echo ""
echo "Export job took $SECONDS seconds."
echo ""
exit 0
else
exit $EXITCODEOFTHESECONDSCRIPT
fi
This way I am able to exit the main script MAINTAINING the exit code provided from the second script.
In fact, I found that the problem was that the process worked well, even in case of errors, but the fact that I was giving more commands after the second script fail (the echo command was enough) provided other exit codes that overwrited the one I wanted.
Thanks to all !

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.

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

How to abort build if run script phase gives an error

I would like to use a run scripts target to tag my revision in mercurial and upload it to a server. I created a new run scripts target that depends on the other target building my app, then I added two run script phases directly one after another.
Now my question: how can I prevent executing run script phase #2 if run script phase #1 gives an error (return code of script is unequal 0)?
The second script would upload the files to a server, so I only want to execute this phase if everything went right until then.
Your first script can abort the build with "exit 1", like this:
if [ error ] then
echo "There are some errors, etc..."
exit 1
fi
A solution would be to generally stop building when errors occur:
XCode -> Preferences -> Building -> Build options "Continue building after errors".
If you uncheck that, the build will stop if your script returns something unequal 0 and the second script will not be executed. If you use xcodebuild, the name for the setting is "PBXBuildsContinueAfterErrors".
exit 1 aborts the build.
echo "error: will flag it up as an error within Xcode.
echo "error: Everything has failed!"
exit 1

Resources