forked from sovrin-foundation/sovrin-devexchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile.master
189 lines (180 loc) · 7.81 KB
/
Jenkinsfile.master
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
// define constants
def BUILDCFG_NAME ='devxp'
def IMAGE_NAME = 'devxp'
def STAGING_DEPLOYMENT_NAME = 'platform-staging'
def STAGING_TAG_NAME = 'staging'
def STAGING_BCK_TAG_NAME = 'staging-previous'
def STAGING_NS = 'devex-platform-test'
def PROD_DEPLOYMENT_NAME = 'platform-prod'
def PROD_TAG_NAME = 'prod'
def PROD_BCK_TAG_NAME = 'prod-previous'
def PROD_NS = 'devex-platform-prod'
//define functions
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
node('maven') {
stage('checkout') {
echo "checking out source"
echo "Build: ${BUILD_ID}"
checkout scm
}
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('Staging Deploy') {
echo ">>> Tag ${STAGING_TAG_NAME} with ${STAGING_BCK_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: STAGING_BCK_TAG_NAME, srcStream: IMAGE_NAME, srcTag: STAGING_TAG_NAME
echo ">>> Tag ${IMAGE_HASH} with ${STAGING_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: STAGING_TAG_NAME, srcStream: IMAGE_NAME, srcTag: "${IMAGE_HASH}"
sleep 5
openshiftVerifyDeployment depCfg: STAGING_DEPLOYMENT_NAME, namespace: STAGING_NS, replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false'
echo ">>>> Deployment Complete"
notifySlack("Staging 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 https://platform-staging.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 Staging') {
// // 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: '1000m',
// resourceRequestMemory: '1Gi',
// resourceLimitMemory: '4Gi',
// workingDir: '/home/jenkins',
// command: '',
// args: '${computer.jnlpmac} ${computer.name}',
// envVars: [
// envVar(key:'BASEURL', value: 'https://platform-staging.pathfinder.gov.bc.ca/')
// ]
// )
// ]) {
// node(podlabel) {
// //the checkout is mandatory, otherwise functional test would fail
// echo "checking out source"
// checkout scm
// 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-prod') {
timeout(time: 3, unit: 'DAYS') {
input message: "Deploy to prod?", submitter: 'SteveChapmanBCDX-admin,mark-a-wilson-view,paulroberts68-view,agehlers-admin,sutherlanda-admin'
}
timeout(time: 3, unit: 'DAYS') {
input message: "This will release code to PRODUCTION. OK to go ahead?", submitter: 'SteveChapmanBCDX-admin,mark-a-wilson-view,paulroberts68-view,agehlers-admin,sutherlanda-admin'
}
node('master') {
echo ">>> Tag ${PROD_TAG_NAME} with ${PROD_BCK_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: PROD_BCK_TAG_NAME, srcStream: IMAGE_NAME, srcTag: PROD_TAG_NAME
echo ">>> Tag ${IMAGE_HASH} with ${PROD_TAG_NAME}"
openshiftTag destStream: IMAGE_NAME, verbose: 'false', destTag: PROD_TAG_NAME, srcStream: IMAGE_NAME, srcTag: "${IMAGE_HASH}"
sleep 5
openshiftVerifyDeployment depCfg: PROD_DEPLOYMENT_NAME, namespace: PROD_NS, replicaCount: 1, verbose: 'false', verifyReplicaCount: 'false'
echo ">>>> Deployment Complete"
notifySlack("PRODUCTION Deploy, changes:\n" + getChangeString(), "#builds", "https://hooks.slack.com/services/${SLACK_TOKEN}", [])
}
}