Search code examples
groovyslackjenkins-job-dsl

Groovy script fails to call Slack notification parameter from Jenkins DSL job


I'm attempting to use the Jenkins Job DSL plugin for the first time to create some basic job "templates" before getting into more complex stuff.

Jenkins is running on a Windows 2012 server. The Jenkins version is 1.650 and we are using the Job DSL plugin version 1.51.

Ideally what I would like is for the seed job to be parameterised so that when it is being run the user can enter four things: the Job DSL script location, the name of the generated job, a Slack channel for failure notifications, and an email address for failure notifications.

The first two are fine: I can call the parameters in the groovy script, for example the script understands job("${JOB_NAME}") and takes the name I enter for the job when I run the seed job.

However when I try to do the same thing with a Slack channel the groovy script doesn't seem to want to play. Note that if I specify a Slack channel rather than trying to call a parameter it works fine.

My Job DSL script is here:

job("${JOB_NAME}") {
    triggers {
        cron("@daily")
    }
    steps {
        shell("echo 'Hello World'")
    }
    publishers {
    slackNotifier {
      room("${SLACK_CHANNEL}")
      notifyAborted(true)
      notifyFailure(true)
      notifyNotBuilt(false)
      notifyUnstable(true)
      notifyBackToNormal(true)
      notifySuccess(false)
      notifyRepeatedFailure(false)
      startNotification(false)
      includeTestSummary(false)
      includeCustomMessage(false)
      customMessage(null)
      buildServerUrl(null)
      sendAs(null)
      commitInfoChoice('NONE')
      teamDomain(null)
      authToken(null)
    }
  }
    logRotator {
        numToKeep(3)
        artifactNumToKeep(3)
    publishers {
        extendedEmail {
            recipientList('[email protected]')
            defaultSubject('Seed job failed')
            defaultContent('Something broken')
            contentType('text/html')
            triggers {
              failure ()
              fixed ()
              unstable ()
                stillUnstable {
                    subject('Subject')
                    content('Body')
                    sendTo {
                        developers()
                        requester()
                        culprits()
                    }
                }
            }
        }
    }
  }
}

But starting the seed job fails and gives me this output:

Started by user 
Building on master in workspace D:\data\jenkins\workspace\tutorial-job-dsl-2
Disk space threshold is set to :5Gb
Checking disk space Now 
Total Disk Space Available is: 28Gb
 Node Name: master
Running Prebuild steps
Processing DSL script jobBuilder.groovy
ERROR: (jobBuilder.groovy, line 10) No signature of method: javaposse.jobdsl.plugin.structs.DescribableContext.room() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [#dev]
Possible solutions: wait(), find(), dump(), grep(), any(), wait(long)
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE

This is the first time I have tried to do anything with Groovy and I'm sure it's a basic error but would appreciate any help.


Solution

  • Hm, that's a bug in Job DSL, see JENKINS-39153.

    You actually do not need to use the template string syntax "${FOO}" if you just want to use the value of FOO. All parameters are string variables which can be used directly:

    job(JOB_NAME) {
      // ...
      publishers {
        slackNotifier {
          room(SLACK_CHANNEL)
          notifyAborted(true)
          notifyFailure(true)
          notifyNotBuilt(false)
          notifyUnstable(true)
          notifyBackToNormal(true)
          notifySuccess(false)
          notifyRepeatedFailure(false)
          startNotification(false)
          includeTestSummary(false)
          includeCustomMessage(false)
          customMessage(null)
          buildServerUrl(null)
          sendAs(null)
          commitInfoChoice('NONE')
          teamDomain(null)
          authToken(null)
        }
      }
      // ...
    }
    

    This syntax is more concise and does not trigger the bug.