Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable in jenkins pipeline

In order to made my jenkins pipeline definition file more customizable, I try to use a maximum of variables.

When I try to use variable in mail or step instruction jenkins throw 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 jenkins pipleline definition file:

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

It's normal or it's me that have an error in my definition file ?

like image 666
righettod Avatar asked Aug 06 '16 11:08

righettod


People also ask

How do I set environment variables in Jenkins pipeline?

Jenkins pipeline environment variables: Pipeline environment variables can be defined in the environment section. This section can be defined globally on the entire pipeline or in the specific pipeline stage. You can define your environment variables in both — global and per-stage — simultaneously.

How to define a variable in a Jenkins file?

Posted on June 18, 2019 by admin Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using $ {...} syntax.

What are the different types of pipelines in Jenkins?

In Jenkins 2.138.3 there are two different types of pipelines. Declarative and Scripted pipelines. "Declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax.

How to inject environment variables in a pipeline block?

You need to define variables before the pipeline block starts. Then it should be work. The variable must be defined in a script section. You can also use environment block to inject an environment variable. Thanks for contributing an answer to DevOps Stack Exchange! Please be sure to answer the question.


1 Answers

It was my fault!

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

Code:

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

Must be:

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

Here I must not use the ${} because i'm in the Groovy code and not in a string.

Second error, the mail body must be:

mail body: "Error: ${err}"

And not:

mail body: ${err}

Because err here is a IOException class instance and not a string.

So final code is:

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

Hope this answer will help.

like image 100
righettod Avatar answered Sep 23 '22 14:09

righettod