Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail a Jenkins build from groovy script

Tags:

jenkins

I have a groovy script that goes and promotes code. Long story short, I know at a point of time within that script if it was successful or not. I would like to fail the build if not successful. Is there a way in groovy to fail the build?

Example:

in the "execute Groovy script" plugin. you can write code.

(insert API call to promote code) if(checkPromote()){ //fail build here }

where 'checkPromote' returns a true or false value depending on the status of the promote.

like image 423
Eddie Avatar asked Apr 13 '17 19:04

Eddie


People also ask

How do you fail a build in Jenkins?

You can use the error step from the pipeline DSL to fail the current build. error("Build failed because of this and that..") Do you know if that message is stored in a variable like BUILD_NUMBER or JOB_NAME, etc...?

How do I run a Groovy script in Jenkins build?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name.


1 Answers

The most elegant way to abort a programm in my opinion is an assertion.

assert condition : "Build fails because..."

like image 111
herm Avatar answered Sep 17 '22 02:09

herm