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