-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
114 lines (102 loc) · 2.68 KB
/
Jenkinsfile
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
pipeline {
agent any
stages {
stage('Build') {
steps {
slackSend(channel: '#jenkins', color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
sh '''chmod +x ./gradlew
./gradlew clean build --exclude-task test
'''
stash(name: 'build-artifacts', includes: '**/build/libs/*.jar')
}
}
stage('Test') {
steps {
sh './gradlew test'
stash(name: 'test-artifacts', includes: '**/build/test-results/test/TEST-*.xml')
}
}
stage('Coverage') {
steps {
sh './gradlew jacocoTestReport'
publishCoverage adapters: [jacocoAdapter('build/reports/jacoco/test/jacocoTestReport.xml')]
}
}
stage('SonarQube analysis') {
steps {
withSonarQubeEnv(credentialsId: 'sonarqube', installationName: 'SonarQube Server') {
sh './gradlew sonarqube'
}
script {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
stage('Report & Publish') {
steps {
unstash 'build-artifacts'
unstash 'test-artifacts'
junit '**/build/test-results/test/TEST-*.xml'
archiveArtifacts 'build/libs/*.jar'
}
}
stage('Build Docker Image') {
when {
anyOf {
branch 'develop'
branch 'main'
}
}
steps {
script {
unstash 'build-artifacts'
dockerImage = docker.build imageName
}
}
}
stage('Push Docker Image') {
when {
anyOf {
branch 'develop'
branch 'main'
}
}
steps{
script {
docker.withRegistry( '', registryCredential ) {
dockerImage.push("$BUILD_NUMBER")
dockerImage.push('latest')
}
}
}
}
stage('Remove Unused Docker Image') {
when {
anyOf {
branch 'develop'
branch 'main'
}
}
steps{
sh "docker rmi $imageName:$BUILD_NUMBER"
sh "docker rmi $imageName:latest"
}
}
}
environment {
imageName = 'riyenas0925/muin_backend'
registryCredential = 'dockerhub'
dockerImage = ''
}
post {
success {
slackSend (channel: '#jenkins', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: '#jenkins', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}