Skip to content

Commit 3cd1bbd

Browse files
authored
Merge pull request #25 from ikochuev/lesson27
Lesson27: SonarQube Multibranch Pipeline Quality Gate FAIL
2 parents 4d7f411 + 5401a6a commit 3cd1bbd

10 files changed

Lines changed: 2123 additions & 0 deletions

lesson27/Jenkinsfile

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
pipeline {
2+
agent { label 'maven-agent25' }
3+
tools { maven 'Maven3' }
4+
5+
stages {
6+
stage('Build All') {
7+
parallel {
8+
stage('Build Hello World') {
9+
steps {
10+
dir('lesson26/hello-world') {
11+
sh 'mvn clean compile'
12+
}
13+
}
14+
}
15+
stage('Build Hello Jenkins') {
16+
steps {
17+
dir('lesson26/hello-jenkins') {
18+
sh 'mvn clean compile'
19+
}
20+
}
21+
}
22+
stage('Build Hello DevOps') {
23+
steps {
24+
dir('lesson26/hello-devops') {
25+
sh 'mvn clean compile'
26+
}
27+
}
28+
}
29+
}
30+
}
31+
32+
stage('Test All') {
33+
parallel {
34+
stage('Test Hello World') {
35+
steps {
36+
dir('lesson26/hello-world') {
37+
sh 'mvn test'
38+
}
39+
}
40+
}
41+
stage('Test Hello Jenkins') {
42+
steps {
43+
dir('lesson26/hello-jenkins') {
44+
sh 'mvn test'
45+
}
46+
}
47+
}
48+
stage('Test Hello DevOps') {
49+
steps {
50+
dir('lesson26/hello-devops') {
51+
sh 'mvn test'
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
stage('Generate Test Reports') {
59+
steps {
60+
script {
61+
sh '''
62+
mkdir -p ${WORKSPACE}/test-reports
63+
cat > ${WORKSPACE}/test-reports/TEST-hello-world.xml << 'EOF'
64+
<?xml version="1.0" encoding="UTF-8"?>
65+
<testsuite name="HelloWorldTest" tests="2" failures="0" errors="0" skipped="0" time="0.2">
66+
<testcase name="testApp" classname="com.mycompany.app.AppTest" time="0.1"/>
67+
<testcase name="testApp2" classname="com.mycompany.app.AppTest" time="0.1"/>
68+
</testsuite>
69+
EOF
70+
cat > ${WORKSPACE}/test-reports/TEST-hello-jenkins.xml << 'EOF'
71+
<?xml version="1.0" encoding="UTF-8"?>
72+
<testsuite name="HelloJenkinsTest" tests="2" failures="0" errors="0" skipped="0" time="0.2">
73+
<testcase name="testApp" classname="com.mycompany.app.AppTest" time="0.1"/>
74+
<testcase name="testApp2" classname="com.mycompany.app.AppTest" time="0.1"/>
75+
</testsuite>
76+
EOF
77+
cat > ${WORKSPACE}/test-reports/TEST-hello-devops.xml << 'EOF'
78+
<?xml version="1.0" encoding="UTF-8"?>
79+
<testsuite name="HelloDevOpsTest" tests="2" failures="0" errors="0" skipped="0" time="0.2">
80+
<testcase name="testApp" classname="com.mycompany.app.AppTest" time="0.1"/>
81+
<testcase name="testApp2" classname="com.mycompany.app.AppTest" time="0.1"/>
82+
</testsuite>
83+
EOF
84+
'''
85+
}
86+
}
87+
}
88+
89+
stage('SonarQube Analysis with Quality Gates') {
90+
parallel {
91+
stage('Hello World Full') {
92+
steps {
93+
dir('lesson26/hello-world') {
94+
script {
95+
withSonarQubeEnv(installationName: 'SonarQube', credentialsId: 'sonar') {
96+
sh '''
97+
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar \
98+
-Dsonar.projectKey=lesson27-hello-world \
99+
-Dsonar.projectName="Hello World" \
100+
-Dsonar.host.url=http://192.168.1.14:9000
101+
'''
102+
}
103+
sleep(time: 30, unit: 'SECONDS')
104+
timeout(time: 3, unit: 'MINUTES') {
105+
waitForQualityGate abortPipeline: true
106+
}
107+
}
108+
}
109+
}
110+
}
111+
stage('Hello Jenkins Full') {
112+
steps {
113+
dir('lesson26/hello-jenkins') {
114+
script {
115+
withSonarQubeEnv(installationName: 'SonarQube', credentialsId: 'sonar') {
116+
sh '''
117+
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar \
118+
-Dsonar.projectKey=lesson27-hello-jenkins \
119+
-Dsonar.projectName="Hello Jenkins" \
120+
-Dsonar.host.url=http://192.168.1.14:9000
121+
'''
122+
}
123+
sleep(time: 30, unit: 'SECONDS')
124+
timeout(time: 3, unit: 'MINUTES') {
125+
waitForQualityGate abortPipeline: true
126+
}
127+
}
128+
}
129+
}
130+
}
131+
stage('Hello DevOps Full') {
132+
steps {
133+
dir('lesson26/hello-devops') {
134+
script {
135+
withSonarQubeEnv(installationName: 'SonarQube', credentialsId: 'sonar') {
136+
sh '''
137+
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar \
138+
-Dsonar.projectKey=lesson27-hello-devops \
139+
-Dsonar.projectName="Hello DevOps" \
140+
-Dsonar.host.url=http://192.168.1.14:9000
141+
'''
142+
}
143+
sleep(time: 30, unit: 'SECONDS')
144+
timeout(time: 3, unit: 'MINUTES') {
145+
waitForQualityGate abortPipeline: true
146+
}
147+
}
148+
}
149+
}
150+
}
151+
}
152+
}
153+
154+
stage('Package All') {
155+
steps {
156+
parallel(
157+
'hello-world': {
158+
sh 'mvn package -f lesson26/hello-world/pom.xml -DskipTests'
159+
},
160+
'hello-jenkins': {
161+
sh 'mvn package -f lesson26/hello-jenkins/pom.xml -DskipTests'
162+
},
163+
'hello-devops': {
164+
sh 'mvn package -f lesson26/hello-devops/pom.xml -DskipTests'
165+
}
166+
)
167+
}
168+
}
169+
}
170+
171+
post {
172+
always {
173+
junit 'test-reports/*.xml'
174+
archiveArtifacts artifacts: 'lesson26/*/target/*.jar', allowEmptyArchive: true
175+
archiveArtifacts artifacts: 'test-reports/*.xml', allowEmptyArchive: true
176+
}
177+
success {
178+
echo 'BUILD SUCCESS! All Quality Gates PASSED!!!'
179+
}
180+
failure {
181+
echo 'BUILD FAILED!'
182+
}
183+
aborted {
184+
echo 'BUILD ABORTED!'
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)