jenkins管道中的变量

时间:2016-08-06 11:49:46

标签: jenkins groovy jenkins-pipeline

为了使我的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]

这是我的jenkins pipleline定义文件:

#!groovy
node {

    //Define job context constants
    def projectName = "JenkinsPipelineTest"
    def notificationEmailRecipients = "aaa@domain.com"
    def notificationEmailSender = "bbb@domain.com"
    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
    }
}

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

1 个答案:

答案 0 :(得分:5)

这是我的错!

我在字符串和变量以及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 = "aaa@domain.com"
    def notificationEmailSender = "bbb@domain.com"
    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
    }
}

希望这个答案有所帮助。