How to break a pipeline manually?

6,895 views
Skip to first unread message

Feng Yu

unread,
May 30, 2016, 3:21:16 AM5/30/16
to Jenkins Users
Hi there:
    Here is my pipeline code:
node{
   
// my code here
   
try {
       
// some steps
        currentBuild
.result = 'SUCCESS'
   
} catch (Exception err) {
       
currentBuild.result = 'FAILURE'
        mail  // email to me if failed
       
// some clean steps
   
}

    if (currentBuild.result == 'FAILURE') {
        // How to exit pipeline ?
    }

    /*
     * other steps here
     */
}


How do I break the pipeline manually if some steps failed?

Thanks.

Michael Neale

unread,
May 30, 2016, 4:38:41 AM5/30/16
to Jenkins Users
you can just return - and it should exit the script? (or throw an exception).

Lionel Orellana

unread,
Jun 29, 2016, 12:28:37 AM6/29/16
to Jenkins Users
From what I've tried simply returning doesn't stop the execution of the script. 

If you throw an exception the result seems to always be FAILURE. 

I would like to know if there is a way to stop execution but leave the result as say UNSTABLE.

Michael Neale

unread,
Jun 29, 2016, 2:41:56 AM6/29/16
to Jenkins Users
If return is called from the "top level" of a script (ie not inside a function) then it should return early. 

eg: 

node {

  echo "42"
  if (42 == 42) {
      currentBuild.result = 'UNSTABLE'
      return
  }
   echo "43"
}

This will exit early and never print "43". 

Sverre Moe

unread,
Jun 29, 2016, 3:20:34 AM6/29/16
to Jenkins Users

Lionel Orellana

unread,
Jun 30, 2016, 12:47:59 AM6/30/16
to Jenkins Users

That gets out of the node but will continue if there other nodes below it. 

I've found that to get out completely it needs to be at the very top (i.e. between nodes). 

Lionel Orellana

unread,
Jun 30, 2016, 12:49:25 AM6/30/16
to Jenkins Users
Thanks Sverre. That marks the build as failed though. 

Doing a return between nodes is what I was looking for to exit without changing the build result. 
Reply all
Reply to author
Forward
0 new messages