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

sha*_*eoh 2 groovy jenkins-job-dsl slack

我尝试第一次使用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()
                    }
                }
            }
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是启动种子作业失败,并提供以下输出:

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
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试对Groovy进行任何操作,我敢肯定这是一个基本错误,但希望能对您有所帮助。

das*_*ker 5

嗯,那是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)
    }
  }
  // ...
}
Run Code Online (Sandbox Code Playgroud)

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