forked from dynatrace-sockshop/orders
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathJenkinsfile
196 lines (193 loc) · 5.19 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
@Library('[email protected]') _
def tagMatchRules = [
[
meTypes: [
[meType: 'SERVICE']
],
tags : [
[context: 'CONTEXTLESS', key: 'app', value: 'orders'],
[context: 'CONTEXTLESS', key: 'environment', value: 'dev']
]
]
]
pipeline {
agent {
label 'maven'
}
environment {
APP_NAME = "orders"
ARTEFACT_ID = "sockshop/" + "${env.APP_NAME}"
VERSION = readFile('version').trim()
TAG = "${env.DOCKER_REGISTRY_URL}:5000/library/${env.ARTEFACT_ID}"
TAG_DEV = "${env.TAG}-${env.VERSION}-${env.BUILD_NUMBER}"
TAG_STAGING = "${env.TAG}-${env.VERSION}"
}
stages {
stage('Maven build') {
steps {
checkout scm
container('maven') {
sh 'mvn -B clean package'
}
}
}
stage('Docker build') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
container('docker') {
sh "docker build -t ${env.TAG_DEV} ."
}
}
}
stage('Docker push to registry'){
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
container('docker') {
sh "docker push ${env.TAG_DEV}"
}
}
}
stage('Deploy to dev namespace') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
container('kubectl') {
sh "sed -i 's#image: .*#image: ${env.TAG_DEV}#' manifest/orders.yml"
sh "kubectl -n dev apply -f manifest/orders.yml"
}
}
}
stage('DT Deploy Event') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
container("curl") {
script {
def status = pushDynatraceDeploymentEvent (
tagRule : tagMatchRules,
customProperties : [
[key: 'Jenkins Build Number', value: "${env.BUILD_ID}"],
[key: 'Git commit', value: "${env.GIT_COMMIT}"]
]
)
}
}
}
}
stage('Run health check in dev') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
echo "Waiting for the service to start..."
container('kubectl') {
script {
def status = waitForDeployment (
deploymentName: "${env.APP_NAME}",
environment: 'dev'
)
if(status !=0 ){
currentBuild.result = 'FAILED'
error "Deployment did not finish before timeout."
}
sleep(10)
}
}
container('jmeter') {
script {
def status = executeJMeter (
scriptName: "jmeter/basiccheck.jmx",
resultsDir: "HealthCheck_${env.APP_NAME}_dev_${env.VERSION}_${BUILD_NUMBER}",
serverUrl: "${env.APP_NAME}.dev",
serverPort: 80,
checkPath: '/health',
vuCount: 1,
loopCount: 1,
LTN: "HealthCheck_${BUILD_NUMBER}",
funcValidation: true,
avgRtValidation: 0
)
if (status != 0) {
currentBuild.result = 'FAILED'
error "Health check in dev failed."
}
}
}
}
}
stage('Run functional check in dev') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*' || env.BRANCH_NAME ==~'master'
}
}
steps {
container('jmeter') {
script {
def status = executeJMeter (
scriptName: "jmeter/${env.APP_NAME}_load.jmx",
resultsDir: "FuncCheck_${env.APP_NAME}_dev_${env.VERSION}_${BUILD_NUMBER}",
serverUrl: "${env.APP_NAME}.dev",
serverPort: 80,
checkPath: '/health',
vuCount: 1,
loopCount: 1,
LTN: "FuncCheck_${BUILD_NUMBER}",
funcValidation: true,
avgRtValidation: 0
)
if (status != 0) {
currentBuild.result = 'FAILED'
error "Functional check in dev failed."
}
}
}
}
}
stage('Mark artifact for staging namespace') {
when {
expression {
return env.BRANCH_NAME ==~ 'release/.*'
}
}
steps {
container('docker'){
sh "docker tag ${env.TAG_DEV} ${env.TAG_STAGING}"
sh "docker push ${env.TAG_STAGING}"
}
}
}
stage('Deploy to staging') {
when {
beforeAgent true
expression {
return env.BRANCH_NAME ==~ 'release/.*'
}
}
steps {
build job: "k8s-deploy-staging",
parameters: [
string(name: 'APP_NAME', value: "${env.APP_NAME}"),
string(name: 'TAG_STAGING', value: "${env.TAG_STAGING}"),
string(name: 'VERSION', value: "${env.VERSION}")
]
}
}
}
}