Working with Jenkins Pipelines
- In jenkins free style projects, when we install plugins we get UI, with Jenkins 2.0 with plugins we get some steps.
- Lets create a declarative pipeline for spring petclinic project
pipeline {
agent { label 'JDK11' }
options {
timeout(time: 1, unit: 'HOURS')
retry(2)
}
triggers {
cron('0 * * * *')
}
stages {
stage('Source Code') {
steps {
git url: 'https://github.com/GitPracticeRepo/spring-petclinic.git',
branch: 'main'
}
}
stage('Build the Code') {
steps {
sh script: 'mvn clean package'
}
}
stage('reporting') {
steps {
junit testResults: 'target/surefire-reports/*.xml'
}
}
}
post {
success {
// send the success email
echo "Success"
}
unsuccessful {
//send the unsuccess email
echo "failure"
}
}
}
Email Notifications
- For email notifications i will be using fake SMTP Refer Here
- Configure Email Notifications in Jenkins
- Manage Jenkins => Configure System

- Lets configure a email notification


- To make the email or to know information about the job, Jenkins has environment al variables



- Find a way to include environment variable BUILD_ID and JOB_DISPLAY_URL in the email
mail bcc: '', body: 'Build Success', cc: '',
from: 'devops@qtdevops.com', replyTo: '',
subject: 'Build Succeded', to: 'qtdevops@gmail.com'
- Refer Here for the official docs
- Refer the jenkins file below
pipeline {
agent { label 'JDK11' }
options {
timeout(time: 1, unit: 'HOURS')
retry(2)
}
triggers {
cron('0 * * * *')
}
stages {
stage('Source Code') {
steps {
git url: 'https://github.com/GitPracticeRepo/spring-petclinic.git',
branch: 'main'
}
}
stage('Build the Code') {
steps {
sh script: 'mvn clean package'
}
}
stage('reporting') {
steps {
junit testResults: 'target/surefire-reports/*.xml'
}
}
}
post {
success {
// send the success email
echo "Success"
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Succeded", to: 'qtdevops@gmail.com'
}
unsuccessful {
//send the unsuccess email
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Failed", to: 'qtdevops@gmail.com'
}
}
}

* Environmental variables are built in into jenkins, if you need to create your own varaibles. Refer Here

* If you want to give an option to the user who is building the jobs, then we call these variables are parameters.
* Lets look at free style project







* Adding parameter to declartive pipeline

pipeline {
agent { label 'JDK11' }
options {
timeout(time: 1, unit: 'HOURS')
retry(2)
}
triggers {
cron('0 * * * *')
}
parameters {
choice(name: 'GOAL', choices: ['compile', 'package', 'clean package'])
}
stages {
stage('Source Code') {
steps {
git url: 'https://github.com/GitPracticeRepo/spring-petclinic.git',
branch: 'main'
}
}
stage('Build the Code') {
steps {
sh script: "mvn ${params.GOAL}"
}
}
stage('reporting') {
steps {
junit testResults: 'target/surefire-reports/*.xml'
}
}
}
post {
success {
// send the success email
echo "Success"
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Succeded", to: 'qtdevops@gmail.com'
}
unsuccessful {
//send the unsuccess email
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Failed", to: 'qtdevops@gmail.com'
}
}
}
Upstream and downstream jobs
- Lets assume we configure the jenkins jobs in the following way

- How to acheive?
- Option 1: Post Build Actions



- Option 2: Build Triggers

- Now lets try to do this from pipeline
- triggers: upstream

- triggers: upstream
Better approach for running a pipeline
- Lets assume we build spring petclinic on node1 and deploy using node (ansible/terraform/cloudformation/arm/kubernetes)
pipeline {
agent { label 'JDK11' }
options {
timeout(time: 1, unit: 'HOURS')
retry(2)
}
triggers {
cron('0 * * * *')
}
parameters {
choice(name: 'GOAL', choices: ['compile', 'package', 'clean package'])
}
stages {
stage('Source Code') {
steps {
git url: 'https://github.com/GitPracticeRepo/spring-petclinic.git',
branch: 'main'
}
}
stage('Build the Code') {
steps {
sh script: "mvn ${params.GOAL}"
stash name: 'spc-build-jar', includes: 'target/*.jar'
}
}
stage('reporting') {
steps {
junit testResults: 'target/surefire-reports/*.xml'
}
}
stage('deployment') {
agent { label 'JDK8' }
steps {
unstash name: 'spc-build-jar'
echo "clone the latest playbook"
sh 'ansible --version'
}
}
}
post {
success {
// send the success email
echo "Success"
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Succeded", to: 'qtdevops@gmail.com'
}
unsuccessful {
//send the unsuccess email
mail bcc: '', body: "BUILD URL: ${BUILD_URL} TEST RESULTS ${RUN_TESTS_DISPLAY_URL} ", cc: '', from: 'devops@qtdevops.com', replyTo: '',
subject: "${JOB_BASE_NAME}: Build ${BUILD_ID} Failed", to: 'qtdevops@gmail.com'
}
}
}
