How to Improve Security and Set Up Notifications for Jenkins Builds

Prem ChoudharyPrem Choudhary
2 min read

For 2FA (For enhancing security)

  1. Go to manage jenkins → Plugin →Install MFA/Two-Factor-Authentication(2FA)

  2. Go to manage **jenkins → System → E-mail Notification
    **SMTP server = smtp.gmail.com
    Default user e-mail suffix = @gmail.comFor password go to your gmail account → 2FA →Scroll down - You will see a APP → Generate Password and enter here.

  3. Test the connection.


For Email-notification on every build: (For getting notification on emails)

  1. Go to manage jenkinsPlugin →Install Email extension

  2. Go to manage jenkinsSystemExtended E-mail Notification
    SMTP server = smtp.gmail.com
    Default user e-mail suffix = @gmail.com

    For password go to your gmail account → 2FA →Scroll down - You will see a APP → Generate Password and enter here.

  3. Test the connection.

For build notification on slack: (For getting build information on slack because sometime due workload we are unable to checkout mails)

  1. Go to SlackAutomation → Search for jenkins

  2. Click Add to Slack.

  3. Choose a channel or create a new blank channel.

  4. This will prompt to new page and give a token number copy that.

  5. Go to Manage JenkinsPlugins → Install Slack Notification.

  6. Go to Configure System

  7. Scroll down to Slack .

1. Workspace: workspace_name

2. Credential: add new (Kind: secret text and paste the token in the secret)

pipeline {
    agent any

    parameters {
            <something-here>
        }

    environment {
            <something-here>
    }

    stages {
        stage('Checkout') {
            steps {
                <something-here>
            }
        }

        stage('Run Ansible Playbook') {
            steps {
                script {
                   <something-here>
                    }
                }
            }
        }

        stage('Latest git commit id') {
            steps {
                script {
                    <something-here>
                }
            }
        }
    }

    post {
        success {
            script {
                def buildNumber = currentBuild.number
                def user = currentBuild.getBuildCauses()[0].userId ?: 'Unknown'  
                def buildTime = new Date(currentBuild.getStartTimeInMillis()).format('yyyy-MM-dd HH:mm:ss')

                def emailSubject = "Build #${buildNumber} completed"
                def emailBody = """
                    The build number ${buildNumber} has completed successfully.
                    Triggered by user: ${user}
                    Build completed at: ${buildTime}
                    Git commit ID: ${env.GIT_COMMIT_ID}
                """
               emailext(
                    to: EMAIL_RECIPIENT,
                    subject: emailSubject,
                    body: emailBody
                )
                slackSend (
                    channel: SLACK_CHANNEL,
                    message: "Build #${buildNumber} completed successfully.\nTriggered by: ${user}\nGit commit ID: ${env.GIT_COMMIT_ID}\nBuild completed at: ${buildTime}",
                    color: 'good'
                )
            }
        }

        failure {
            script {
                def buildNumber = currentBuild.number
                def user = currentBuild.getBuildCauses()[0].userId ?: 'Unknown'
                def buildTime = new Date(currentBuild.getStartTimeInMillis()).format('yyyy-MM-dd HH:mm:ss')

                def emailSubject = "Build #${buildNumber} failed"
                def emailBody = """
                    The build number ${buildNumber} has failed.
                    Triggered by user: ${user}
                    Build failed at: ${buildTime}
                    Git commit ID: ${env.GIT_COMMIT_ID}
                """

                emailext(
                    to: EMAIL_RECIPIENT,
                    subject: emailSubject,
                    body: emailBody
                )

                slackSend (
                    channel: SLACK_CHANNEL,
                    message: "Build #${buildNumber} failed.\nTriggered by: ${user}\nGit commit ID: ${env.GIT_COMMIT_ID}\nBuild failed at: ${buildTime}",
                    color: 'danger'
                )
            }
        }
    }
}

Hari Om Tat Sat🕉❤️.

10
Subscribe to my newsletter

Read articles from Prem Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prem Choudhary
Prem Choudhary

DEVOPS ENGINEER | AWS | Java | Linux | Python | Git | Github | Docker | Spring Boot | Ansible | Jenkins | Algorithmic Trader | Pine Editior