Thursday, 18 April 2019

Sample Jenkinsfile to create Jenkins Pipeline

#!groovy?

// properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '3']]])

properties(
  [
    office365ConnectorWebhooks([[notifyBackToNormal: true, notifyFailure: true, notifyRepeatedFailure: true, notifySuccess: true, notifyUnstable: true, url: 'https://outlook.office.com/webhook/e20e3b8e-ef2b-44a8-8ed7-70e44725e4f8@e0793d39-0939-496d-b129-198edd916feb/IncomingWebhook/8584f894ef9c4c759c03261f6f3ac987/b41687d2-b80a-40bc-af19-b2b7756570f7']]),buildDiscarder(logRotator(numToKeepStr: '3'))
  ]
)

node(){
    compile()
    // deploy()
 codequality()
    dockerBuild()
 propertiesFile()
    deploy2k8s()
}

def compile(){
 stage('CleanUp') {
  cleanWs()
 }
    stage('Build') {
            checkout scm
            def pom_version = version()
            //currentBuild.displayName =  "${pom_version}-${env.BUILD_NUMBER}"
            //mvn "versions:set -DnewVersion=${currentBuild.displayName}"
            mvn "clean compile test package"  
    }
}


def codequality(){
    pom = readMavenPom file: 'pom.xml'
    def POM_VERSION = pom.version
    def POM_ARTIFACTID = pom.artifactId
  stage('SonarQube analysis') {
    // requires SonarQube Scanner 2.8+
    def scannerHome = tool 'sonar-scanner';
    withSonarQubeEnv('ipssonarqube') {
      sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=${POM_ARTIFACTID} -Dsonar.login=admin -Dsonar.password=admin -Dsonar.java.binaries=target/classes -Dsonar.java.libraries=src/"
    }
 
}

 stage("Quality Gate"){
          timeout(time: 2, unit: 'MINUTES') {
              def qg = waitForQualityGate()
              if (qg.status != 'OK') {
                  error "Pipeline aborted due to quality gate failure: ${qg.status}"
              }
          }
}

}

def deploy(){
    stage("Deploy to Nexus"){
    mvn "deploy  -DaltDeploymentRepository=nexus::default::https://repo/"
    }
}

def dockerBuild(){
    pom = readMavenPom file: 'pom.xml'
    def POM_VERSION = pom.version
    def POM_ARTIFACTID = pom.artifactId
    stage('Docker Image'){
        docker_image = docker.build("dockerhub/test/${POM_ARTIFACTID}:${POM_VERSION}","--build-arg POM_VERSION=${POM_VERSION} --build-arg POM_ARTIFACTID=${POM_ARTIFACTID} .")
 docker_image.push()
 sh "sudo docker system prune -f"
 }
}

def propertiesFile(){
// This section will update the properties file to create Kubernetes configmap
 sh "sed -i 's/{{ KAFKA_BROKER }}/localhost:9092/g' config.sample/application.properties"
 sh "sed -i 's/{{ KAFKA_TOPIC }}/patient/g' config.sample/application.properties"
 sh "sed -i 's/{{ KAFKA_GROUPID }}/poa-gp/g' config.sample/application.properties"
}

def deploy2k8s(){
    pom = readMavenPom file: 'pom.xml'
    def POM_VERSION = pom.version
    def POM_ARTIFACTID = pom.artifactId
   
    stage('Deploy to K8S'){
    
    environment {
    POM_VERSION=${POM_VERSION}
    POM_ARTIFACTID=${POM_ARTIFACTID}
    }
        sh "cp ~/.kube/config config"
     sh "echo '$POM_VERSION and $POM_ARTIFACTID'"
        //sh "kubectl delete -f *.yaml --kubeconfig=config"
  sh "sed -i 's/POM_VERSION/$POM_VERSION/g' app.yaml"
  sh "sed -i 's/POM_ARTIFACTID/$POM_ARTIFACTID/g' app.yaml"
     sh "kubectl delete cm $POM_ARTIFACTID --kubeconfig=config"
  sh "kubectl delete -f app.yaml --kubeconfig=config"
  sh "kubectl create cm $POM_ARTIFACTID --from-file=config.sample/application.properties --kubeconfig=config"
     sh "kubectl apply -f app.yaml --kubeconfig=config" 
    }
}

def mvn(String goals) {
    def mvnHome = tool "maven-3.5.0"
    def javaHome = tool "jdk8"

    withEnv(["JAVA_HOME=${javaHome}", "PATH+MAVEN=${mvnHome}/bin"]) {
        sh "mvn ${goals}"
    }
}

def version() {
    pom = readMavenPom file: 'pom.xml'
    return pom.version
}

No comments:

Post a Comment