0

I am running a psql script with 4 queries in it in jenkins, and the script failed because of a small typo but the build was marked as successful:

TRUNCATE TABLE
INSERT 0 3331
INSERT 0 133
INSERT 0 151
psql:/home/BI/payments/paypal_payments.sql:196: ERROR:  table name "pe" specified more than once
Finished: SUCCESS

Anyone know how to make the build be marked correctly as failed?

EDIT

Apparently Jenkins marks all builds as successful if the last command was... See this answer

Does anyone know how to log or output an error from postgres at the end of the build so that jenkins understands this?

Community
  • 1
  • 1
johan855
  • 1,578
  • 4
  • 26
  • 51
  • How are you executing the script? Do you have something like 'ant' in your build? – Dave Feb 10 '16 at 18:05
  • Just as if it was run from the command line: psql -d montredo -f /home/BI/payments/paypal_payments.sql – johan855 Feb 10 '16 at 18:48
  • I ask, because if you were using Ant somewhere, and wrapped the script within an ant SQL tag (https://ant.apache.org/manual/Tasks/sql.html), then you'd be able to fail the Jenkins build step by setting the 'onerror' attribute. – Dave Feb 10 '16 at 18:50

4 Answers4

1

Assuming you are running this from the bash, you can have this inside a shell script, store the return in a variable and regex lookup the return for the ERROR string. If you find it just 'exit 1' and Jenkins will fail.

  • 1
    Hi Eudes, thanks for your time, I decided to go with the plugin in Jenkins, although I think this is a more efficient solution I want to keep the bash scripts to the minimum in my setup. – johan855 Feb 12 '16 at 16:31
0

Try using the 'ON_ERROR_STOP' psql advanced feature, take a look at this. I hope this helps!

Community
  • 1
  • 1
Dave
  • 161
  • 1
  • 7
0

You can use the Log Parser Plugin Using that plugin, you'll need to define a parsing rule file. In the file you map between expressions that appear in the actual console log, to the required result for each expression. Examples: if the log contains "ERROR", set error result:

error /ERROR/

you can be more specific - mark error when there is psql error(using regex):

error /psql.*ERROR:/

there are more marks that you can use: "ok", "wanring", "info" and "start".

after you select the file that contains the parsing rules, you can decide if to mark the build as unstable in case of warning and it to fail the build in case of error.

log parser settings

for more info see the plugin's wiki in the link.

Tidhar Klein Orbach
  • 2,896
  • 2
  • 30
  • 47
  • But this plugin only allows me to check for the log in a more simple way right? I see there are no feactures that actually force the build to be marked as failed. Have you used it? – johan855 Feb 11 '16 at 09:22
  • @johan855 This is the main features of the plugin. checkout my edit, you decide if warning would make the build unstable and if error would make the build failed. – Tidhar Klein Orbach Feb 11 '16 at 10:28
0

If you're using the "Execute shell" step in Jenkins, by default, it will stop as soon as any command fails, and the build will be marked as failed.
i.e. as soon as a command returns a non-zero exit code.
(the link in your question relates to executing Windows batch scripts with Jenkins)

The psql command returns exit code 0 if execution succeeded, or exit code 3 if an error occurred in the script (which did happen), and you set the ON_ERROR_STOP variable (which apparently was not set).

So it seems like just setting ON_ERROR_STOP will cause psql (and therefore Jenkins) to behave as you expect in the future.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193