-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
123 lines (112 loc) · 3.18 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
115
116
117
118
119
120
121
122
123
pipeline{
agent{
label "jenkins-agent"
}
environment{
SONAR_TOKEN = credentials('jenkins-sonarqube-token')
APP_NAME = "complete-production-e2e-pipeline"
RELEASE = "1.0.0"
DOCKER_USER = "saqlainkhan"
DOCKER_PASS = 'dockerhub'
IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}"
IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}"
JENKINS_API_TOKEN = credentials('JENKINS_API_TOKEN')
}
tools{
nodejs "node"
}
stages{
stage("Cleanup Workspace"){
steps{
cleanWs()
}
}
stage("Checkout from SCM"){
steps{
git branch: 'main', credentialsId: 'github', url: 'https://github.com/saqlaink/complete-production-e2e-pipeline'
}
}
stage("Build Application"){
steps{
sh "npm install"
}
}
stage("Test Application"){
steps{
sh "CI=true npm test"
}
}
stage("Sonarqube Analysis"){
steps{
script {
withSonarQubeEnv(credentialsId: 'jenkins-sonarqube-token') {
sh "npm install -g sonarqube-scanner"
sh "sonar-scanner -Dsonar.projectKey=react-app -Dsonar.sources=src -Dsonar.host.url=https://sonarqube.realcollection.tech -Dsonar.login=${SONAR_TOKEN}"
}
}
}
}
stage("Quality Gate"){
steps{
script {
waitForQualityGate abortPipeline: false, credentialsId: 'jenkins-sonarqube-token'
}
}
}
stage("Build & Push Docker Image"){
steps{
script {
docker.withRegistry('', DOCKER_PASS){
docker_image = docker.build "${IMAGE_NAME}"
}
docker.withRegistry('', DOCKER_PASS){
docker_image.push("${IMAGE_TAG}")
docker_image.push("latest")
}
}
}
}
stage("Trivy Artifact Scan"){
steps{
script {
sh "trivy image ${IMAGE_NAME}:${IMAGE_TAG} > trivy_scan_report.txt"
}
}
}
stage('Send Email with Trivy Scan Report'){
steps {
emailext body: 'Please find the Trivy scan report attached.',
subject: 'Trivy Scan Report',
to: '[email protected]',
attachmentsPattern: 'trivy_scan_report.txt',
mimeType: 'text/plain'
}
}
stage("Trigger CD Pipeline"){
steps{
script {
sh "curl -v -k --user admin:${JENKINS_API_TOKEN} -X POST -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' --data 'IMAGE_TAG=${IMAGE_TAG}' 'https://jenkins.realcollection.tech/job/gitops-complete-pipeline/buildWithParameters?token=gitops-token'"
}
}
}
}
post {
always {
script {
def status = currentBuild.result ?: 'UNKNOWN'
def color
switch(status) {
case 'SUCCESS':
color = 'good'
break
case 'FAILURE':
color = 'danger'
break
default:
color = 'warning'
}
slackSend(channel: "#jenkins", message: "GitOps Pipeline trigger ${status.toLowerCase()} for ${env.JOB_NAME} (${env.BUILD_NUMBER})\n More info at: ${env.BUILD_URL}", iconEmoji: ":jenkins:", color: color)
}
}
}
}