Phase 5: Setup email notifications

Table of contents

Make sure you have 2 factor authentication enabled.

Get App password

create and get the passkey.

post {
    always {
        emailext attachLog: true,
        subject: "${currentBuild.result}",
        body: """
            Project: ${env.JOB_NAME}<br/>
            Build Number: ${env.BUILD_NUMBER}<br/>
            URL: ${env.BUILD_URL}<br/>
        """,
        to: 'youremail@gmail.com',
        attachmentsPattern: 'trivyfs.txt,trivyimage.txt'
    }
}

Build the pipeline.

This my overall pipeline till sending notifications in mail.


pipeline{
    agent any
    tools{
        jdk 'jdk17'
        nodejs 'node16'
    }
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }
    stages {
        stage('clean workspace'){
            steps{
                cleanWs()
            }
        }
        stage('Checkout from Git'){
            steps{
                git branch: 'main', url: 'https://github.com/N4si/DevSecOps-Project.git'
            }
        }
        stage("Sonarqube Analysis "){
            steps{
                withSonarQubeEnv('sonar-server') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Netflix \
                    -Dsonar.projectKey=Netflix '''
                }
            }
        }
        stage("quality gate"){
           steps {
                script {
                    waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-token' 
                }
            } 
        }
        stage('Install Dependencies') {
            steps {
                sh "npm install"
            }
        }
        stage('OWASP FS SCAN') {
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
        stage('TRIVY FS SCAN') {
            steps {
                sh "trivy fs . > trivyfs.txt"
            }
        }
        stage("Docker Build & Push"){
            steps{
                script{
                   withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){   
                       sh "docker build --build-arg TMDB_V3_API_KEY=e79e5f0be51bce34b39d6693b68c7ffb -t netflix ."
                       sh "docker tag netflix aasifa/netflix:latest "
                       sh "docker push aasifa/netflix:latest "
                    }
                }
            }
        }
        stage("TRIVY"){
            steps{
                sh "trivy image aasifa/netflix:latest > trivyimage.txt" 
            }
        }
        stage('Deploy to container'){
            steps{
                 // Stop and remove any existing container named 'Netflix'
                sh 'docker stop Netflix || true'
                sh 'docker rm Netflix || true'
                sh 'docker run -d --name Netflix -p 8081:80 aasifa/netflix:latest'
            }
        }
    }
}

Please update necessary values like mail id, dockerhub usernames etc.