6

I want to use the Slack Notification Plugin in my pipelines, which is very easy:

slackSend color: 'danger', message: 'Everything broke'

However, I don't want the build to break if slackSend doesn't exist. Is there a way to check that first?

DanielM
  • 6,380
  • 2
  • 38
  • 57

2 Answers2

1

You might be able to wrap it in a conditional, though I'm not sure how Jenkins adds stuff to the scripts...

if(this.respondsTo('slackSend')) {
    slackSend color: 'danger', message: 'Everything broke'
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Unfortunately Jenkins doesn't like it `Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods respondsTo java.lang.Object java.lang.String`. I'm seeing this kind of "scripts not permitted to" with a lot of stuff other people seem to be able to use, including `currentBuild.rawBuild` that other people use so I don't know if my pipelines are configured correctly. – DanielM Aug 23 '16 at 14:04
  • 1
    I wonder if it is really the `this` object that you should check for that `respondsTo` on – Simon Forsberg Nov 09 '17 at 20:49
1

You could always use the old try/catch to make sure your build doesn't fail on this step :

def resultBefore = currentBuild.result
try {
   slackSend color: 'danger', message: 'Everything broke'
} catch(err) {
   currentBuild.result = resultBefore
}

However, I don't really see why slackSend command would not exist ? It can fail (e.g. if your Slack server is down) but as long as you have Slack Notification Plugin installed it should exist !

Pom12
  • 7,622
  • 5
  • 50
  • 69
  • 1
    It's more about portability. It's on the Jenkins machine I set up myself, but if it gets moved to the main engineering one, I don't want them to have to worry if it's there or not (they have their own thing going on). I tried this approach but it doesn't seem to work. Also, if I'm reporting on a broken build, I wouldn't want to change the build status. :) – DanielM Aug 23 '16 at 14:24
  • I tried recording the build result before using a function I knew didn't exist and then setting it back in the catch. Unfortunately this still doesn't work `No such DSL method 'thisFunctionNoExists' found among steps`. – DanielM Aug 23 '16 at 14:32
  • Just tested above code in a simple pipeline and it works perfectly ! You're right, you need to set back previous build result, I edited my post accordingly – Pom12 Aug 23 '16 at 14:39