Below is my script for jenkins pipeline where i am changing one of the env value in one of the stages and using those custom env in post email body.
pipeline {
agent none
environment {
ENV_STAGE = 'dev'
UNTTST_SNRSCN = 'false'
SNRSCN_OUTPUT = ''
BRANCHNAME = 'development'
EMAIL_RECIPIENTS = 'abc@xyz.com'
}
stages {
stage("Sonar-api"){
when{
expression { params.UNTTST_SNRSCN == 'true' }
}
agent{ label "worker" }
stages{
stage('SonarQube analysis') {
steps {
script {
scannerHome = tool 'sonar-scanner';
}
withSonarQubeEnv('sonarqube9000') {
git branch: '${BRANCHNAME}', credentialsId: '', url: ''
dir ("$WORKSPACE/api/") {
sh "npm install && npm install -D typescript"
sh "npm run test"
sh """/opt/sonar-scanner/bin/sonar-scanner"""
sh "sleep 20"
}
}
}
}
stage ("Clearing Workspace"){
steps{
cleanWs()
}
}
}
}
stage("Build and deploy"){
agent{ label 'worker' }
stages {
stage("Checkout Sourcecode") {
steps{
git branch: "${BRANCHNAME}", credentialsId: '', url: ''
}
}
stage ("Set scan output"){
steps {
script {
if (env.UNTTST_SNRSCN == 'true') {
def SNRSCN_OUTPUT = 'Unit test and sonar scanning performed.'
sh "echo $SNRSCN_OUTPUT"
} else if (env.UNTTST_SNRSCN == 'false') {
def SNRSCN_OUTPUT = 'Unit test and sonar scanning not performed.'
sh "echo $SNRSCN_OUTPUT"
}
}
}
}
stage ("Clearing Workspace"){
steps{
cleanWs()
}
}
}
}
}
post {
failure {
emailext body: """Hi,
Env stage ${env.ENV_STAGE} deployment failed. Please check jenkins error logs and redeploy.
${env.SNRSCN_OUTPUT}
Regards,
DT""", subject: "Deployment #${BUILD_NUMBER} - ${currentBuild.currentResult}", to: "${env.EMAIL_RECIPIENTS}"
}
}
}
I am expecting the value of SNRSCN_OUTPUT in email but i keep getting it as null. How do i achieve this? i want the new value of SNRSCN_OUTPUT from stage 'Set scan output' to be shown in email. Appreciate your valuable inputs.
Subject: Deployment #26 - FAILURE
Hi,
Env stage dev deployment failed. Please check jenkins error logs and redeploy.
null
Regards,
DT