-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
101 lines (93 loc) · 3.39 KB
/
Copy pathJenkinsfile
File metadata and controls
101 lines (93 loc) · 3.39 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
pipeline {
agent {
label 'built-in'
}
// Using Maven via Global Configuration
// Tells Jenkins to auto-install and use this Maven version configured in Global Tool Configuration
tools {
maven 'Maven-3.9.16'
}
options {
// Keeps the Jenkins server from running out of disk space
buildDiscarder(logRotator(numToKeepStr: '10'))
timestamps()
}
environment {
APP_NAME = 'rs-shoe-apparel' // reference to pom.xml
TOMCAT_CREDS = credentials('tomcat-manager-creds')
// Note: TOMCAT_SERVER_IP and TOMCAT_SERVER_PORT environment variables must be configured globally
// in Jenkins under Manage Jenkins -> System -> Global properties
}
stages {
// Continuous Integration
stage('Checkout') {
steps {
// Get code from a GitHub repository
checkout scm
}
}
stage('Compile & Test') {
steps {
// Compiles Clean here, so we start fresh once
sh 'mvn clean test'
}
}
stage('Build & Package') {
steps {
// Reuses the compilation from above, speeding up the build
echo "Building standard war package."
sh 'mvn package -DskipTests'
echo "Injecting build number into artifact name for tracking."
// Renames the compiled war file to include the Jenkins build number
sh "cp target/${APP_NAME}.war target/${APP_NAME}-v1.0.${BUILD_NUMBER}.war"
}
post {
success {
echo 'Archiving production-ready WAR file to Jenkins controller.'
// Save the WAR as a Jenkins build artifact; fingerprint enables tracking across builds/jobs.
archiveArtifacts artifacts: 'target/*-v1.0.*.war', fingerprint: true
}
}
}
// Continuous Deployment
stage('Deploy to Tomcat') {
steps {
echo 'Deploying standard war file via overwrite strategy...'
// Uses the standard war name so Tomcat performs an in-place overwrite
sh '''
curl -u ${TOMCAT_CREDS_USR}:${TOMCAT_CREDS_PSW} \
-T target/${APP_NAME}.war \
"http://${TOMCAT_SERVER_IP}:${TOMCAT_SERVER_PORT}/manager/text/deploy?path=/${APP_NAME}&update=true"
'''
}
}
stage('Smoke Test') {
steps {
echo "Validating deployment endpoint health..."
// Ensures the pipeline fails immediately if Tomcat returns a 404 or 500
sh '''
curl -fs "http://${TOMCAT_SERVER_IP}:${TOMCAT_SERVER_PORT}/${APP_NAME}/" || exit 1
'''
}
post {
success {
echo "Smoke test passed. Application context is healthy."
}
failure {
echo "ERROR: Smoke test failed! Application context is unreachable or returned an error code."
}
}
}
}
post {
always {
echo 'Pipeline has finished.'
}
success {
echo 'Build completed successfully.'
}
failure {
echo 'Build failed.'
}
}
}