Thursday, December 27, 2018

Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, deploy WARs using Jenkins Pipeline - Build pipelines integrate with Bitbucket, Sonarqube, Slack, JaCoCo, Nexus, Tomcat

What are Pipelines in Jenkins?

- Pipelines are better than freestyle jobs, you can write a lot of complex tasks using pipelines when compared to Freestyle jobs.
- You can see how long each stage takes time to execute so you have more control compared to freestyle.
- Pipeline is groovy based script that have set of plug-ins integrated for automating the builds, deployment and test execution.
- Pipeline defines your entire build process, which typically includes stages for building an application, testing it and then delivering it. 
 - You can use snippet generator to generate pipeline code for the stages you don't know how to write groovy code.
- Pipelines are two types - Scripted pipeline and Declarative pipeline. Click here to know the difference between them.

Install plug-ins
1. Install Pipeline stage view, Deploy to container, Slack, Jacoco, Nexus Artifact Uploader and SonarQube, pipeline stage view plug-ins (if already installed, you can skip it)

Steps to Create Scripted Pipeline in Jenkins

1. Login to Jenkins

2. Create a New item

3. Give name as MyfirstPipelineJob and choose pipeline

4. Click ok. Pipeline is created now

5. Under build triggers, click on poll SCM, schedule as

H/02 * * * *

6. Go to Pipeline definition section, click on Pipeline syntax link. under sample step drop down, choose checkout: Checkout from version control. enter bitbucket or GitHub Repository URL, and enter right credentials. Click here to learn to use PAT if you are using GitHub. scroll down, click on Generate Pipeline script. Copy the code.

7. Now copy the below pipeline code highlighted section into Pipeline section in the pipeline. Please copy stage by stage

8. Change Maven3, SonarQube variables and also Slack channel name as highlighted above in red as per your settings.

9. For Nexus Upload stage, You need to change the Nexus URL and credentials ID for Nexus (which you can grab from Credentials tab after login)

10. For Dev Deploy stage, you can copy credentials ID used for connecting to Tomcat.

Pipeline Code:

node {

    def mvnHome = tool 'Maven3'
    stage ("checkout")  {
       copy code here which you generated from step #6

   stage ('build')  {
    sh "${mvnHome}/bin/mvn clean install -f MyWebApp/pom.xml"

     stage ('Code Quality scan')  {
       withSonarQubeEnv('SonarQube') {
       sh "${mvnHome}/bin/mvn -f MyWebApp/pom.xml sonar:sonar"
   stage ('Code coverage')  {

   stage ('Nexus upload')  {
        nexusVersion: 'nexus3',
        protocol: 'http',
        nexusUrl: 'nexus_url:8081',
        groupId: 'myGroupId',
        version: '1.0-SNAPSHOT',
        repository: 'maven-snapshots',
        credentialsId: '2c293828-9509-49b4-a6e7-77f3ceae7b39',
        artifacts: [
            [artifactId: 'MyWebApp',
             classifier: '',
             file: 'MyWebApp/target/MyWebApp.war',
             type: 'war']
   stage ('DEV Deploy')  {
      echo "deploying to DEV Env "
      deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'


  stage ('Slack notification')  {
    slackSend(channel:'channel-name', message: "Job is successful, here is the info -  Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")

   stage ('DEV Approve')  {
            echo "Taking approval from DEV Manager for QA Deployment"     
            timeout(time: 7, unit: 'DAYS') {
            input message: 'Do you approve QA Deployment?', submitter: 'admin'

stage ('QA Deploy')  {
     echo "deploying into QA Env " 
deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'


  stage ('QA notify')  {
    slackSend(channel:'channel-name', message: "QA Deployment was successful, here is the info -  Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")

stage ('QA Approve')  {
    echo "Taking approval from QA manager"
    timeout(time: 7, unit: 'DAYS') {
        input message: 'Do you want to proceed to PROD Deploy?', submitter: 'admin,manager_userid'

stage ('PROD Deploy')  {
     echo "deploying into PROD Env " 
deploy adapters: [tomcat9(credentialsId: '4c55fae1-a02d-4b82-ba34-d262176eeb46', path: '', url: 'http://your_tomcat_url:8080')], contextPath: null, war: '**/*.war'


11. Click Apply, Save
12. Now click on Build. It should execute all the stages and show pipeline view like this.

You can watch the Scripted pipeline video in my YouTube channel:


  1. Thank you Coach, because of you and these labs, i'm going to be exceptional in cloud engineering.
    DevOps Training in Chennai
    DevOps Certification in Chennai
    AWS Training in Chennai
    AWS course in Chennai
    RPA Training in Chennai
    DevOps Training in Velachery
    DevOps Training in Tambaram
    DevOps course in Chennai

  2. Thank you sir for sharing ur knowledge.Your labs practicals are exceptional.God bless you!

  3. I blog frequently and I genuinely thank you for your information. This great article has really peaked my interest. I will book mark your blog and keep checking for new details about once per week. subject I opted in for your Feed as well.

  4. They’re extremely organized, efficient, and design firm San Francisco provide a personal touch that creates an enjoyable working relationship

  5. This is a good tip particularly to those fresh to the blogosphere. Short but very accurate info… Many thanks for sharing this one. A must read post!

  6. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. idraulico viareggio

  7. We appreciated their honesty and transparency, which enabled both teams to challenge one another…
    marketing and branding companies

  8. Any time you connect with the administrations of an expert, you are basically going into an agreement. Since an agreement is lawfully restricting, you need to guarantee that you are ensured as to legitimate issue. The primary thing you would need to check is that the handyman has a substantial permit.Kitchen Plumbing Gladesville

  9. Fantastic Blog to learn CI_CD concepts. I love the aws_eks_jenkins. I was wondering if you have done a similar pipeline including terraform, meaning terraform, aws_eks_jenkins pipeline

  10. They are very consistent and they care very highly about what their clients think.
    mobile app design services

  11. The features a small yet attentive team with distinguished creative design capabilities.
    digital branding companies

  12. The team's professionalism was stellar, along with their deep understanding of how software should look and function. They're a skilled, quick team.
    best UI UX design

  13. very help full thank you its working. now am able to create a war file, deployed the war file on tomcat docker platform, which is also moved to aws ecr and deployed as aws eks pods..

    i want to automate those activities currently am using sh script to partially automated it required manual interruption kindly educate me to single touch automation. using platform git, jenkins, docker, ecr & finally eks.

  14. I sent your articles links to all my contacts and they all adore it including me.
    logo design firm

  15. I will prefer this blog because it has much more informative stuff.
    best web designers in the world

  16. Great! We will be connecting to this enormous post on our site. Continue the good writing.
    best web design agencies

  17. This blog is very informative the stuff you provide I really enjoyed reading.
    UI UX consulting

  18. Very Nice Article.
    How can we achieve exactly the same stages with declarative pipeline ?