0

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
justin xa
  • 105
  • 1
  • 11

2 Answers2

0

When the stage builds are done the assign variables values are not passing to the post-build action. As a solution what you can do is pass the required values to file and then by using Environment Inject Plugin you can access the value in post action. Environment Inject Plugin job configurations. plugin setup

pipeline {
    agent any
    environment {
                ENV_STAGE = 'dev'
                UNTTST_SNRSCN = 'false'
                SNRSCN_OUTPUT = ''  
                BRANCHNAME = 'development'
                EMAIL_RECIPIENTS = 'abc@xyz.com'    
                }

    stages {
        stage ("Set scan output"){
           steps {
                script {
                    if (env.UNTTST_SNRSCN == 'true') {
                       def SNRSCN_OUTPUT = 'Unit test and sonar scanning performed.'
                       sh "echo $SNRSCN_OUTPUT"
                       writeFile file: 'result.txt', text: SNRSCN_OUTPUT
                    } else if (env.UNTTST_SNRSCN == 'false') {
                        def SNRSCN_OUTPUT = 'Unit test and sonar scanning not performed.'
                        sh "echo $SNRSCN_OUTPUT"
                        writeFile file: 'result.txt', text: SNRSCN_OUTPUT
                    }

                }
            }
        }
    }

   post {
       always{
        script {
            def readContent = readFile 'result.txt'
            echo "The result is ${readContent}"    
       }
     }
   }
}
RidmiR
  • 1
  • 1
0

You can do the following changes in script to pass custom env values to email body in post stage in jenkins pipeline:

Change in stage: Set scan output

stage ("Set scan output"){
                                    steps {
                                        script {
                                            if (env.UNTTST_SNRSCN == 'true') {
                                            def SNRSCN_OUTPUT = 'Unit test and sonar scanning performed.'
                                            sh "echo $SNRSCN_OUTPUT"
                                            env.SNRSCN_OUTPUT = SNRSCN_OUTPUT
                                            } else if (env.UNTTST_SNRSCN == 'false') {
                                            def SNRSCN_OUTPUT = 'Unit test and sonar scanning not performed.'
                                            sh "echo $SNRSCN_OUTPUT"
                                            env.SNRSCN_OUTPUT = SNRSCN_OUTPUT
                                            }
                                        }
                                    }
                            }

Do not make any changes in post build step.

May be this will resolve your issue.

Sourav
  • 3,025
  • 2
  • 13
  • 29