jenkins管道中的变量

rig*_*tod 5 groovy jenkins 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]
Run Code Online (Sandbox Code Playgroud)

这是我的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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

rig*_*tod 6

这都怪我!

我在字符串和变量以及Groovy代码中的变量之间产生了混淆:

码:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: ${notificationEmailRecipients}, sendToIndividuals: false])
Run Code Online (Sandbox Code Playgroud)

一定是:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: notificationEmailRecipients, sendToIndividuals: false])
Run Code Online (Sandbox Code Playgroud)

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

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

mail body: "Error: ${err}"
Run Code Online (Sandbox Code Playgroud)

并不是:

mail body: ${err}
Run Code Online (Sandbox Code Playgroud)

因为这里的错误是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
    }
}
Run Code Online (Sandbox Code Playgroud)

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