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.
Join the DZone community and get the full member experience.
Join For FreeIn 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.
- We need to provide
JAVA_HOME
,MAVEN_HOME
in the "Global Tool Configuration" of Jenkins ("Manage Jenkins → Global Tool Configuration"). - 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."
- 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
ORStrictHostKeyChecking 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:
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.
Published at DZone with permission of Koushik Saha. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments