forked from CDCgov/SDP-Vocabulary-Service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
143 lines (120 loc) · 5.73 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
#!/usr/bin/env groovy
pipeline {
agent none
options {
timeout(time: 90, unit: 'MINUTES')
}
stages {
stage('Run Tests') {
agent { label 'vocab-ruby' }
steps {
updateSlack('#FFFF00', 'Started tests')
script {
env.svcname = sh returnStdout: true, script: 'echo -n "test-${BUILD_NUMBER}-${BRANCH_NAME}" | tr "_A-Z" "-a-z" | cut -c1-24 | sed -e "s/-$//"'
env.tdbname = sh returnStdout: true, script: 'echo -n "${svcname}" | tr "-" "_"'
env.esname = sh returnStdout: true, script: 'echo -n "es-test-${BUILD_NUMBER}-${BRANCH_NAME}" | tr "_A-Z" "-a-z" | cut -c1-24 | sed -e "s/-$//"'
}
echo "svc: ${svcname}, tdbname: ${tdbname}"
checkout scm
echo "Installing dependencies..."
sh 'yarn install'
sh 'bundle install'
echo "Precompiling assets..."
sh 'NODE_ENV=production bundle exec rails assets:precompile'
echo "Starting Foreman..."
sh 'foreman start webpack &'
echo "Starting test database..."
timeout(time: 5, unit: 'MINUTES') {
sh 'oc process openshift//postgresql-ephemeral -l testdb=${svcname} DATABASE_SERVICE_NAME=${svcname} POSTGRESQL_USER=railstest POSTGRESQL_PASSWORD=railstest POSTGRESQL_DATABASE=${tdbname} | oc create -f -'
waitUntil {
script {
sleep time: 15, unit: 'SECONDS'
def r = sh returnStdout: true, script: 'oc get pod -l name=${svcname} -o jsonpath="{range .items[*]}{.status.containerStatuses[*].ready}{end}"'
return (r == "true")
}
}
script {
env.dbhost = sh returnStdout: true, script: 'oc get service -l testdb=${svcname} -o jsonpath="{.items[*].spec.clusterIP}"'
env.podName = sh returnStdout: true, script: 'oc get pod -l name=${svcname} -o jsonpath="{.items[*].metadata.name}"'
env.namespace = sh returnStdout: true, script: 'oc get pod -l name=${svcname} -o jsonpath="{.items[*].metadata.namespace}"'
}
openshiftExec namespace: "${namespace}", pod: "${podName}", container: 'postgresql', command: [ "/bin/sh", "-i", "-c", "psql -h 127.0.0.1 -q -c 'ALTER ROLE railstest WITH SUPERUSER'" ]
}
echo "Creating schema..."
withEnv(["OPENSHIFT_POSTGRESQL_DB_NAME=${tdbname}", 'OPENSHIFT_POSTGRESQL_DB_USERNAME=railstest', 'OPENSHIFT_POSTGRESQL_DB_PASSWORD=railstest', "OPENSHIFT_POSTGRESQL_DB_HOST=${dbhost}", 'OPENSHIFT_POSTGRESQL_DB_PORT=5432', 'RAILS_ENV=test']) {
sh 'bundle exec rake db:create'
sh 'bundle exec rake db:schema:load'
}
echo "Starting elasticsearch..."
timeout(time: 5, unit: 'MINUTES') {
sh 'oc process openshift//elasticsearch-ephemeral -l name=${esname} ELASTICSEARCH_SERVICE_NAME=${esname} | oc create -f -'
waitUntil {
script {
sleep time: 15, unit: 'SECONDS'
def r = sh returnStdout: true, script: 'oc get pod -l name=${esname} -o jsonpath="{range .items[*]}{.status.containerStatuses[*].ready}{end}"'
return (r == "true")
}
}
script {
env.elastichost = sh returnStdout: true, script: 'oc get service -l name=${esname} -o jsonpath="{.items[*].spec.clusterIP}"'
}
}
echo "Running tests..."
withEnv(['NO_PROXY=localhost,127.0.0.1', "OPENSHIFT_POSTGRESQL_DB_NAME=${tdbname}", 'OPENSHIFT_POSTGRESQL_DB_USERNAME=railstest', 'OPENSHIFT_POSTGRESQL_DB_PASSWORD=railstest', "OPENSHIFT_POSTGRESQL_DB_HOST=${dbhost}", 'OPENSHIFT_POSTGRESQL_DB_PORT=5432']) {
sh 'bundle exec rake'
}
echo "Running elasticsearch integration tests..."
withEnv(["NO_PROXY=localhost,127.0.0.1,${elastichost}", "OPENSHIFT_POSTGRESQL_DB_NAME=${tdbname}", 'OPENSHIFT_POSTGRESQL_DB_USERNAME=railstest',
'OPENSHIFT_POSTGRESQL_DB_PASSWORD=railstest', "OPENSHIFT_POSTGRESQL_DB_HOST=${dbhost}", 'OPENSHIFT_POSTGRESQL_DB_PORT=5432',
"ES_HOST=http://${elastichost}:9200", 'RAILS_ENV=test']) {
sh 'bundle exec rake db:seed'
sh 'bundle exec rake admin:create_user[[email protected],testtest,false]'
sh 'bundle exec rake data:load_test[[email protected]]'
sh 'bundle exec rake es:test[[email protected]]'
}
}
post {
always {
echo "Destroying test database..."
sh 'oc delete pods,dc,rc,services,secrets -l testdb=${svcname}'
echo "Destroying elasticsearch..."
sh 'oc delete pods,dc,rc,services,secrets -l name=${esname}'
echo "Archiving test artifacts..."
archiveArtifacts artifacts: '**/reports/coverage/*, **/reports/mini_test/*',
fingerprint: true
}
success {
updateSlack('#00FF00', 'Finished tests')
}
failure {
updateSlack('#FF0000', 'Failed tests')
}
}
}
stage('Build for Dev Env') {
agent any
when {
branch 'development'
}
steps {
updateSlack('#FFFF00', 'Starting build for development environment')
echo "Triggering new build for development environment..."
openshiftBuild namespace: 'sdp', bldCfg: 'vocabulary',
waitTime: '20', waitUnit: 'min'
}
post {
success {
updateSlack('#00FF00', 'Finished building for development environment')
}
failure {
updateSlack('#FF0000', 'Failed to build for development environment')
}
}
}
}
}
def updateSlack(String colorHex, String messageText) {
if (env.BRANCH_NAME == 'development' || env.CHANGE_ID) {
slackSend (color: colorHex, message: "${messageText}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}