Translation between declaritive pipleline and groovy pipeline

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Translation between declaritive pipleline and groovy pipeline

Peter Berghold
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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv1EH79K8XA9QMUDTqRAa8QWsixi_Pdx7cYon-88nyjcKg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Translation between declaritive pipleline and groovy pipeline

Ramanathan Muthaiah
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

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.

https://www.cloudbees.com/blog/sending-notifications-pipeline

/Ram

--
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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Translation between declaritive pipleline and groovy pipeline

Peter Berghold
Kool! Thanks! 

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

On Mon, Dec 4, 2017 at 1:15 AM Ramanathan Muthaiah <[hidden email]> wrote:
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

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

--
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAArvnv3%2ByXh54RAG8pr3yspRUMXzmV6vAsADjuDn0gC__t-93w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Translation between declaritive pipleline and groovy pipeline

jerome-2
In reply to this post by Peter Berghold
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: '[hidden email]', replyTo: '[hidden email]', 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/dfdf3042-8b26-4f83-b43f-a17b8142a4a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Translation between declaritive pipleline and groovy pipeline

Daniel Butler

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

 

From: [hidden email]
Sent: 04 December 2017 17:06
To: [hidden email]
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: '[hidden email]', replyTo: '[hidden email]', 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/dfdf3042-8b26-4f83-b43f-a17b8142a4a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a266724.43af500a.705b8.e595%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Translation between declaritive pipleline and groovy pipeline

stephenconnolly


On 5 December 2017 at 09:30, Daniel Butler <[hidden email]> 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: [hidden email]
Sent: 04 December 2017 17:06
To: [hidden email]
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: '[hidden email]', replyTo: '[hidden email]', 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/dfdf3042-8b26-4f83-b43f-a17b8142a4a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5a266724.43af500a.705b8.e595%40mx.google.com.

For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CA%2BnPnMyvDBXRWXRLgyV_647kqUzZNSxaVJQ0-xYhMdekP1grHw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.