0

I am trying to pass the variable value from one stage to the mail body of jenkins. But the variable value is not reflecting.

The code works if I create another stage and call the variable. But not in the notifyStatusChangeViaEmail method block

Below is the example code.

def variable
pipeline {
agent { label 'master' }
options {
   timestamps()
   timeout(time: 1, unit: 'HOURS' )
}
  stages{
      stage('Variable'){
  steps {
    script{
  variable = "Hi, Build is successful"
      }
   }
  }
 }
}

def notifyStatusChangeViaEmail(prevBuild) {
def subject
def body

if (currentBuild.previousBuild != null) {
    switch (prevBuild.result) {
        case 'FAILURE':
            emailext attachLog: true, body: "${variable}", recipientProviders: [[$class: 'CulpritsRecipientProvider']], subject: 'Build Status : Build is back to Normal', to: 'sumith.waghmare@gmail.com';
            break

        case 'UNSTABLE':
            emailext attachLog: true, body: "${variable}", recipientProviders: [[$class: 'CulpritsRecipientProvider']], subject: 'Build Status : Build is back to Normal', to: 'sumith.waghmare@gmail.com';
            break

        case 'SUCCESS':
            emailext attachLog: true, body: "${variable}", recipientProviders: [[$class: 'CulpritsRecipientProvider']], subject: 'Build Status', to: 'sumith.waghmare@gmail.com';
            break
    }
}
}
Sumith08
  • 582
  • 6
  • 16

1 Answers1

0

The problem is that the variable is not defined in the scope of the function (and this post explains how scope of functions works in groovy).

To fix the issue you're seeing I would either make variable a parameter of notifyStatusChangeViaEmail, or I would remove the def variable statement, because that would make variable a global variable and so the function would be able to access it.

Examples:

Making the variable, a parameter of notifyStatusChangeViaEmail:

pipeline {
agent { label 'master' }
options {
   timestamps()
   timeout(time: 1, unit: 'HOURS' )
}
  stages {
    stage('Send Email') {
      steps {
        script {
          def emailBody = "Hi, Build is successful"

          // I assume that prevBuild is defined somewhere already

          notifyStatusChangeViaEmail(prevBuild, emailBody)
        }
      }
    }
  }
}

def notifyStatusChangeViaEmail(prevBuild, emailBody) {
  if (currentBuild.previousBuild != null) {
    switch (prevBuild.result) {
        case 'FAILURE':
            emailext attachLog: true, 
                     body: emailBody, 
                     recipientProviders: [[$class: 'CulpritsRecipientProvider']], 
                     subject: 'Build Status : Build is back to Normal', 
                     to: 'sumith.waghmare@gmail.com';
            break

      //...
    }
  }
}

Using variable as a global:

// --> I deleted the "def variable" line <--


pipeline {
agent { label 'master' }
options {
   timestamps()
   timeout(time: 1, unit: 'HOURS' )
}
  stages {
    stage('Variable') {
      steps {
        script {
          // There is no "def" when you first assign a value to
          // `variable`, so it will become a global variable, 
          // which is a variable you can use anywhere in your file
          variable = "Hi, Build is successful"
        }
      }
    }
  }
}
def notifyStatusChangeViaEmail(prevBuild) {
if (currentBuild.previousBuild != null) {
    switch (prevBuild.result) {
        case 'FAILURE':
            emailext attachLog: true, body: variable, recipientProviders: [[$class: 'CulpritsRecipientProvider']], subject: 'Build Status : Build is back to Normal', to: 'sumith.waghmare@gmail.com';
            break

     //...
    }
  }
}

Using variable as a parameter:

Vasiliki Siakka
  • 1,185
  • 8
  • 15