forked from ak-devops2/sample-java-sonar-nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
162 lines (146 loc) · 5.95 KB
/
Jenkinsfile
File metadata and controls
162 lines (146 loc) · 5.95 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
pipeline {
agent any
environment {
JAVA_HOME = "/usr/lib/jvm/java-21-amazon-corretto.x86_64"
PATH = "${JAVA_HOME}/bin:${env.PATH}"
GIT_REPO_URL = 'https://github.com/manojac/sample-java-sonar-nexus.git'
SONAR_URL = 'http://3.111.214.55:30900'
SONAR_TOKEN = 'squ_b32ed949b7fcc7d845ae15bac7687934c92c6bdd'
SONAR_CRED_ID = 'id-sonar-token'
MAX_BUILDS_TO_KEEP = 5
NEXUS_URL = 'http://3.111.214.55:30801'
NEXUS_DOCKER_REPO = '3.111.214.55:30002' // Updated to correct HTTP port
NEXUS_CREDENTIAL_ID = 'nexus-creds'
}
tools {
maven 'maven-3.9.10'
}
stages {
stage('Checkout') {
steps {
git url: "${GIT_REPO_URL}", branch: 'main'
}
}
stage('Create Sonar Project') {
steps {
script {
def projectName = "${env.JOB_NAME}-${env.BUILD_NUMBER}".replace('/', '-')
withCredentials([usernamePassword(credentialsId: "${SONAR_CRED_ID}", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh """
curl -s -o /dev/null -w "%{http_code}" -u $USERNAME:$PASSWORD -X POST \
"${SONAR_URL}/api/projects/create?project=${projectName}&name=${projectName}" || true
"""
}
}
}
}
stage('SonarQube Analysis') {
steps {
script {
def projectName = "${env.JOB_NAME}-${env.BUILD_NUMBER}".replace('/', '-')
sh """
mvn clean verify sonar:sonar \
-Dsonar.projectKey=${projectName} \
-Dsonar.host.url=${SONAR_URL} \
-Dsonar.login=${SONAR_TOKEN}
"""
}
}
}
stage('Build and Tag Artifact') {
steps {
script {
sh "mvn clean package -DskipTests"
def artifactName = "my-app-${BUILD_NUMBER}.jar"
sh """
mkdir -p tagged-artifacts
cp target/*.jar tagged-artifacts/${artifactName}
echo "Artifact tagged as ${artifactName}"
"""
}
}
}
stage('Push Artifact to Nexus') {
steps {
script {
def version = "1.0.${BUILD_NUMBER}"
def artifactId = "my-app"
def groupPath = "com/mycompany/app"
def nexusPath = "${groupPath}/${artifactId}/${version}"
def finalArtifact = "${artifactId}-${version}.jar"
sh """
mv tagged-artifacts/my-app-${BUILD_NUMBER}.jar tagged-artifacts/${finalArtifact}
"""
withCredentials([usernamePassword(credentialsId: "${NEXUS_CREDENTIAL_ID}", usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS')]) {
sh """
curl -u $NEXUS_USER:$NEXUS_PASS \
--upload-file tagged-artifacts/${finalArtifact} \
${NEXUS_URL}${nexusPath}/${finalArtifact}
"""
}
}
}
}
stage('Create Dockerfile') {
steps {
script {
writeFile file: 'Dockerfile', text: """
FROM openjdk:21-jdk-slim
WORKDIR /app
COPY tagged-artifacts/my-app-*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
"""
}
}
}
stage('Build Docker Image') {
steps {
script {
def imageTag = "${NEXUS_DOCKER_REPO}/my-app:${BUILD_NUMBER}"
sh "docker build -t ${imageTag} ."
}
}
}
stage('Push Docker Image to Nexus') {
steps {
script {
def imageTag = "${NEXUS_DOCKER_REPO}/my-app:${BUILD_NUMBER}"
withCredentials([usernamePassword(credentialsId: "${NEXUS_CREDENTIAL_ID}", usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS')]) {
sh """
echo $NEXUS_PASS | docker login http://${NEXUS_DOCKER_REPO} -u $NEXUS_USER --password-stdin
docker push ${imageTag}
docker logout http://${NEXUS_DOCKER_REPO}
"""
}
}
}
}
stage('Delete Old Sonar Projects') {
steps {
script {
def currentBuild = env.BUILD_NUMBER.toInteger()
def minBuildToKeep = currentBuild - MAX_BUILDS_TO_KEEP.toInteger()
if (minBuildToKeep > 0) {
withCredentials([usernamePassword(credentialsId: "${SONAR_CRED_ID}", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
for (int i = 1; i <= minBuildToKeep; i++) {
def oldProject = "${env.JOB_NAME}-${i}".replace('/', '-')
echo "Deleting old Sonar project: ${oldProject}"
sh """
curl -s -o /dev/null -w "%{http_code}" -u $USERNAME:$PASSWORD -X POST \
"${SONAR_URL}/api/projects/delete" \
-d "project=${oldProject}" || true
"""
}
}
}
}
}
}
}
post {
always {
cleanWs()
}
}
}