forked from jaiswaladi246/Petclinic
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Jenkinsfile
158 lines (155 loc) · 5.05 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
147
148
149
150
151
152
153
154
155
156
157
158
pipeline{
agent any
tools{
jdk 'jdk17'
maven 'maven3'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
}
stages {
stage('clean workspace'){
steps{
cleanWs()
}
}
stage('Checkout From Git'){
steps{
git branch: 'main', url: 'https://github.com/Aj7Ay/Petclinic-Real.git'
}
}
stage('mvn compile'){
steps{
sh 'mvn clean compile'
}
}
stage('mvn test'){
steps{
sh 'mvn test'
}
}
stage("Sonarqube Analysis "){
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Petclinic \
-Dsonar.java.binaries=. \
-Dsonar.projectKey=Petclinic '''
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-token'
}
}
}
stage('mvn build'){
steps{
sh 'mvn clean install'
}
}
stage("OWASP Dependency Check"){
steps{
dependencyCheck additionalArguments: '--scan ./ --format HTML ', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.html'
}
}
stage("Docker Build & Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "docker build -t petclinic1 ."
sh "docker tag petclinic1 sevenajay/petclinic1:latest "
sh "docker push sevenajay/petclinic1:latest "
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image sevenajay/petclinic1:latest > trivy.txt"
}
}
stage('Clean up containers') { //if container runs it will stop and remove this block
steps {
script {
try {
sh 'docker stop pet1'
sh 'docker rm pet1'
} catch (Exception e) {
echo "Container pet1 not found, moving to next stage"
}
}
}
}
stage ('Manual Approval'){
steps {
script {
timeout(time: 10, unit: 'MINUTES') {
def approvalMailContent = """
Project: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
Go to build URL and approve the deployment request.
URL de build: ${env.BUILD_URL}
"""
mail(
to: '[email protected]',
subject: "${currentBuild.result} CI: Project name -> ${env.JOB_NAME}",
body: approvalMailContent,
mimeType: 'text/plain'
)
input(
id: "DeployGate",
message: "Deploy ${params.project_name}?",
submitter: "approver",
parameters: [choice(name: 'action', choices: ['Deploy'], description: 'Approve deployment')]
)
}
}
}
}
stage('Deploy to conatiner'){
steps{
sh 'docker run -d --name pet1 -p 8082:8080 sevenajay/petclinic1:latest'
}
}
stage("Deploy To Tomcat"){
steps{
sh "sudo cp /var/lib/jenkins/workspace/petclinic/target/petclinic.war /opt/apache-tomcat-9.0.65/webapps/ "
}
}
stage('Deploy to kubernets'){
steps{
script{
withKubeConfig(caCertificate: '', clusterName: '', 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'
}
}
}
// try this approval stage also
stage('Manual Approval') {
timeout(time: 10, unit: 'MINUTES') {
mail to: '[email protected]',
subject: "${currentBuild.result} CI: ${env.JOB_NAME}",
body: "Project: ${env.JOB_NAME}\nBuild Number: ${env.BUILD_NUMBER}\nGo to ${env.BUILD_URL} and approve deployment"
input message: "Deploy ${params.project_name}?",
id: "DeployGate",
submitter: "approver",
parameters: [choice(name: 'action', choices: ['Deploy'], description: 'Approve deployment')]
}
}