Variable in jenkins pipe

To make my jenkins pipeline definition file more customizable, I try to use a maximum of variables.

When I try to use a variable in the mail or step command, jenkins throws this error:

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]

This is my pinyline definition file of my jenkins:

#!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
    }
}

Is this normal or is it me who has an error in my definition file?

+4
source share
1 answer

That was my fault!

I made a confusion between the variable in the string and the variable and Groovy code:

the code:

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

Must be:

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

I should not use $ {} here , because I'm in Groovy code, not a line.

, :

mail body: "Error: ${err}"

:

mail body: ${err}

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
    }
}

, .

+3

Source: https://habr.com/ru/post/1650402/


All Articles