jenkins pipeline script to Build & deploy app on web server

Jenkins pipeline script to build & deploy application on web server

Last Updated on February 18, 2023 by cscontents

Introduction

In this article we will discuss automation of application deployment on a web server using Jenkins pipeline. We will use Jenkins pipeline as a script. Jenkins pipeline as script can be of two types declarative type & scripted type. Here, we will use declarative Jenkins pipeline script.

Prerequisite Knowledge

  • Git, GitHub
  • Building java application using maven
  • Jenkins
  • Web server like tomcat, JBoss etc

If you are new to Jenkins pipeline script, then we would recommend you to go through below article first.

Jenkins pipeline script to build Java application and push artifacts into repository

If you want to learn Jenkins, Docker Kubernetes etc., then I would recommend to check out the below courses from KodeKloud which offers one of the best learning courses in the DevOps world.

Jenkins Course URL: https://kodekloud.com/courses/jenkins/?aff=827846 

Docker Learning Path: https://kodekloud.com/learning-path-docker/?aff=827846

Kubernetes with Hands-on Course URL: https://kodekloud.com/courses/kubernetes-for-the-absolute-beginners-hands-on/?aff=827846

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Introduction

Here we will deploy a sample Java application on top of tomcat. To automate the process, we will create pipeline, and we will create this pipeline using Jenkins. And we will create Jenkins pipeline as script which has tremendous benefit.

Tools/Technology used

  • Java based application – we are using a simple java application.
  • Git – SCM tool.
  • GitHub – our source code is on GitHub.
  • Maven – we are using maven to build the java app.
  • Jenkins – Jenkins is used as pipeline tool.
  • JFrog artifactory – we are storing the build artifacts into JFrog artifactory.
  • Tomcat web server – we will deploy our sample java application on tomcat web server.

Block diagram of the pipeline

There are two option –

  • Option 1: Push the build artifacts to repository and then download it from repository on the tomcat host.
  • Option 2: Send the build artifacts to tomcat host using SCP.

In this article we discussed Option 1.

 

Option 1: Push the build artifacts to repository and then download it from repository on the tomcat host

jenkins pipeline to build, push artifacts into repository and deploy on tomcat

In this case, after build we are pushing the artifacts into repository and downloading it on the tomcat host.

Option 2: Send the build artifacts to tomcat host using SCP

jenkins pipeline to build and depoy artifacts on tomcat

In this case, after build we are sending the artifacts to tomcat host using SCP.

Jenkins Configuration

Before going into the Jenkins pipeline script, we need to ensure below configurations.

  1. We need to provide JAVA_HOME, MAVEN_HOME in the ‘Global Tool Configuration’ of Jenkins (Manage Jenkins → Global Tool Configuration).
  2. Since we are using JFrog artifactory to store the build artifacts, so we configured JFrog artifactory with Jenkins. For this we installed JFrog plugin in Jenkins and configured the details under ‘Mange Jenkins → Configure System’.
  3. On the Jenkins server we need to generate SSH keys, and we need to place the public key of Jenkins server into the tomcat host so that Jenkins can connect with the tomcat host without password. And on the Jenkins server we need to make StrictHostKeyChecking no OR StrictHostKeyChecking accept-new in the /etc/ssh/ssh_config file.

For detailed information, please follow below article.

           How to do SSH to remote Linux machine using SSH key

Jenkins Pipeline Script

Below is the Jenkins pipeline script

pipeline {
    agent any
    tools {
        maven "LocalMVN"
    }
    stages {
        stage('Checkout') {
            steps {
              withCredentials([string(credentialsId: 'GitHub_Token', variable: 'github_token')]) {
                checkout([$class: 'GitSCM',
                          branches: [[name: '*/master']],
                                extensions: [[$class: 'CleanCheckout']],
                                userRemoteConfigs: [[url: 'https://' + env.github_token + '@' + 'github.com/sk617/simple-java-maven-app.git']]
                       ])
              }
            }
        }
        stage('Build') {
            steps {
                sh "mvn -Dmaven.test.failure.ignore=true clean package"
            }
        }
        stage('Push artifacts into artifactory') {
            steps {
              rtUpload (
                serverId: 'my-artifactory',
                spec: '''{
                      "files": [
                        {
                          "pattern": "*.war",
                          "target": "example-repo-local/build-files/"
                        }
                    ]
                }'''
              )
          }
        }
        stage('Pull artifacts & deploy on tomcat') {
            steps{
                  withCredentials([usernamePassword(credentialsId: 'my-artifactory-cred',
                                      usernameVariable: 'USERNAME',
                                      passwordVariable: 'PASSWORD')]) {
                    sh 'ssh ubuntu@149.158.89.34 curl -u ' + USERNAME + ':' + PASSWORD + ' -X GET "Your_JFrog_Artifactory_URL_of_file" --output /opt/tomcat9/webapps/deploy.war'
            }
          }
       }
    }
}

Stage: Checkout

In this stage source code will be fetched from GitHub. If it is a private repo then we need to provide the credential. In the above script we have provided credential.

Stage: Build

In this stage source code will be built using maven. At the end of build it produces a war file.

Stage: Push artifacts into artifactory

In this stage, build artifacts are pushed into artifactory.

Stage: Pull artifacts & deploy on tomcat

In this stage, artifacts are pulled from artifactory and deployed on tomcat web server.

 

Thank You.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.