-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
318 lines (289 loc) · 9.11 KB
/
Jenkinsfile
File metadata and controls
318 lines (289 loc) · 9.11 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env groovy
/*
* Jenkinsfile
*
* Use the Scripted style of Jenkinsfile in order to
* write more Groovy functions and use variables to
* control the workflow.
*/
import java.util.Random
// Set default variables
final default_timeout_minutes = 20
/** Set up CAPTCHA*/
final int MAX = 10
final Long XOR_CONST = MAX * 3
def get_captcha(Long hash_const, int max) {
Random rand = new Random()
def op1 = rand.nextInt(max+1)
def op2 = rand.nextInt(max+1) + max
def op3 = rand.nextInt(max+1)
def captcha_problem = "CAPTCHA problem: What is the answer to this problem: ${op1} + ${op2} - ${op3}"
Long captcha_answer = op1 + op2 - op3
Long captcha_hash = captcha_answer ^ hash_const
return [captcha_problem, captcha_hash.toString()]
}
def prepEnv = {
sh ("""
cp env.sh.sample env.sh
cp terraform/variables-local.tf.sample terraform/variables-local.tf
rm -rf build
mkdir build
""")
}
def wrap = { fn->
ansiColor('xterm') {
fn()
}
}
(captcha_problem, captcha_hash) = get_captcha(XOR_CONST,MAX)
/** Gather properties from user parameters */
properties([
parameters([
booleanParam(
name: 'Destroy_Terraform',
defaultValue: false,
description: 'Destroy Terraform resources?'
),
booleanParam(
name: 'Apply_Terraform',
defaultValue: false,
description: 'Apply Terraform plan?'
),
string(
name: 'Application',
defaultValue: 'all',
description: '''The application to build.
Separate multiple values by a space.
The value 'all' iterates over the applications.'''
),
booleanParam(
name: 'Build_Container',
defaultValue: false,
description: 'Build container/s?'
),
booleanParam(
name: 'Push_Container',
defaultValue: false,
description: 'Push container/s to the repository?'
),
booleanParam(
name: 'Deploy_Container',
defaultValue: false,
description: 'Deploy container/s?'
),
booleanParam(
name: 'Add_To_DNS',
defaultValue: false,
description: 'Add the application ELB to DNS?'
),
booleanParam(
name: 'Update_Container',
defaultValue: false,
description: 'Update container/s on this build?'
),
booleanParam(
name: 'Run_Jmeter',
defaultValue: false,
description: 'Run jmeter against the deployed app?'
),
booleanParam(
name: 'Delete_Container',
defaultValue: false,
description: 'Delete container/s?'
),
string(
name: 'CAPTCHA_Guess',
defaultValue: '',
description: captcha_problem
),
string(
name: 'CAPTCHA_Hash',
defaultValue: captcha_hash,
description: 'Hash for CAPTCHA answer (DO NOT modify)'
),
string(
name: 'Terraform_Targets',
defaultValue: '',
description: '''Specific Terraform resource or resource names to target
(Use this to modify or delete less than the full set of resources'''
),
text(
name: 'Extra_Variables',
defaultValue: '',
description: '''Terraform Variables to define for this run.
Allows you to override declared variables.
Put one variable per line, in JSON or HCL like this:
associate_public_ip_address = "true"'''
),
string(
name: 'Build_Command_Arguments',
defaultValue: '',
description: 'The arguments to the build command.'
),
booleanParam(
name: 'Run_Build_Command',
defaultValue: false,
description: 'Run build.sh with the above arguments for debugging.'
)
])
])
stage('Preflight') {
// Check CAPTCHA
def should_validate_captcha = false
/*
def should_validate_captcha = params.Build_Container || params.Push_Container || params.Deploy_Container || params.Updatey_Container || params.Delete_Container || params.Apply_Terraform || params.Destroy_Terraform || params.Add_To_DNS
*/
if (should_validate_captcha) {
if (params.CAPTCHA_Guess == null || params.CAPTCHA_Guess == "") {
throw new Exception("No CAPTCHA guess detected, try again!")
}
def guess = params.CAPTCHA_Guess as Long
def hash = params.CAPTCHA_Hash as Long
if ((guess ^ XOR_CONST) != hash) {
throw new Exception("CAPTCHA incorrect, try again")
}
echo "CAPTCHA validated OK"
} else {
echo "No CAPTCHA required, continuing"
}
}
stage('Checkout') {
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
checkout scm
sh ('bin/prep.sh') // Clean and prepare environment
stash includes: "**", excludes: ".git/", name: 'src'
}
}
}
stage('Validate') {
node {
unstash 'src'
wrap.call({
// Validate packer templates, check branch
sh ("./bin/validate.sh")
})
}
}
def terraform_prompt = 'Should we apply the Terraform plan?'
stage('Plan Terraform') {
node {
unstash 'src'
wrap.call({
prepEnv()
def verb = "plan"
if (params.Destroy_Terraform) {
verb += '-destroy';
terraform_prompt += ' WARNING: will DESTROY resources';
}
sh ("""
./bin/terraform.sh ${verb}
""")
})
stash includes: "**", excludes: ".git/", name: 'plan'
}
}
if (params.Apply_Terraform || params.Destroy_Terraform) {
// See https://support.cloudbees.com/hc/en-us/articles/226554067-Pipeline-How-to-add-an-input-step-with-timeout-that-continues-if-timeout-is-reached-using-a-default-value
def userInput = false
try {
timeout(time: 20, unit: 'MINUTES') {
userInput = input(message: terraform_prompt)
}
stage('Apply Terraform') {
node {
unstash 'plan'
wrap.call({
prepEnv()
sh ("./bin/terraform.sh apply")
})
}
}
} catch(err) { // timeout reached or other error
echo err.toString()
currentBuild.result = 'ABORTED'
}
}
if (params.Build_Container) {
stage('Build Application Containers'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh build ${params.Application}")
}
}
}
}
if (params.Push_Container) {
stage('Push Application Containers'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh push ${params.Application}")
}
}
}
}
if (params.Deploy_Container) {
stage('Deploy Application Containers'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh deploy ${params.Application}")
sh ("./bin/build.sh list-pods")
sh ("./bin/build.sh list-svcs")
}
}
}
}
if (params.Add_To_DNS) {
stage('ADD Application to DNS'){
node {
if (params.Deploy_Container) {
sh ("sleep 20")
}
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh add-dns ${params.Application}")
}
}
}
}
if (params.Update_Container) {
stage('Update Application Containers'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh update ${params.Application}")
sh ("./bin/build.sh describe-pod ${params.Application}")
}
}
}
}
if (params.Run_Jmeter) {
stage('Run Jmeter'){
node {
if (params.Deploy_Container || params.Update_Container) {
sh ("sleep 20")
}
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh run-jmeter-www ${params.Application}")
}
}
}
}
if (params.Delete_Container) {
stage('Delete Application Containers'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh delete ${params.Application}")
sh ("./bin/build.sh list-pods")
sh ("./bin/build.sh list-svcs")
}
}
}
}
if (params.Run_Build_Command) {
stage('Run Build Command'){
node {
timeout(time:default_timeout_minutes, unit:'MINUTES') {
sh ("./bin/build.sh ${Build_Command_Arguments}")
}
}
}
}