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:
- Jenkins server [localhost]
- Tomcat Prod server [Tomcat URL http://52.23.238.138:8080/]
- Tomcat Stage server [Tomcat URL http://54.91.86.53:8080]
- Github Project URL : https://github.com/harpal1990/sampletomcat.git
Jenkins Plugins
- Maven Integration Plugin
- Build Pipeline Plugin
- Copy Artifact Plugin
- Deploy to container Plugin
Code Editor:
- VSCode [JenkinsFile Extension]
Process: As previous blog, we will create 4 Project stage to manage maven project as follow
- Project-Test == In this Project source code from git cloned and test using maven
- Project-Build == In this Project build created after test
- Project-Deploy-Stage == Here deploy build to staging environment with automatic
- Project-Deploy-Prod == Here deploy build to Production environment by manual approval
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
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 approvalsteps{ 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========" } } }
input{ message "Successfully checked on stage, Should we continue on prod?" ok "Yes we should" }
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