2016-08-06 217 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 
    } 
} 

希望這個答案會有所幫助。