-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJenkinsfile
146 lines (119 loc) · 4.79 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
pipeline {
agent any
environment {
// Define environment variables
IMAGE_NAME = 'cloudsihmar/dummy-pet'
PORT_MAPPING = '9090:8080'
dockerhub_cred = credentials("dockerhub-creds")
}
stages {
// clean workspace
stage('clean workspace'){
steps{
cleanWs()
}
}
// cleaning ends here
// Git Stage starts
stage('checkout from git') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/CloudSihmar/pet.git']])
}
}
// Git stage ends
// Static Code Analysis starts
stage('Static code analysis') {
environment {
SCANNER_HOME = tool 'sonar-scanner'
}
steps {
withSonarQubeEnv('sonarqube-server') {
sh """${SCANNER_HOME}/bin/sonar-scanner \\
-Dsonar.projectKey=govtech-pet \\
-Dsonar.projectName=govtech-pet \\
-Dsonar.projectVersion=${BUILD_NUMBER} \\
-Dsonar.skipSSLVerification=true \\
-Dsonar.sources=src/main/java \\
-Dsonar.tests=src/test/java \\
-Dsonar.exclusions='src/main/resources/**/*.java,src/test/**/*.java,src/pmd/**/*,**/*.properties'
"""
}
}
}
// Static Code Analysis
// Quality Gate stage starts
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: true, credentialsId: 'sonarqube-creds'
}
}
}
// Quality Gate ends
// Build starts here
stage("maven build"){
steps {
script {
sh 'mvn clean package'
}
}
}
// Build ends here
// Software composition analysis starts
stage('OWASP FS SCAN') {
steps {
dependencyCheck additionalArguments: '--format HTML', odcInstallation: 'Dependency-Check'
}
}
//Software composition analysis ends
// Build stage starts
stage('build') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', passwordVariable: 'docker_password', usernameVariable: 'docker_username')]) {
// Login to Docker Hub , username and password are stored in jenkins credentials with dockerhub as ID.
sh "docker login -u ${docker_username} -p ${docker_password}"
// Build and push Docker image
sh "docker build -t $IMAGE_NAME:${BUILD_NUMBER} -f Dockerfile.v3 ."
sh "docker push $IMAGE_NAME:${BUILD_NUMBER}"
}
}
}
// build stage ends
// Trivy scan starts
stage("TRIVY-IMAGE-SCAN"){
steps{
sh "trivy image $IMAGE_NAME:${BUILD_NUMBER} --no-progress --timeout 10m --severity HIGH,CRITICAL > trivyimagescan.txt"
}
}
// Trivy stage ends
// Trivy scan starts
stage("TRIVY-IMAGE-SCAN1"){
steps{
sh "trivy image $IMAGE_NAME:$BUILD_NUMBER --output result.html --severity HIGH,CRITICAL"
}
}
// Trivy stage ends
// Deployment-change starts
stage("Deployment-Change"){
steps{
dir('deployment-dir') {
git branch: 'main', url: 'https://github.com/CloudSihmar/pet-argocd-k8s.git'
withCredentials([string(credentialsId: 'github-secret', variable: 'GIT_TOKEN')]) {
sh '''
git config --global user.email "cloudsihmar@gmail.com"
git config --global user.name "cloudsihmar"
git config --global user.password "${GIT_TOKEN}"
sed -i "s|image: cloudsihmar/dummy-pet:.*|image: cloudsihmar/dummy-pet:${BUILD_NUMBER}|" deployment.yaml
git add deployment.yaml
git commit -m "Update deployment image to version ${BUILD_NUMBER}"
git remote set-url origin https://${GIT_TOKEN}@github.com/cloudsihmar/pet-argocd-k8s.git
git push origin main
ls -al
'''
}
}
}
}
// Deployment-change ends
}
}