-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
95 lines (83 loc) · 3.25 KB
/
Jenkinsfile
File metadata and controls
95 lines (83 loc) · 3.25 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
pipeline {
agent none
environment {
DOCKER_IMAGE = "pmayors/jenkinsjob:latest"
}
stages {
stage('Build with Maven') {
agent { label 'agentmaven' }
steps {
// Pull Spring PetClinic project from GitHub (public repo)
git branch: 'main', url: 'https://github.com/spring-projects/spring-petclinic.git'
// Build the project (skip tests)
sh 'mvn clean package -DskipTests'
// Save jar artifact for later stages
stash includes: 'target/*.jar', name: 'app-jar'
}
}
// Skipping Test stage as per your setup
// stage('Test with Maven') {
// agent { label 'agentmaven' }
// steps {
// sh 'mvn test'
// }
// post {
// always {
// junit 'target/surefire-reports/*.xml'
// }
// }
// }
stage('Run on Windows Agent') {
agent { label 'windows' }
steps {
echo "Running a task on the Windows agent..."
bat 'echo Hello from Windows Agent > windows_task.txt'
bat 'type windows_task.txt'
}
post {
always {
// Archive the output from Windows agent for verification
archiveArtifacts artifacts: 'windows_task.txt', fingerprint: true
echo "Windows agent task output archived."
}
}
}
stage('Docker Build & Push') {
agent { label 'agentdocker' }
steps {
// Retrieve the JAR file from Maven build
unstash 'app-jar'
// Create Dockerfile dynamically (Java 17 & correct jar name)
sh '''
cp target/spring-petclinic-*.jar app.jar
echo "FROM openjdk:17-jdk-slim" > Dockerfile
echo "COPY app.jar /app.jar" >> Dockerfile
echo 'ENTRYPOINT ["java","-jar","/app.jar"]' >> Dockerfile
'''
// Build and push Docker image
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
sh '''
docker build -t $DOCKER_IMAGE .
echo $PASS | docker login -u $USER --password-stdin
docker push $DOCKER_IMAGE
'''
}
}
}
stage('Deploy on Docker Agent') {
agent { label 'agentdocker' }
steps {
// Stop & remove old container if it exists, then run new one
sh '''
docker stop jenkinsjob || true
docker rm jenkinsjob || true
docker run -d --name jenkinsjob -p 8080:8080 $DOCKER_IMAGE
echo "=== Running Containers ==="
docker ps
echo "=== Last 50 container logs ==="
docker logs --tail=50 jenkinsjob || true
'''
}
}
}
}