-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-CI
109 lines (99 loc) · 3.38 KB
/
Jenkinsfile-CI
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
pipeline{
agent any
tools {
maven 'MAVEN'
}
environment {
APP_NAME = "auth-service"
RELEASE = "1.0.0"
DOCKER_USER = "kienlvjd"
DOCKER_PASS = 'Dockerhub'
IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}"
IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}"
JENKINS_API_TOKEN = credentials('JENKINS_API_TOKEN')
}
stages {
stage ('Clean workspace') {
steps {
cleanWs()
}
}
stage("Checkout from SCM"){
steps {
git branch: 'master', credentialsId: 'GitHub', url: 'https://github.com/Kien-fit/authentication'
}
}
stage("Build Application"){
steps {
sh "mvn clean package"
}
}
stage("Test Application"){
steps {
sh "mvn test"
}
}
stage("Sonarqube Analysis") {
steps {
script {
withSonarQubeEnv(credentialsId: 'sonarQube') {
sh "mvn sonar:sonar"
}
}
}
}
stage("Quality Gate") {
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'sonarQube'
}
}
}
stage("Build & Push Docker Image") {
steps {
script {
withCredentials([string(credentialsId: 'Dockerhub', variable: 'dockerhub_pwd')]) {
docker_image = docker.build "${IMAGE_NAME}"
docker_image.push("${IMAGE_TAG}")
docker_image.push('latest')
}
}
}
}
stage("Trivy Scan") {
steps {
script {
sh ('docker run -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image ${IMAGE_NAME}:latest --no-progress --scanners vuln --exit-code 0 --severity HIGH,CRITICAL --format table')
}
}
}
stage("Trigger CD Pipeline") {
steps {
script {
sh "curl -v -k --user jenkins:${JENKINS_API_TOKEN} -X POST -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' --data 'IMAGE_TAG=${IMAGE_TAG}' 'http://127.0.0.1:8080/job/auth-service-cd/buildWithParameters?token=gitops-token'"
}
}
}
stage("Cleanup Artifacts") {
steps {
script {
sh "docker rmi ${IMAGE_NAME}:${IMAGE_TAG}"
sh "docker rmi ${IMAGE_NAME}:latest"
}
}
}
}
// Note: Need to setting Email Notification in Jenkins-lts
// post {
// failure {
// emailext body: '''${SCRIPT, template="groovy-html.template"}''',
// subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Failed",
// mimeType: 'text/html',to: "[email protected]"
// }
// success {
// emailext body: '''${SCRIPT, template="groovy-html.template"}''',
// subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
// mimeType: 'text/html',to: "[email protected]"
// }
// }
}