Friday, May 20, 2022

Setup Maven Project using Pipeline as code in Jenkins

Description: In the previous blog, We have setup pipeline manually to setup maven project on Tomcat 9. Here I am going to setup Maven project using Pipeline as code as Jenkins file 

Setup:

  1. Jenkins server [localhost]
  2. Tomcat Prod server  [Tomcat URL http://52.23.238.138:8080/]
  3. Tomcat Stage server [Tomcat URL http://54.91.86.53:8080]
  4. Github Project URL : https://github.com/harpal1990/sampletomcat.git
Jenkins Plugins

  1. Maven Integration Plugin 
  2. Build Pipeline Plugin
  3. Copy Artifact Plugin 
  4. Deploy to container Plugin 


Code Editor:
  1. VSCode [JenkinsFile Extension]
Process: As previous blog, we will create 4 Project stage to manage maven project as follow

  1. Project-Test                 == In this Project source code from git cloned and test using maven
  2. Project-Build               ==  In this Project build created after test
  3. Project-Deploy-Stage  == Here deploy build to staging environment with automatic
  4. Project-Deploy-Prod   ==  Here deploy build to Production environment by manual approval
Maven Installation:  To run maven commands, we need to install maven at Jenkins level. To install maven navigate to Global Tool Configuration --> Maven Installation and fill details as follow


Note: Here I have defined maven installation as "Maven" which I will use in code as follow

pipeline{
agent any
tools {
maven 'Maven'
}

After defined maven command, we can use in maven test and build source code as follow

stages{
stage("Project-Test"){
steps{
sh "mvn test"
}
}

stage("Project-Build"){
steps{
sh "mvn package"
}
}

 
Once Test and Build Project completed, Now we need to deploy to stage and Prod instance respectively. In previous blog we used plugin called "Deploy to Container" 

Here to create code for deploy to container, I am going to use "Pipeline Syntax" under build Trigger as follow






















Once you click on Pipeline Syntax use Deploy war/ear to container in sample step and fill all the require details as follow





















After filled all the details click on Generate Pipeline stage, it will generate code as follow 



Deploy to stage code looks like as follow

stage("Project-Deploy-Stage"){
steps{
deploy adapters: [tomcat9(credentialsId: 'latestcred', path: '', url: 'http://54.91.86.53:8080')], contextPath: '/techserver', war: '**/*.war'
}
}





Same Code use for Production environment just changed the URL and credentials. After fill all the details, I am uploading below code to my github repository with the name of Jenkinsfile

pipeline{ agent any tools { maven 'Maven' } stages{ stage("Project-Test"){ steps{ sh "mvn test" } } stage("Project-Build"){ steps{ sh "mvn package" } } stage("Project-Deploy-Stage"){ steps{ deploy adapters: [tomcat9(credentialsId: 'latestcred', path: '', url: 'http://54.91.86.53:8080')], contextPath: '/techserver', war: '**/*.war' } } stage("Project-Deploy-Prod"){
            
        // Define below for approval
        input{
message "Successfully checked on stage, Should we continue on prod?"
ok "Yes we should"
}
steps{ deploy adapters: [tomcat9(credentialsId: 'latestcred', path: '', url: 'http://52.23.238.138:8080')], contextPath: '/techserver', war: '**/*.war' } } } post{ always{ echo "========always========" } success{ echo "========pipeline executed successfully ========" } failure{ echo "========pipeline execution failed========" } } }

Once file uploaded to git repository, Create new project with Pipeline as follow 




















After click ok, select Pipeline script from SCM to deploy from git and give repository URL as follow






















After save the pipeline click on Build Now, once build completed it shows result as follow


















Here we have deployed on Prod and Staging environment without manual input. So I am going to add manual approval for Production deployment as follow and then try to build again

stage("Project-Deploy-Prod"){
input{
message "Successfully checked on stage, Should we continue on prod?"
ok "Yes we should"
}
steps{
deploy adapters: [tomcat9(credentialsId: 'latestcred', path: '', url: 'http://52.23.238.138:8080')], contextPath: '/techserver', war: '**/*.war'
}
}


After adding Input it will ask for approval as follow. Once you approve it will deploy to prod






















After successfully approve, we can check both prod and staging URLs and it shows the output as follow



No comments:

Post a Comment