Declarative Jenkins Pipeline to Deploy Java Web Application.

We’ve already seen Jenkins Installation, Running sample Application. In this post we will be getting our hands dirty with real time Java Web application deployment in Tomcat where we will be deploying java .war file using Declarative Jenkins Pipeline.

If you’re not sure What is Jenkins than you’re probably at the wrong place and I would recommend you to go through this.

Before we deep dive into this, you must have some prerequisite to get started, such as

  1. Jenkins Basics.
  2. Groovy Basics.
  3. What is Jenkins Pipeline
  4. How to build java app from command line.
Jenkins CI | Slack App Directory
Jenkins

What is Jenkins Pipeline?

Jenkins Pipeline (or simply “Pipeline” with a capital “P”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. To read more about it you can go through this.

Steps:

1.Install tomcat

2.Go to /opt/tomcat

3.Change permission to 777 web app “chmod 777 /tomcat/webapp” or

4.Change the user owner  “sudo chown -R ubuntu:ubuntu tomcat/”

5.Go to /opt/tomcat/bin

6../ startup.sh

7. In jenkin install sshagent plugin.

8. Generate the pipleline syntax add, credentials username with private key.

9. Add user name “ubuntu” and add private key “copy”

10. Find below jenkins declarative file

pipeline{
  agent any
  stages{
    stage("Git Checkout"){
      steps{
            git credentialsId: 'github', url: 'https://github.com/aditya-malviya/myweb.git'
           }
          }
     stage("Maven Build"){
       steps{
            sh "mvn clean package"
            sh "mv target/*.war target/myweb.war"
             }
            }
     stage("deploy-dev"){
       steps{
          sshagent(['tomcat-dev1']) {
          sh """
          scp -o StrictHostKeyChecking=no target/myweb.war  
          ubuntu@yourip:/opt/tomcat/webapps/
          ssh ubuntu@yourip /opt/tomcat/bin/shutdown.sh
          ssh ubuntu@yourip /opt/tomcat/bin/startup.sh
           """
            }
          }
        }
      }
    }

So this is how we build Java application using Jenkins Declarative Pipleline, Do revert me for any queries.



Categories: DevOps, Jenkins

Tags: , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: