forked from bcgov/range-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
230 lines (199 loc) · 9.56 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
import groovy.json.JsonOutput
def APP_NAME = 'range-myra-api'
def BUILD_CONFIG = APP_NAME
def IMAGESTREAM_NAME = APP_NAME
def TAG_NAMES = ['dev', 'test', 'prod']
def PIRATE_ICO = 'http://icons.iconarchive.com/icons/aha-soft/torrent/64/pirate-icon.png'
def JENKINS_ICO = 'https://wiki.jenkins-ci.org/download/attachments/2916393/logo.png'
def OPENSHIFT_ICO = 'https://commons.wikimedia.org/wiki/File:OpenShift-LogoType.svg'
def SCHEMA_SPY_IMAGSTREAM_NAME = 'schema-spy'
def PROJECT_NAMESPACE_BASE = 'range-myra-'
def POD_LABEL = "${APP_NAME}-${UUID.randomUUID().toString()}"
// jenkins-slave-python3nod
def notifySlack(text, channel, url, attachments, icon) {
def slackURL = url
def jenkinsIcon = icon
def payload = JsonOutput.toJson([text: text,
channel: channel,
username: "Jenkins",
icon_url: jenkinsIcon,
attachments: attachments
])
sh "curl -s -S -X POST --data-urlencode \'payload=${payload}\' ${slackURL}"
}
// See https://github.com/jenkinsci/kubernetes-plugin
podTemplate(label: "${POD_LABEL}", name: "${POD_LABEL}", serviceAccount: 'jenkins', cloud: 'openshift', containers: [
containerTemplate(
name: 'jnlp',
image: 'docker-registry.default.svc:5000/openshift/jenkins-slave-nodejs:8',
resourceRequestCpu: '1500m',
resourceLimitCpu: '2000m',
resourceRequestMemory: '1Gi',
resourceLimitMemory: '2Gi',
workingDir: '/tmp',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
alwaysPullImage: false
// envVars: [
// secretEnvVar(key: 'BDD_DEVICE_FARM_USER', secretName: 'bdd-credentials', secretKey: 'username'),
// secretEnvVar(key: 'BDD_DEVICE_FARM_PASSWD', secretName: 'bdd-credentials', secretKey: 'password'),
// secretEnvVar(key: 'ANDROID_DECRYPT_KEY', secretName: 'android-decrypt-key', secretKey: 'decryptKey')
// ]
)
]) {
node("${POD_LABEL}") {
stage('Checkout') {
echo "Checking out source"
checkout scm
GIT_COMMIT_SHORT_HASH = sh (
script: """git describe --always""",
returnStdout: true).trim()
GIT_COMMIT_AUTHOR = sh (
script: """git show -s --pretty=%an""",
returnStdout: true).trim()
GIT_BRANCH_NAME = sh (
script: """git branch -a -v --no-abbrev --contains ${GIT_COMMIT_SHORT_HASH} | \
grep 'remotes' | \
awk -F ' ' '{print \$1}' | \
awk -F '/' '{print \$3}'""",
returnStdout: true).trim()
SLACK_TOKEN = sh (
script: """oc get secret/slack -o template --template="{{.data.token}}" | base64 --decode""",
returnStdout: true).trim()
}
stage('Setup') {
echo "Setup: ${BUILD_ID}"
sh "node -v"
sh "npm -v"
sh "npm ci"
}
stage('Test') {
echo "Testing: ${BUILD_ID}"
script {
//
// Run our unit tests et al.
//
try {
echo "Running Unit Tests"
sh "npm test"
} catch (error) {
def attachment = [:]
attachment.fallback = 'See build log for more details'
attachment.title = "API Build ${BUILD_ID} FAILED! :face_with_head_bandage: :hankey:"
attachment.color = '#CD0000' // Red
attachment.text = "There are issues with the unit tests.\ncommit ${GIT_COMMIT_SHORT_HASH} by ${GIT_COMMIT_AUTHOR}"
// attachment.title_link = "${env.BUILD_URL}"
notifySlack("${APP_NAME}, Build #${BUILD_ID}", "#rangedevteam", "https://hooks.slack.com/services/${SLACK_TOKEN}", [attachment], JENKINS_ICO)
sh "exit 1001"
}
//
// Check the code builds
//
try {
echo "Checking Build"
sh "npm run build"
} catch (error) {
def attachment = [:]
attachment.fallback = 'See build log for more details'
attachment.title = "API Build ${BUILD_ID} FAILED! :face_with_head_bandage: :hankey:"
attachment.color = '#CD0000' // Red
attachment.text = "The code does not build.\ncommit ${GIT_COMMIT_SHORT_HASH} by ${GIT_COMMIT_AUTHOR}"
// attachment.title_link = "${env.BUILD_URL}"
notifySlack("${APP_NAME}, Build #${BUILD_ID}", "#rangedevteam", "https://hooks.slack.com/services/${SLACK_TOKEN}", [attachment], JENKINS_ICO)
sh "exit 1001"
}
//
// Run our code quality tests
//
try {
echo "Checking code quality with SonarQube"
SONARQUBE_URL = sh (
script: 'oc get routes -o wide --no-headers | awk \'/sonarqube/{ print match($0,/edge/) ? "https://"$2 : "http://"$2 }\'',
returnStdout: true
).trim()
echo "SONARQUBE_URL: ${SONARQUBE_URL}"
dir('sonar-runner') {
// Most settings are configured in `build.gradle`.
sh returnStdout: true,
script: "./gradlew sonarqube -Dsonar.host.url=${SONARQUBE_URL} -Dsonar.branch=${GIT_BRANCH_NAME} --stacktrace --info"
}
} catch (error) {
def attachment = [:]
attachment.fallback = 'See build log for more details'
attachment.title = "API Build ${BUILD_ID} WARNING! :unamused: :zany_face: :fox4:"
attachment.color = '#FFA500' // Orange
attachment.text = "The SonarQube code quality check failed.\ncommit ${GIT_COMMIT_SHORT_HASH} by ${GIT_COMMIT_AUTHOR}"
// attachment.title_link = "${env.BUILD_URL}"
notifySlack("${APP_NAME}, Build #${BUILD_ID}", "#rangedevteam", "https://hooks.slack.com/services/${SLACK_TOKEN}", [attachment], JENKINS_ICO)
}
//
// Check code quality with a LINTer
//
try {
echo "Checking code quality with LINTer"
sh "npm run test:lint"
} catch (error) {
def attachment = [:]
attachment.fallback = 'See build log for more details'
attachment.title = "API Build ${BUILD_ID} WARNING! :unamused: :zany_face: :fox4:"
attachment.color = '#FFA500' // Orange
attachment.text = "There LINTer code quality check failed.\ncommit ${GIT_COMMIT_SHORT_HASH} by ${GIT_COMMIT_AUTHOR}"
// attachment.title_link = "${env.BUILD_URL}"
notifySlack("${APP_NAME}, Build #${BUILD_ID}", "#rangedevteam", "https://hooks.slack.com/services/${SLACK_TOKEN}", [attachment], JENKINS_ICO)
}
}
}
stage('Image Build') {
echo "Build: ${BUILD_ID}"
// run the oc build to package the artifacts into a docker image
openshiftBuild bldCfg: APP_NAME, showBuildLogs: 'true', verbose: 'false'
// Don't tag with BUILD_ID so the pruner can do it's job; it won't delete tagged images.
// Tag the images for deployment based on the image's hash
IMAGE_HASH = sh (
script: """oc get istag ${IMAGESTREAM_NAME}:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: TAG_NAMES[0], srcStream: IMAGESTREAM_NAME, srcTag: "${IMAGE_HASH}"
try {
// Sale Schema Spy down and up will cause it to rebuild the schema documentation. This isn't the most
// efficiant way to do this but at least its automated.
// For this to work the Jenkins service mush have edit permissions within the deployment project.
// Example OC cmd to accomplish this task (it's better if you have project init scripts that do this);
// oc policy add-role-to-user edit system:serviceaccount:range-myra-tools:jenkins -n range-myra-dev
echo "Scaling Schema Spy to trigger refresh"
openshiftScale deploymentConfig: SCHEMA_SPY_IMAGSTREAM_NAME, replicaCount: 0, namespace: PROJECT_NAMESPACE_BASE + TAG_NAMES[0]
openshiftScale deploymentConfig: SCHEMA_SPY_IMAGSTREAM_NAME, replicaCount: 1, namespace: PROJECT_NAMESPACE_BASE + TAG_NAMES[0]
} catch (error) {
echo "Unable to scale schema spy"
}
try {
def attachment = [:]
attachment.title = "API Build ${BUILD_ID} OK! :heart: :tada:"
attachment.fallback = 'See build log for more details'
attachment.text = "Another huge sucess for the Range Team.\nA freshly minted build is being deployed and will be available shortly.\ncommit ${GIT_COMMIT_SHORT_HASH} by ${GIT_COMMIT_AUTHOR}"
attachment.color = '#00FF00' // Lime Green
notifySlack("${APP_NAME}", "#rangedevteam", "https://hooks.slack.com/services/${SLACK_TOKEN}", [attachment], JENKINS_ICO)
} catch (error) {
echo "Unable send update to slack, error = ${error}"
}
}
}
node ('master') {
stage('Approval') {
timeout(time: 4, unit: 'HOURS') {
input message: "Deploy to test?", submitter: 'authenticated'
}
stage('Test Promotion') {
openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: TAG_NAMES[1], srcStream: IMAGESTREAM_NAME, srcTag: "${IMAGE_HASH}"
notifySlack("Promotion Completed\n Build #${BUILD_ID} was promoted to test.", "#range-api", "https://hooks.slack.com/services/${SLACK_TOKEN}", [], OPENSHIFT_ICO)
}
stage('Prod Promotion') {
timeout(time: 4, unit: 'HOURS') {
input message: "Promote this image to prod?", submitter: 'authenticated'
}
openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: TAG_NAMES[2], srcStream: IMAGESTREAM_NAME, srcTag: "${IMAGE_HASH}"
notifySlack("Promotion Completed\n Build #${BUILD_ID} was promoted to prod.", "#range-api", "https://hooks.slack.com/services/${SLACK_TOKEN}", [], OPENSHIFT_ICO)
}
}
}
}