How to Improve Security and Set Up Notifications for Jenkins Builds


For 2FA (For enhancing security)
Go to manage jenkins → Plugin →Install MFA/Two-Factor-Authentication(2FA)
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.Test the connection.
For Email-notification on every build: (For getting notification on emails)
Go to manage jenkins → Plugin →Install Email extension
Go to manage jenkins → System →Extended 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.
Test the connection.
For build notification on slack: (For getting build information on slack because sometime due workload we are unable to checkout mails)
Go to Slack → Automation → Search for jenkins
Click Add to Slack.
Choose a channel or create a new blank channel.
This will prompt to new page and give a token number copy that.
Go to Manage Jenkins → Plugins → Install Slack Notification.
Go to Configure System
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🕉❤️.
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