Skip to content

Instantly share code, notes, and snippets.

@vijayakumar-psg587
Last active November 10, 2018 12:06
Show Gist options
  • Save vijayakumar-psg587/4e37efe4daf0cd8dbaf153da94e01fea to your computer and use it in GitHub Desktop.
Save vijayakumar-psg587/4e37efe4daf0cd8dbaf153da94e01fea to your computer and use it in GitHub Desktop.
pipeline {
agent any
tools {
maven 'localMaven'
git 'localGit'
jdk 'localJava'
/*
For opehsnift jenkins
maven 'MAVEN-3.5.4'
git 'GIT'
jdk 'JDK'*/
}
stages{
stage('Init'){
steps{
sh '''
echo "JAVA_HOME = ${JAVA_HOME}"
echo "PATH = ${PATH}"
echo "MAVEN_HOME = ${M2_HOME}"
'''
echo "Hellow world in init"
println "Init success.."
}
}
stage('Build'){
steps{
echo "Starting build ...."
sh 'mvn clean install -DskipTests -Dmaven.test.failure.ignore=true'
println "BUILD NUMBER = $BUILD_NUMBER"
println "Build Success.."
}
post {
always {
/*
java.lang.ClassCastException: hudson.plugins.emailext.EmailExtStep.body expects class java.lang.String but received class java.util.ArrayList
body:[[$class:'ExtendedEmailPublisher']],*/
notifyThroughEmail('Build-stage');
notifyThroughSlack('Build-Stage');
}
}
}
stage('Package'){
steps{
println "Starting package of war file..."
sh 'mvn clean package -DskipTests'
println "$JENKINS_HOME $JENKINS_URL $BUILD_URL"
println "Packaging Success.."
}
post{
always{
notifyThroughEmail('Package-stage');
notifyThroughSlack('Package-Stage');
}
}
}
stage('Publish'){
steps{
println('Starting publish..');
script{
def server = Artifactory.server('XXX-XXX-artifactory');
println("here is the server: "+server);
println("here is the server: ${server}");
}
/*
Here script block is used to add groovy scripts to the DSL
script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}*/
}
post {
always {
notifyThroughEmail('Publish-stage');
notifyThroughSlack('Publish-Stage');
/*
[DependencyCheck] Collecting Dependency-Check analysis files...
[DependencyCheck] Searching for all files in /var/lib/jenkins/jobs/my-prj-pipeline/workspace that match the pattern (** is also provided)/dependency-check-report.xml
[DependencyCheck] No files found. Configuration error?
Skipping warnings blame since pipelines do not have an SCM link
*/
dependencyCheckPublisher(canComputeNew: false, defaultEncoding: '', healthy: 'HEALTHY', pattern: '', unHealthy: '');
}
}
}
}
}
def notifyThroughSlack(String stage= "Default stage") {
// build status of null means successful
def buildStatus = "${currentBuild.currentResult}";
// Default values
def colorName = 'RED'
def colorCode = '##33ccff'
def subject = " Job - '${env.JOB_NAME}' Build - '[${env.BUILD_NUMBER}]'" + "";
def summary = """
Job - '${env.JOB_NAME}' Build - '[${env.BUILD_NUMBER}]' Stage - ${stage}
Result: ${currentBuild.currentResult}
Please refer the url for complete details - (<${env.BUILD_URL}| Jenkins>)
"""
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESS') {
color = 'GREEN'
colorCode = '#ff99ff'
} else {
color = 'RED'
colorCode = '#FF0000'
}
// Send notifications
slackSend (color: colorCode, message: summary)
}
def notifyThroughEmail(String stage= "Default stage"){
//$BUILD_LOG is available in openshift jenkins. not sure why it is not available in local
emailext (body:"""
Adtech-Service - Build # $BUILD_NUMBER - $currentBuild.currentResult:
$BUILD_LOG maxLines=9000, escapeHtml=true
Check console output at $BUILD_URL to view the results.
""",
compressLog: true,
attachLog: true,
replyTo: 'XXXX',
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "Build Notification Jenkins - Project : Adtech-service - Job: $JOB_NAME Build # $BUILD_NUMBER ${currentBuild.currentResult}",
to: 'XXXX');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment