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

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

我第一次尝试使用 Jenkins Job DSL 插件来创建一些基本的作业 "templates",然后再进入更复杂的东西。

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

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

前两个没问题:我可以调用 groovy 脚本中的参数,例如脚本理解 job("${JOB_NAME}") 并在我 [=35= 时采用我为作业输入的名称] 种子工作。

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

我的作业 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 做任何事情,我确信这是一个基本错误,但希望得到任何帮助。

嗯,这是作业 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)
    }
  }
  // ...
}

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