2

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rajspeeder
  • 217
  • 5
  • 14
  • Jenkins Console Output Fast-forward get_factory_view.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) OK ERROR: syntax error at or near "CREATEd" LINE 1: CREATEd OR REPLACES FUNCTION public.set_view( ^ 0 0 Finished: SUCCESS – Rajspeeder Sep 06 '19 at 18:25
  • 1
    Make sure to forward your fail condition to the exitcode of your script. in your example, if the `rm` is the last command, this will become your exit code, no matter if you had a failure. Either handle the exit codes of your subcommand and explicitely set it in your script, or chain your stuff with help of `&&` to abort on error. – Dominik Gebhart Sep 06 '19 at 18:27
  • 1
    In addition to what Dominik said, which is good advice, look into the console parser plugin - https://wiki.jenkins.io/display/JENKINS/Console+Parser+Plugin – Adam vonNieda Sep 06 '19 at 20:14
  • In my case the problem was that I used `tee`: `bash script.sh | tee log.log`, so `tee` erased any bad exit code – Demetry Pascal Jun 04 '23 at 11:18

4 Answers4

1

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.

Rajspeeder
  • 217
  • 5
  • 14
  • 100% @rajaspeeder. I spent a lot of wasted time trying to get `WHENEVER SQLERROR EXIT SQL.SQLCODE;` to no avail. Log Parser was the only reliable way I found to reliably detect Oracle errors and fail Jenkins jobs running oracle scripts. In my case my "Parsing Rules File" has 1 line: "error /ORA-/" next you add a Log Parser post build task to the end of the job and it will catch all ORA- errors in the console output and fail the job. Brilliant. – Jeff Mergler Feb 20 '21 at 17:58
0

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.

Ian W
  • 4,559
  • 2
  • 18
  • 37
0

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
Sreejith
  • 434
  • 3
  • 19
0

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.