-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathJenkinsfile
247 lines (246 loc) · 10.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
pipeline {
agent none
options {
disableConcurrentBuilds()
}
stages {
stage('Build') {
agent { label 'yona' }
environment {
DOCKER_HUB = credentials('docker-hub')
GITHUB_APP = credentials('github-app')
HELM_HOME = "/opt/jnlp-agent/yona/k8s/helm/.helm"
}
steps {
checkout scm
sh './gradlew --no-daemon -PdockerHubUserName=$DOCKER_HUB_USR -PdockerHubPassword="$DOCKER_HUB_PSW" -PdockerUrl=unix:///var/run/docker.sock build pushDockerImage htmlDependencyReport'
script {
def scannerHome = tool 'SonarQube scanner 3.0';
withSonarQubeEnv('Yona SonarQube server') {
sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectVersion=build-$BUILD_NUMBER"
}
}
dir ('k8s/helm') {
sh '../../scripts/publish-helm-package.sh $BUILD_NUMBER 1.2.$BUILD_NUMBER yona $GITHUB_APP_PSW /opt/jnlp-agent/yona/k8s/helm helm-charts'
}
sh 'git tag -a build-$BUILD_NUMBER -m "Jenkins"'
sh 'git push https://$x-access-token:${GITHUB_APP_PSW}@github.com/yonadev/yona-server.git --tags'
script {
env.BUILD_NUMBER_TO_DEPLOY = env.BUILD_NUMBER
}
}
post {
always {
junit '**/build/test-results/*/*.xml'
}
success {
step([$class: 'hudson.plugins.jira.JiraIssueUpdater',
issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'],
scm: [$class: 'hudson.plugins.git.GitSCM']])
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'build/reports/project/dependencies',
includes: '**/*',
reportFiles: 'index.html',
reportName: 'Depencencies Report'
]
}
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed"
}
}
}
stage('Setup test server') {
agent { label 'yona' }
environment {
YONA_DB = credentials('test-db')
HELM_HOME = "/opt/jnlp-agent/yona/k8s/helm/.helm"
KUBECONFIG = "/opt/jnlp-agent/yona/k8s/admin.conf"
}
steps {
sh 'while ! $(curl -s -q -f -o /dev/null https://jump.ops.yona.nu/helm-charts/yona-1.2.$BUILD_NUMBER_TO_DEPLOY.tgz) ;do echo Waiting for Helm chart to become available; sleep 5; done'
sh script: 'helm uninstall yona --namespace yona; kubectl delete -n yona configmaps --all; kubectl delete -n yona job --all; kubectl delete -n yona secrets --all; kubectl delete pvc -n yona --all', returnStatus: true
sh script: 'echo Waiting for purge to complete; sleep 30'
sh 'helm repo update'
helmUpgradeOrInstall(4, "infrastructure/helm/values.yaml", "yona")
sh 'scripts/wait-for-services.sh k8snew'
}
post {
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed to deploy build ${env.BUILD_NUMBER_TO_DEPLOY} to integration test server"
}
}
}
stage('Run integration tests') {
agent { label 'yona' }
steps {
sh './gradlew --no-daemon -Pyona_adminservice_url=http://build.dev.yona.nu:31000 -Pyona_analysisservice_url=http://build.dev.yona.nu:31001 -Pyona_appservice_url=https://build.dev.yona.nu -Pyona_batchservice_url=http://build.dev.yona.nu:31003 intTest'
}
post {
always {
junit testResults: '**/build/test-results/*/*.xml', keepLongStdio: true
}
success {
slackSend color: 'good', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> passed all tests on build ${env.BUILD_NUMBER_TO_DEPLOY}"
}
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed integration test of build ${env.BUILD_NUMBER_TO_DEPLOY}"
}
}
}
stage('Decide deploy to test servers') {
agent none
steps {
script {
env.DEPLOY_TO_TEST_SERVERS = input message: 'User input required',
submitter: 'authenticated',
parameters: [choice(name: 'Deploy to the test servers', choices: 'no\nyes', description: 'Choose "yes" if you want to deploy the test servers')]
if (env.DEPLOY_TO_TEST_SERVERS == 'no') {
slackSend color: 'warning', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> skips all further steps"
}
}
}
}
stage('Deploy to beta test server') {
agent { label 'beta' }
when {
environment name: 'DEPLOY_TO_TEST_SERVERS', value: 'yes'
}
steps {
sh 'helm repo add yona https://jump.ops.yona.nu/helm-charts'
helmUpgradeOrInstall(2, "helm/values.yaml", "yona")
sh 'scripts/wait-for-services.sh k8snew'
}
post {
success {
script {
jiraIssueSelector(issueSelector: [$class: 'DefaultIssueSelector']).each {
id -> jiraComment(issueKey: id,
body: "Build [#${env.BUILD_NUMBER}|${currentBuild.absoluteUrl}] deployed this to beta")
}
}
}
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed to deploy build ${env.BUILD_NUMBER_TO_DEPLOY} to beta"
}
}
}
stage('Deploy to load test server') {
agent { label 'load' }
environment {
BETA_DB = credentials('beta-db-jenkins')
BETA_DB_IP = credentials('beta-db-ip')
}
when {
environment name: 'DEPLOY_TO_TEST_SERVERS', value: 'yes'
}
steps {
sh 'mysql -h $BETA_DB_IP -u $BETA_DB_USR -p$BETA_DB_PSW -e "DROP DATABASE loadtest; CREATE DATABASE loadtest;"'
sh 'helm repo add yona https://jump.ops.yona.nu/helm-charts'
helmUpgradeOrInstall(2, "helm/values_loadtest.yaml", "loadtest")
sh 'NAMESPACE=loadtest scripts/wait-for-services.sh k8snew'
}
post {
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed to deploy build ${env.BUILD_NUMBER_TO_DEPLOY} to load test"
}
}
}
stage('Decide run load test') {
agent none
when {
environment name: 'DEPLOY_TO_TEST_SERVERS', value: 'yes'
}
steps {
slackSend color: 'good', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> ready to start load test of build ${env.BUILD_NUMBER_TO_DEPLOY}"
script {
env.RUN_LOAD_TEST = input message: 'User input required',
submitter: 'authenticated',
parameters: [choice(name: 'Run load test', choices: 'no\nyes', description: 'Choose "yes" if you want to run the load test')]
}
}
}
stage('Run load test') {
agent { label 'yona' }
when {
environment name: 'RUN_LOAD_TEST', value: 'yes'
}
environment {
JM_PATH_IN_CONT = "/mnt/jmeter"
JM_LOCAL_PATH = "jmeter"
JM_THREADS = "100"
JM_LOAD_DURATION = "1200"
}
steps {
checkout scm
sh 'rm -rf ${JM_LOCAL_PATH}'
sh 'mkdir ${JM_LOCAL_PATH}'
sh 'cp scripts/load-test.jmx ${JM_LOCAL_PATH}'
sh 'docker run --volume ${WORKSPACE}/${JM_LOCAL_PATH}:${JM_PATH_IN_CONT} yonadev/jmeter:JMeter3.3 -n -t ${JM_PATH_IN_CONT}/load-test.jmx -l ${JM_PATH_IN_CONT}/out/result.jtl -j ${JM_PATH_IN_CONT}/out/jmeter.log -Jthreads=${JM_THREADS} -JloadDuration=${JM_LOAD_DURATION}'
sh 'docker run --volume ${WORKSPACE}/${JM_LOCAL_PATH}:${JM_PATH_IN_CONT} --entrypoint /opt/apache-jmeter-3.3/bin/FilterResults.sh yonadev/jmeter:JMeter3.3 --input-file ${JM_PATH_IN_CONT}/out/result.jtl --output-file ${JM_PATH_IN_CONT}/out/filtered-result.jtl --exclude-label-regex true --exclude-labels .*Batch.*'
perfReport "${env.JM_LOCAL_PATH}/out/filtered-result.jtl"
}
post {
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed in load test of build ${env.BUILD_NUMBER_TO_DEPLOY}"
}
}
}
stage('Decide deploy to production server') {
agent none
when {
environment name: 'DEPLOY_TO_TEST_SERVERS', value: 'yes'
}
steps {
slackSend color: 'good', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> ready to deploy build ${env.BUILD_NUMBER_TO_DEPLOY} to production"
script {
env.DEPLOY_TO_PRD = input message: 'User input required',
submitter: 'authenticated',
parameters: [choice(name: 'Deploy to production server', choices: 'no\nyes', description: 'Choose "yes" if you want to deploy the production server')]
if (env.DEPLOY_TO_PRD == 'no') {
slackSend color: 'warning', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> skips all further steps"
}
}
}
}
stage('Deploy to production server') {
agent { label 'prd' }
when {
environment name: 'DEPLOY_TO_PRD', value: 'yes'
}
steps {
sh 'helm repo add yona https://jump.ops.yona.nu/helm-charts'
helmUpgradeOrInstall(1, "helm/values.yaml", "yona")
sh 'scripts/wait-for-services.sh k8snew'
}
post {
success {
slackSend color: 'good', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> successfully deployed build ${env.BUILD_NUMBER_TO_DEPLOY} to production"
script {
jiraIssueSelector(issueSelector: [$class: 'DefaultIssueSelector']).each {
id -> jiraComment(issueKey: id,
body: "Build [#${env.BUILD_NUMBER}|${currentBuild.absoluteUrl}] deployed this to production")
}
}
}
failure {
slackSend color: 'danger', channel: '#devops', message: "<${currentBuild.absoluteUrl}|Server build ${env.BUILD_NUMBER}> failed to deploy build ${env.BUILD_NUMBER_TO_DEPLOY} to production"
}
}
}
}
}
void helmUpgradeOrInstall(def repoNumber, def srcPath, def name)
{
def namespace = name
def deploymentName = name
def encodedPath = java.net.URLEncoder.encode(srcPath, "UTF-8")
def url = "https://git.ops.yona.nu/api/v4/projects/${repoNumber}/repository/files/${encodedPath}/raw?ref=master"
withCredentials([string( credentialsId: 'gitlab-yonabuild', variable: 'token')]) {
withEnv(["TOKEN=${token}"]) {
sh "wget --header='PRIVATE-TOKEN: ${TOKEN}' --output-document=- ${url} | helm upgrade --install --values - --namespace ${namespace} --version 1.2.${BUILD_NUMBER_TO_DEPLOY} ${deploymentName} yona/yona"
}
}
}