-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
139 lines (133 loc) · 4.74 KB
/
Jenkinsfile
File metadata and controls
139 lines (133 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Jenkins Pipeline for building, testing, and deploying a Java web application to Tomcat
pipeline {
agent any
// Define global options for the pipeline
options {
timestamps()
buildDiscarder(logRotator(numToKeepStr: '20'))
ansiColor('xterm')
}
// Define the tools required for the build
tools {
jdk 'Java_17' // Java version make sure to configure in Jenkins global tools
maven 'Maven_3.8.4' // Maven version make sure to configure in Jenkins global tools
}
// Define environment variables
environment {
APP_NAME = "NumberGuessGame-1.0-SNAPSHOT" // war name without .war
TOMCAT_HOME = "/opt/tomcat" // tomcat home directory
TOMCAT_WEBAPPS = "/opt/tomcat/webapps" // tomcat webapps dir
DEPLOY_MODE = "local" // Local deployment
}
// Define the stages of the pipeline
stages {
// Checkout the code from SCM
stage('01 – Checkout Code') {
steps {
checkout scm
}
}
// Build the application using Maven
stage('02 – Build with Maven') {
steps {
sh 'mvn -version'
sh 'mvn -B -DskipTests=false clean compile'
}
}
// Run unit tests and collect results
stage('03 – Run Unit Tests') {
steps {
sh 'mvn -B test'
}
post {
always {
junit allowEmptyResults: false, testResults: 'target/surefire-reports/*.xml'
}
}
}
// Package the application as a WAR file
stage('04 – Package WAR File') {
steps {
sh 'mvn -B war:war'
}
post {
success {
archiveArtifacts artifacts: 'target/*.war', fingerprint: true
}
}
}
// Deploy the WAR to Tomcat
stage('05 – Deploy to Tomcat') {
steps {
script {
// Verify the WAR file exists with the exact expected name
def warFile = "target/${APP_NAME}.war"
if (!fileExists(warFile)) {
error "WAR not found: ${warFile} - build/package failed."
}
echo "Found WAR: ${warFile}"
if (env.DEPLOY_MODE == 'local') {
echo "Deploying using deploy.sh logic to ${env.TOMCAT_WEBAPPS}"
// === UNDEPLOY OLD APP (matches deploy.sh) ===
echo "[1/3] Removing previous deployment (if any)..."
sh """
rm -rf "${TOMCAT_WEBAPPS}/${APP_NAME}" \\
"${TOMCAT_WEBAPPS}/${APP_NAME}.war" || true
"""
// === DEPLOY NEW WAR (matches deploy.sh) ===
echo "[2/3] Copying new WAR to Tomcat webapps..."
sh """
cp "${warFile}" "${TOMCAT_WEBAPPS}/${APP_NAME}.war"
"""
// === RESTART TOMCAT (matches deploy.sh) ===
echo "[3/3] Restarting Tomcat..."
sh """
sudo "${TOMCAT_HOME}/bin/shutdown.sh" || true
sleep 2
sudo "${TOMCAT_HOME}/bin/startup.sh"
"""
} else if (env.DEPLOY_MODE == 'ssh') { // SSH deployment
sshagent([env.REMOTE_SSH_CREDENTIALS]) {
sh "scp -o StrictHostKeyChecking=no ${war} jenkins@${env.REMOTE_HOST}:${env.TOMCAT_WEBAPPS}/"
}
} else if (env.DEPLOY_MODE == 'manager') { // Tomcat Manager deployment
withCredentials([usernamePassword(credentialsId: env.TOMCAT_MANAGER_CREDENTIALS, usernameVariable: 'TM_USER', passwordVariable: 'TM_PASS')]) {
def contextPath = "/${APP_NAME}"
sh """
curl -s -u ${TM_USER}:${TM_PASS} "${TOMCAT_MANAGER_URL}/undeploy?path=${contextPath}" || true
curl -s -u ${TM_USER}:${TM_PASS} --upload-file ${war} "${TOMCAT_MANAGER_URL}/deploy?path=${contextPath}&update=true"
"""
}
} else {
error "Unknown DEPLOY_MODE: ${env.DEPLOY_MODE}"
}
}
}
}
// Simple smoke test to verify deployment
stage('06 – Smoke Test') {
steps {
script {
def url = "http://localhost:8081/${APP_NAME}/"
echo "Running smoke test against ${url}"
echo "Deployment complete!"
echo "App available at: ${url}"
sleep 10 // Give Tomcat more time to restart and deploy
sh "curl -fsS ${url} > /dev/null || echo 'Application may still be starting'"
}
}
}
}
// Post actions for success/failure notifications and artifact archiving
post {
success {
echo "✅ Pipeline succeeded for build #${env.BUILD_NUMBER}"
}
failure {
echo "❌ Pipeline failed for build #${env.BUILD_NUMBER}"
}
always {
archiveArtifacts allowEmptyArchive: true, artifacts: 'target/surefire-reports/**/*.xml' // Archive test reports
}
}
}