-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
118 lines (104 loc) · 3.5 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
pipeline {
agent any
tools {
jdk 'jdk17'
nodejs 'node20'
}
environment {
SCANNER_HOME = tool 'sonar-scanner'
}
stages {
stage('Clean workspace') {
steps {
cleanWs()
}
}
stage('Git checkout') {
steps {
git branch: 'main', url: 'https://github.com/Zackaria-Slimane/gittrackr.git'
}
}
stage("SonarQube static Analysis") {
steps {
withSonarQubeEnv('sonar-server') {
sh """$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Gittracker \
-Dsonar.projectKey=Gittracker"""
}
}
}
stage("SonarQube gate check") {
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'sonarqube'
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP Security FS SCAN') {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'dp-check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage('TRIVY FS SCAN') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage("Docker setup, Build & Push") {
steps {
script {
withDockerRegistry(credentialsId: 'docker', toolName: 'docker') {
sh "docker buildx build -t gittracker ."
sh "docker tag gittracker zackariasl/gittracker:latest"
sh "docker push zackariasl/gittracker:latest"
}
}
}
}
stage("Docker image TRIVY Scan") {
steps {
sh "trivy image zackariasl/gittracker:latest > trivy.txt"
}
}
stage('Docker build || run container') {
steps {
script {
def containerName = 'cicd'
def containerExists = sh(script: "docker ps -a --format '{{.Names}}' | grep -w ${containerName}", returnStatus: true) == 0
if (containerExists) {
echo "Container ${containerName} already exists."
sh "docker start ${containerName}"
} else {
echo "Container ${containerName} building from scratch"
sh "docker run -d --name ${containerName} -p 3000:3000 zackariasl/gittracker:latest"
}
}
}
}
stage('Deploy to kubernetes'){
steps{
script{
withKubeConfig(caCertificate: '', clusterName: 'GittrackerK8s', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
sh 'kubectl apply -f deployment.yaml'
}
}
}
}
}
post {
always {
emailext attachLog: true,
subject: "'${currentBuild.result}'",
body: "Project: ${env.JOB_NAME}<br/>" +
"Build Number: ${env.BUILD_NUMBER}<br/>" +
"URL: ${env.BUILD_URL}<br/>",
to: '[email protected]',
attachmentsPattern: 'trivy.txt'
}
}
}