forked from ExchangeBC/devex
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
219 lines (207 loc) · 9.01 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
// define constants
def BUILDCFG_NAME ='devxp-dev'
def IMAGE_NAME = 'devxp-dev'
def DEV_DEPLOYMENT_NAME = 'platform-dev'
def DEV_TAG_NAME = 'dev'
def DEV_NS = 'devex-platform-dev'
def TST_DEPLOYMENT_NAME = 'platform-test'
def TST_TAG_NAME = 'test'
def TST_BCK_TAG_NAME = 'test-previous'
def TST_NS = 'devex-platform-test'
import groovy.json.JsonOutput
def notifySlack(text, channel, url, attachments) {
def slackURL = url
def jenkinsIcon = 'https://wiki.jenkins-ci.org/download/attachments/2916393/logo.png'
def payload = JsonOutput.toJson([text: text,
channel: channel,
username: "Jenkins",
icon_url: jenkinsIcon,
attachments: attachments
])
def encodedReq = URLEncoder.encode(payload, "UTF-8")
sh("curl -s -S -X POST " +
"--data \'payload=${encodedReq}\' ${slackURL}")
}
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 512
def changeString = ""
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += " - ${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = "No changes"
}
return changeString
}
// pipeline
// Note: openshiftVerifyDeploy requires policy to be added:
// oc policy add-role-to-user view system:serviceaccount:devex-platform-tools:jenkins -n devex-platform-dev
// oc policy add-role-to-user view system:serviceaccount:devex-platform-tools:jenkins -n devex-platform-test
// oc policy add-role-to-user view system:serviceaccount:devex-platform-tools:jenkins -n devex-platform-prod
properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10']]])
node('maven') {
stage('checkout') {
echo "checking out source"
echo "Build: ${BUILD_ID}"
checkout scm
}
// stage('dependency check') {
// dir('owasp') {
// // sh 'wget http://dl.bintray.com/jeremy-long/owasp/dependency-check-3.1.2-release.zip'
// sh './dependency-check/bin/dependency-check.sh --project "Developers Exchange" --scan ../package.json --enableExperimental --enableRetired'
// sh 'rm -rf ./dependency-check/data/'
// publishHTML (target: [
// allowMissing: false,
// alwaysLinkToLastBuild: false,
// keepAll: true,
// reportDir: './',
// reportFiles: 'dependency-check-report.html',
// reportName: "OWASP Dependency Check Report"
// ])
// }
// }
// stage('sonarqube') {
// script {
// openshift.withCluster() {
// openshift.withProject() {
// def sonarqube = openshift.selector("bc", "sonarqube-pipeline")
// def build = sonarqube.startBuild()
// build.untilEach(1) {
// return it.object().status.phase == "Complete"
// }
// }
// }
// }
// }
stage('build') {
echo "Building..."
openshiftBuild bldCfg: BUILDCFG_NAME, verbose: 'false', showBuildLogs: 'true'
sleep 5
// openshiftVerifyBuild bldCfg: BUILDCFG_NAME
echo ">>> Get Image Hash"
IMAGE_HASH = sh (
script: """oc get istag ${IMAGE_NAME}:latest -o template --template=\"{{.image.dockerImageReference}}\"|awk -F \":\" \'{print \$3}\'""",
returnStdout: true).trim()
echo ">> IMAGE_HASH: ${IMAGE_HASH}"
echo ">>>> Build Complete"
}
stage('Dev deploy') {
echo ">>> Tag ${IMAGE_HASH} with ${DEV_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: DEV_TAG_NAME, srcStream: IMAGE_NAME, srcTag: "${IMAGE_HASH}"
sleep 5
openshiftVerifyDeployment depCfg: DEV_DEPLOYMENT_NAME, namespace: DEV_NS, replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false'
echo ">>>> Deployment Complete"
notifySlack("Dev Deploy, changes:\n" + getChangeString(), "#builds", "https://hooks.slack.com/services/${SLACK_TOKEN}", [])
}
}
def owaspPodLabel = "owasp-zap-${UUID.randomUUID().toString()}"
podTemplate(label: owaspPodLabel, name: owaspPodLabel, serviceAccount: 'jenkins', cloud: 'openshift', containers: [
containerTemplate(
name: 'jnlp',
image: '172.50.0.2:5000/openshift/jenkins-slave-zap',
resourceRequestCpu: '500m',
resourceLimitCpu: '1000m',
resourceRequestMemory: '3Gi',
resourceLimitMemory: '4Gi',
workingDir: '/home/jenkins',
command: '',
args: '${computer.jnlpmac} ${computer.name}'
)
]) {
stage('ZAP Security Scan') {
node(owaspPodLabel) {
sleep 60
def retVal = sh returnStatus: true, script: '/zap/zap-baseline.py -r baseline.html -t http://platform-dev.pathfinder.gov.bc.ca/'
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: '/zap/wrk', reportFiles: 'baseline.html', reportName: 'ZAP Baseline Scan', reportTitles: 'ZAP Baseline Scan'])
echo "Return value is: ${retVal}"
}
}
}
// stage('Functional Test Dev') {
// // def userInput = 'y'
// def podlabel = "devxp-bddstack-${UUID.randomUUID().toString()}"
// // try {
// // timeout(time: 1, unit: 'DAYS') {
// // userInput = input(
// // id: 'userInput', message: 'Run Functional Tests (y/n - Default: y) ?',
// // parameters: [[$class: 'TextParameterDefinition', defaultValue: 'y', description: 'BDDTest', name: 'BDDTest']
// // ])
// // }
// // } catch(err) {}
// // echo ("BDD Test Run: "+userInput)
// // if ( userInput == 'y' ) {
// podTemplate(label: podlabel, name: podlabel, serviceAccount: 'jenkins', cloud: 'openshift',
// volumes: [
// emptyDirVolume(mountPath:'/dev/shm', memory: true)
// ],
// containers: [
// containerTemplate(
// name: 'jnlp',
// image: '172.50.0.2:5000/openshift/jenkins-slave-bddstack',
// resourceRequestCpu: '500m',
// resourceLimitCpu: '2000m',
// resourceRequestMemory: '2Gi',
// resourceLimitMemory: '8Gi',
// workingDir: '/home/jenkins',
// command: '',
// args: '${computer.jnlpmac} ${computer.name}',
// envVars: [
// envVar(key:'BASEURL', value: 'http://platform-dev.pathfinder.gov.bc.ca/')
// ]
// )
// ]) {
// node(podlabel) {
// //the checkout is mandatory, otherwise functional test would fail
// echo "checking out source"
// checkout scm
// //sleep 1000
// dir('functional-tests') {
// try {
// sh './gradlew chromeHeadlessTest'
// } finally {
// archiveArtifacts allowEmptyArchive: true, artifacts: 'build/reports/**/*'
// archiveArtifacts allowEmptyArchive: true, artifacts: 'build/test-results/**/*'
// junit 'build/test-results/**/*.xml'
// publishHTML (target: [
// allowMissing: false,
// alwaysLinkToLastBuild: false,
// keepAll: true,
// reportDir: 'build/reports/spock',
// reportFiles: 'index.html',
// reportName: "BDD Spock Report"
// ])
// publishHTML (target: [
// allowMissing: false,
// alwaysLinkToLastBuild: false,
// keepAll: true,
// reportDir: 'build/reports/tests/chromeHeadlessTest',
// reportFiles: 'index.html',
// reportName: "Full Test Report"
// ])
// }
// }
// }}
// // }
// }
stage('deploy-test') {
timeout(time: 1, unit: 'DAYS') {
input message: "Deploy to test?", submitter: 'mark-a-wilson-view,paulroberts68-view,agehlers-admin,SteveChapmanBCDX-admin,sutherlanda-admin'
}
node('master') {
echo ">>> Tag ${TST_TAG_NAME} with ${TST_BCK_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: TST_BCK_TAG_NAME, srcStream: IMAGE_NAME, srcTag: TST_TAG_NAME
echo ">>> Tag ${IMAGE_HASH} with ${TST_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: TST_TAG_NAME, srcStream: IMAGE_NAME, srcTag: "${IMAGE_HASH}"
sleep 5
openshiftVerifyDeployment depCfg: TST_DEPLOYMENT_NAME, namespace: TST_NS, replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false'
echo ">>>> Deployment Complete"
notifySlack("Test Deploy, changes:\n" + getChangeString(), "#builds", "https://hooks.slack.com/services/${SLACK_TOKEN}", [])
}
}