DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Jenkins Pipeline Script to Build and Deploy Application on Web Server

Jenkins Pipeline Script to Build and Deploy Application on Web Server

Jenkins Pipeline as script provides us with a more flexible way to create our pipeline. Let's discuss Jenkins Pipeline script to build and deploy apps.

Koushik Saha user avatar by
Koushik Saha
·
Dec. 11, 22 · Tutorial
Like (1)
Save
Tweet
Share
3.53K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will discuss the 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 and scripted type. Here, we will use declarative Jenkins Pipeline script.

Prerequisite Knowledge

  • Git, GitHub
  • Building Java applications 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 this article first.

Introduction

Here we will deploy a sample Java application on top of Tomcat. To automate the process, we will create the pipeline using Jenkins. And we will create a Jenkins Pipeline as script, which has tremendous benefits.

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 a pipeline tool.
  • JFrog Artifactory – we are storing the build artifacts in JFrog Artifactory.
  • Tomcat web server – we will deploy our sample Java application on a Tomcat web server.

Block Diagram of the Pipeline

There are two options:

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

Option 1: Push the Build Artifacts to Repository and Then Download from Repository on the Tomcat Host

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

Option 2: Send the Build Artifacts to Tomcat Host Using SCP

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

Jenkins Configuration

Before going into the Jenkins Pipeline script, we need to ensure the 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, we configured JFrog Artifactory with Jenkins. For this, we installed the JFrog plugin in Jenkins and configured the details under "Manage Jenkins → Configure System."
  3. On the Jenkins server, we need to generate SSH keys, and we need to place the public key of the Jenkins server into the Tomcat host so that Jenkins can connect with the Tomcat host without a 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 this article.

Jenkins Pipeline Script

Below is the Jenkins Pipeline script:

Groovy
 
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 the credential.

Stage: Build

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

Stage: Push Artifacts into Artifactory

In this stage, build artifacts are pushed into Artifactory.

Stage: Pull Artifacts and Deploy on Tomcat

In this stage, artifacts are pulled from Artifactory and deployed on a Tomcat web server.

 Thank you.

Apache Maven Apache Tomcat Jenkins (software) Pipeline (software) DevOps

Published at DZone with permission of Koushik Saha. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Spring Boot, Quarkus, or Micronaut?
  • REST vs. Messaging for Microservices
  • HTTP vs Messaging for Microservices Communications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: