Translation between declaritive pipleline and groovy pipeline

280 views
Skip to first unread message

Peter Berghold

unread,
Dec 3, 2017, 10:31:15 PM12/3/17
to Jenkins Users
What would be the equivalent of this:
post {
        always {
            echo 'One way or another, I have finished'
            deleteDir() /* clean up our workspace */
        }
        success {
            echo 'I succeeeded!'
        }
        unstable {
            echo 'I am unstable :/'
        }
        failure {
            echo 'I failed :('
        }
        changed {
            echo 'Things were different before...'
        }
    }
}
in a Groovy pipleline? 
I'm looking at using "something" to notify by email if a build fails

Ramanathan Muthaiah

unread,
Dec 4, 2017, 1:15:02 AM12/4/17
to Jenkins Users
Groovy code presented in this Cloudbees' article is about mail notifications (also, slack and hipchat) -- but you may want to extend it to suit your needs.


/Ram

Peter Berghold

unread,
Dec 4, 2017, 12:04:12 PM12/4/17
to jenkins...@googlegroups.com
Kool! Thanks! 

I suspected as much but now I full understand what's going on. 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/da5927cf-2fc5-4c46-9734-8db06b87be7a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jer...@bodycad.com

unread,
Dec 4, 2017, 12:05:54 PM12/4/17
to Jenkins Users
You need to wrap your groovy code into a try/catch/finally block and check the current build result ( I also give you some hint when things are not yet filled)
try
{
}
catch(any)
{
  println('Error occurred during build:');
  println(any.toString());
  println('Marking build as FAILURE because of this');
  currentBuild.result = 'FAILURE'
  throw any //rethrow exception to prevent the build from proceeding
}
finally
{
  if(!currentBuild.result)
  {
currentBuild.result = 'SUCCESS'
println("Current build result is undefined, setting it as ${currentBuild.result}")
  }
  // send email
  emailext body: 'my email body', subject: 'email subject', to: 'te...@test.dev', replyTo: 'te...@test.dev', mimeType: 'text/html';
}


Daniel Butler

unread,
Dec 5, 2017, 4:30:22 AM12/5/17
to jenkins...@googlegroups.com

If you use currentBuild.currentResult (was added a few months ago IIRC) you get a value that will never be null.

--

You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Stephen Connolly

unread,
Dec 5, 2017, 5:21:54 AM12/5/17
to jenkins...@googlegroups.com
On 5 December 2017 at 09:30, Daniel Butler <danielsi...@gmail.com> wrote:

If you use currentBuild.currentResult (was added a few months ago IIRC) you get a value that will never be null.


but it may not be completely correct.

https://gitbox.apache.org/repos/asf?p=maven-jenkins-lib.git;a=blob;f=vars/asfMavenTlpStdBuild.groovy;h=b03cd5e5c79ad26302f9e16d5af543aee96f426c;hb=HEAD#l97 is my best attempt at determining the different build results... I am not completely happy that it catches all cases correctly though 

 

From: jer...@bodycad.com
Sent: 04 December 2017 17:06
To: Jenkins Users
Subject: Re: Translation between declaritive pipleline and groovy pipeline

 

You need to wrap your groovy code into a try/catch/finally block and check the current build result ( I also give you some hint when things are not yet filled)

try
{
}
catch(any)
{
  
println('Error occurred during build:');

  println(any.toString());

  println('Marking build as FAILURE because of this');

  currentBuild.result = 'FAILURE'

  throw any //rethrow exception to prevent the build from proceeding

}
finally
{
  
if(!currentBuild.result)

  {

   currentBuild.result = 'SUCCESS'

   println("Current build result is undefined, setting it as ${currentBuild.result}")

  }

  // send email
  emailext body: 'my email body', subject: 'email subject', to: 'te...@test.dev', replyTo: 'te...@test.dev', mimeType: 'text/html';
}

 

 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

 

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a266724.43af500a.705b8.e595%40mx.google.com.
Reply all
Reply to author
Forward
0 new messages