2016-08-06 177 views
3

为了让我的jenkins管道定义文件更具可定制性,我尝试使用最大的变量。jenkins管道中的变量

当我尝试在邮件指令詹金斯使用变量抛出此错误:

java.lang.NoSuchMethodError: No such DSL method '$' found among [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, emailextrecipients, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, pwd, readFile, readTrusted, retry, sh, sleep, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, waitUntil, withCredentials, withEnv, wrap, writeFile, ws] 

这是我的詹金斯pipleline定义文件:

#!groovy 
node { 

    //Define job context constants 
    def projectName = "JenkinsPipelineTest" 
    def notificationEmailRecipients = "[email protected]" 
    def notificationEmailSender = "[email protected]" 
    currentBuild.result = "SUCCESS" 

    //Handle error that can occur in every satge 
    try { 

      //Some others stage... 

      stage 'Finalization' 
      step([$class: 'ArtifactArchiver', artifacts: '*.zip, *.tar, *.exe, *.html', excludes: null]) 
      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: ${notificationEmailRecipients}, sendToIndividuals: false]) 
    } 
    catch (err) { 
     //Set built state to error 
     currentBuild.result = "FAILURE" 

     //Send error notification mail 
     mail body: ${err}, 
     charset: 'UTF-8', 
     from: ${notificationEmailSender}, 
     mimeType: 'text/plain', 
     replyTo: ${notificationEmailSender}, 
     subject: '"${projectName}" meet an error', 
     to: ${notificationEmailRecipients} 

     throw err 
    } 
} 

这是正常的,或者它我的定义文件中有错误吗?

回答

2

这是我的错!

我已使变量之间的混乱字符串变量和Groovy代码:

代码:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: ${notificationEmailRecipients}, sendToIndividuals: false]) 

必须是:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: notificationEmailRecipients, sendToIndividuals: false]) 

我在这里不能使用$ {}因为我处于Groovy代码中而不是字符串中。

二错误,邮件正文必须是:

mail body: "Error: ${err}" 

而不是:

mail body: ${err} 

因为ERR这里是一个IOException异常类的实例,而不是一个字符串。

所以最后的代码是:

#!groovy 
node { 

    //Define job context constants 
    def projectName = "JenkinsPipelineTest" 
    def notificationEmailRecipients = "[email protected]" 
    def notificationEmailSender = "[email protected]" 
    currentBuild.result = "SUCCESS" 

    //Handle error that can occur in every satge 
    try { 

      //Some others stage... 

      stage 'Finalization' 
      step([$class: 'ArtifactArchiver', artifacts: '*.zip, *.tar, *.exe, *.html', excludes: null]) 
      step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: notificationEmailRecipients, sendToIndividuals: false]) 
    } 
    catch (err) { 
     //Set built state to error 
     currentBuild.result = "FAILURE" 

     //Send error notification mail 
     mail body: "Error: ${err}", 
     charset: 'UTF-8', 
     from: notificationEmailSender, 
     mimeType: 'text/plain', 
     replyTo: notificationEmailSender, 
     subject: '${projectName} meet an error', 
     to: notificationEmailRecipients 

     throw err 
    } 
} 

希望这个答案会有所帮助。