Groovy脚本无法从Jenkins DSL作业调用Slack通知参数

时间:2016-10-20 14:55:30

标签: groovy slack jenkins-job-dsl

我第一次尝试使用Jenkins Job DSL插件来创建一些基本工作"模板"在进入更复杂的事情之前。

Jenkins正在Windows 2012服务器上运行。 Jenkins版本是1.650,我们使用的是Job DSL插件版本1.51。

理想情况下,我希望对种子作业进行参数化,以便在运行时,用户可以输入以下四项内容:作业DSL脚本位置,生成作业的名称,失败通知的Slack通道,和失败通知的电子邮件地址。

前两个很好:我可以在groovy脚本中调用参数,例如脚本理解job("${JOB_NAME}")并在运行种子作业时获取我为作业输入的名称。

然而,当我尝试用Slack频道做同样的事情时,groovy脚本似乎不想玩。请注意,如果我指定Slack通道而不是尝试调用参数,则它可以正常工作。

我的Job DSL脚本在这里:

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('me@mydomain.com')
            defaultSubject('Seed job failed')
            defaultContent('Something broken')
            contentType('text/html')
            triggers {
              failure ()
              fixed ()
              unstable ()
                stillUnstable {
                    subject('Subject')
                    content('Body')
                    sendTo {
                        developers()
                        requester()
                        culprits()
                    }
                }
            }
        }
    }
  }
}

但是启动种子作业失败并给我这个输出:

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

这是我第一次尝试使用Groovy做任何事情,我确定这是一个基本错误,但我会感激任何帮助。

1 个答案:

答案 0 :(得分:4)

嗯,这是Job DSL中的一个错误,请参阅JENKINS-39153

如果您只想使用"${FOO}"的值,则实际上不需要使用模板字符串语法FOO。所有参数都是字符串变量,可以直接使用:

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)
    }
  }
  // ...
}

此语法更简洁,不会触发错误。