forked from liatrio/spring-petclinic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
227 lines (210 loc) · 6.03 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/env groovy
@Library('ldop-shared-library@fd16602cad0f97ca1b04090f93a0540ddc871b45') _
pipeline {
agent none
environment {
IMAGE = "liatrio/petclinic-tomcat"
}
stages {
stage('Build') {
agent {
docker {
image 'maven:3.5.0'
args '-e INITIAL_ADMIN_USER -e INITIAL_ADMIN_PASSWORD --network=${LDOP_NETWORK_NAME}'
}
}
steps {
configFileProvider([configFile(fileId: 'nexus', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean deploy -DskipTests=true -B'
}
}
}
stage('Sonar') {
agent {
docker {
image 'sebp/sonar-runner'
args '-e SONAR_ACCOUNT_LOGIN -e SONAR_ACCOUNT_PASSWORD -e SONAR_DB_URL -e SONAR_DB_LOGIN -e SONAR_DB_PASSWORD --network=${LDOP_NETWORK_NAME}'
}
}
steps {
sh '/opt/sonar-runner-2.4/bin/sonar-runner -e -D sonar.login=${SONAR_ACCOUNT_LOGIN} -D sonar.password=${SONAR_ACCOUNT_PASSWORD} -D sonar.jdbc.url=${SONAR_DB_URL} -D sonar.jdbc.username=${SONAR_DB_LOGIN} -D sonar.jdbc.password=${SONAR_DB_PASSWORD}'
}
}
stage('Get Artifact') {
agent {
docker {
image 'maven:3.5.0'
args '-e INITIAL_ADMIN_USER -e INITIAL_ADMIN_PASSWORD --network=${LDOP_NETWORK_NAME}'
}
}
steps {
sh 'mvn clean'
script {
pom = readMavenPom file: 'pom.xml'
getArtifact(pom.groupId, pom.artifactId, pom.version, 'petclinic')
}
}
}
stage('Build container') {
agent any
steps {
script {
if ( env.BRANCH_NAME == 'master' ) {
pom = readMavenPom file: 'pom.xml'
TAG = pom.version
} else {
TAG = env.BRANCH_NAME
}
sh "docker build -t ${env.IMAGE}:${TAG} ."
}
}
}
stage('Run local container') {
agent any
steps {
sh 'docker rm -f petclinic-tomcat-temp || true'
sh "docker run -d --network=${LDOP_NETWORK_NAME} --name petclinic-tomcat-temp ${env.IMAGE}:${TAG}"
}
}
stage('Smoke-Test & OWASP Security Scan') {
agent {
docker {
image 'maven:3.5.0'
args '--network=${LDOP_NETWORK_NAME}'
}
}
steps {
sh "cd regression-suite && mvn clean -B test -DPETCLINIC_URL=http://petclinic-tomcat-temp:8080/petclinic/"
}
}
stage('Stop local container') {
agent any
steps {
sh 'docker rm -f petclinic-tomcat-temp || true'
}
}
stage('Push to dockerhub') {
agent any
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUsername')]){
script {
sh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}"
sh "docker push ${env.IMAGE}:${TAG}"
}
}
}
}
stage('Deploy to dev') {
when {
branch 'master'
}
agent any
steps {
script {
deployToEnvironment("ec2-user", "dev.petclinic.liatr.io", "petclinic-deploy-key", env.IMAGE, TAG, "spring-petclinic", "dev.petclinic.liatr.io")
}
}
}
stage('Smoke test dev') {
when {
branch 'master'
}
agent {
docker {
image 'maven:3.5.0'
args '--network=${LDOP_NETWORK_NAME}'
}
}
steps {
sh "cd regression-suite && mvn clean -B test -DPETCLINIC_URL=https://dev.petclinic.liatr.io/petclinic"
echo "Should be accessible at https://dev.petclinic.liatr.io/petclinic"
}
}
stage('Deploy to qa') {
when {
branch 'master'
}
agent any
steps {
deployToEnvironment("ec2-user", "qa.petclinic.liatr.io", "petclinic-deploy-key", env.IMAGE, TAG, "spring-petclinic", "qa.petclinic.liatr.io")
}
}
stage('Smoke test qa') {
when {
branch 'master'
}
agent {
docker {
image 'maven:3.5.0'
args '--network=${LDOP_NETWORK_NAME}'
}
}
steps {
sh "cd regression-suite && mvn clean -B test -DPETCLINIC_URL=https://qa.petclinic.liatr.io/petclinic"
echo "Should be accessible at https://qa.petclinic.liatr.io/petclinic"
input 'Deploy to Prod?'
}
}
stage('Blue/Green Prod Deploy') {
when {
branch 'master'
}
agent {
dockerfile {
filename "blue-green/Dockerfile"
}
}
steps {
withCredentials([
usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY'),
file(credentialsId: 'petclinic-deploy-key', variable: 'DEPLOY_KEY_PATH')
]) {
script {
sh "TAG=${TAG} blue-green/blue-green deploy"
}
}
}
}
stage('Blue/Green Prod Regression Test') {
when {
branch 'master'
}
agent {
dockerfile {
filename "blue-green/Dockerfile"
}
}
steps {
withCredentials([
usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY'),
file(credentialsId: 'petclinic-deploy-key', variable: 'DEPLOY_KEY_PATH')
]) {
script {
sh "TAG=${TAG} blue-green/blue-green test"
}
}
}
}
stage('Blue/Green Prod Toggle Load Balancer') {
when {
branch 'master'
}
agent {
dockerfile {
filename "blue-green/Dockerfile"
}
}
steps {
input "Toggle Prod Load Balancer?"
withCredentials([
usernamePassword(credentialsId: 'aws', usernameVariable: 'AWS_ACCESS_KEY_ID', passwordVariable: 'AWS_SECRET_ACCESS_KEY'),
file(credentialsId: 'petclinic-deploy-key', variable: 'DEPLOY_KEY_PATH')
]) {
script {
sh "TAG=${TAG} blue-green/blue-green toggle"
}
}
}
}
}
}