且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-05 13:29:40

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

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

  job(JOB_NAME){
// ...
发布商{
slackNotifier {
房间(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)
}
}
// ...
}

这个语法更简洁,不会触发bug。


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('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()
                    }
                }
            }
        }
    }
  }
}

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.

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.