-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
200 lines (164 loc) · 6.78 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
// set build properties
properties([
buildDiscarder(logRotator(numToKeepStr: '10')), // keep only last 10 builds
[$class: 'GithubProjectProperty', projectUrlStr: 'https://github.com/pengyue/Awin-ReportTask'],
// pipelineTriggers([cron('@daily')]) // build once a day
])
node {
environment {
registry = "pengyue/awin-reporttask"
registryCredential = ‘dockerhub’
}
def commitId = "latest"
def user = env.BUILD_USER_ID
def dockerRegistry = "pengyue/awin-reporttask"
def version = 'latest'
def projectName = "awin-report-task"
def slackBaseUrl = "https://triplanteam.slack.com/services/hooks/jenkins-ci/"
def slackTeamDomain = "triplanteam"
def ownerSlackChannel = "#deployment"
def slackTokenCredentialId = "6b884e09-0ba2-4a86-b93c-08c3de571ca2"
def ownerEmail = "[email protected]"
stage ("Checkout") {
milestone()
checkout scm
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
shortCommit = gitCommit.take(7)
//pom = readMavenPom file: 'pom.xml'
//version = "${pom.version}-${BUILD_NUMBER}-${shortCommit}"
version = "build-${BUILD_NUMBER}-${shortCommit}"
echo "version: ${version}"
currentBuild.description = version
}
stage('Setup JAVA env') {
//env.JAVA_HOME="${tool 'jdk 8u51'}"
//env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
//sh 'java -version'
}
try {
milestone()
stage ("Build") {
sh "composer install"
sh "php deptrac.phar -v analyze depfile.yml"
}
stage ("Static code analysis") {
//sh "vendor/bin/phpcs --config-set ignore_warnings_on_exit 1 --report=checkstyle --report-file=var/checkstyle-result.xml -q /src"
//step([$class: 'hudson.plugins.checkstyle.CheckStylePublisher', pattern: 'checkstyle-*'])
}
stage ("Generate report") {
//publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'var/code-coverage', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: 'Code Report'])
}
stage ('Test') {
// Run any testing suites
sh "echo 'Running tests ...'"
sh "vendor/bin/phpunit --config phpunit.xml --printer PHPUnit\\\\TextUI\\\\ResultPrinter"
sh "vendor/bin/behat"
}
milestone()
stage ('Docker Registration Push') {
sh "echo 'PUSHING TO DockerHub ...'"
docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
def app = docker.build("${dockerRegistry}:${commitId}", '.').push()
}
}
def currentDate = getCurrentDate()
def deploy = canDeploy(currentDate)
if (!deploy) {
try {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to deploy outside of deployment hours?',
parameters: [[$class: 'BooleanParameterDefinition',
defaultValue: false,
description: 'Ticking this box will do a live deploy',
name: 'Live Deploy']]
}
deploy = true
} catch (err) {
deploy = false
echo "${err}"
echo "Production deployment skipped."
}
}
if (deploy) {
// if we got till here w/o any errors, means job is successful
currentBuild.result = 'SUCCESS'
println("Automatic deployment allowed")
lock('Production Deployment') {
stage('Deploy to Production') {
//sh "make VERSION=${version} TRIGGER=${user} SERVER_LIST=${productionServerList} SPRING_PROFILES_ACTIVE=production deploy-production"
//trigger a kubernetes deployment here
}
}
} else {
currentBuild.result = "FAILURE"
println("Automatic deployment to production is only allowed at the following times:")
println("Mon-Thu 7 -> 17:30 London time")
println("Fri 7 -> 12:00 London time")
println("Run the build again within those time windows next time :p")
}
} catch(error) {
currentBuild.result = "FAILURE"
echo "${error}"
} finally {
// Any cleanup operations needed, whether we hit an error or not
if (currentBuild.result == "FAILURE") {
//sh "make clean version=${version}"
//throw error
}
}
if (currentBuild.result == "FAILURE" && currentBuild.previousBuild.result != 'FAILURE') {
slackSend baseUrl: "${slackBaseUrl}",
teamDomain: "${slackTeamDomain}",
channel: "${ownerSlackChannel}",
color: '#af0000',
message: "master branch of ${projectName} has failed - ${env.BUILD_URL}",
tokenCredentialId: "${slackTokenCredentialId}"
//mail from: '[email protected]',
// to: "${ownerEmail}",
// subject: "${projectName} build failed",
// body: "${projectName} build failed: ${env.BUILD_URL}"
// for recovery
} else if (currentBuild.result == 'SUCCESS' && currentBuild.previousBuild.result != 'SUCCESS') {
slackSend baseUrl: "${slackBaseUrl}",
teamDomain: "${slackTeamDomain}",
channel: "${ownerSlackChannel}",
color: '#00960c',
message: "master branch of ${projectName} has recovered! - ${env.BUILD_URL}",
tokenCredentialId: "${slackTokenCredentialId}"
//mail from: '[email protected]',
// to: "${ownerEmail}",
// subject: "${projectName} build has recovered",
// body: "${projectName} build has recovered: ${env.BUILD_URL}"
}
}
def canDeploy(Date date) {
// Monoday to Thursday between 07:00 and 17:30 London time
// On Friday from 07:00 to 12:00 London time
def londonTimeZone = TimeZone.getTimeZone('Europe/London')
int day_of_week = date.format("u", londonTimeZone) as Integer
int hour = date.format("HH", londonTimeZone) as Integer
int minute = date.format("mm", londonTimeZone) as Integer
// Monday - Thursday
if (day_of_week < 5) {
if ((hour >= 7 && hour < 17) || (hour == 17 && minute <= 30)) {
return true
}
}
// Friday
if (day_of_week == 5) {
// Between 7 and 12:00
if ((hour >= 7) && (hour < 12)) {
return true
}
}
return false
}
def getCurrentDate() {
def date = new Date()
int day_of_week = date.format("u") as Integer
int hour = date.format("HH") as Integer
int minute = date.format("mm") as Integer
println(date)
println("Day of week: ${day_of_week}\t hour: ${hour}\t minute: ${minute}")
return date
}