2016-08-06 6 views
3

Чтобы сделать файл определения конвейера моего 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} 

Поскольку эээ здесь является экземпляром класса 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 
    } 
} 

Надежда этот ответ поможет.

Смежные вопросы