-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
143 lines (121 loc) · 5.33 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
import groovy.transform.Field
import groovy.json.JsonSlurperClassic
import net.sf.json.JSONArray
import net.sf.json.JSONObject
// other parameters which is not required
@Field def buildRootDir = "AakashCode"
@Field def slackMessageChannel = "#cloud-deployments"
// waf params
@Field def wafEndpointType = "ALB"
def sendSlackMessage(titleText, messageText, messageColor, channelName){
echo "Message Sent"
}
def downloadFileFromGit(gitUrl, branchName, filePath) {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'githubcredentials',
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GIT_PASSWORD']]) {
// Get the waf.yaml from devops repo
//sh "git archive --remote=${gitUrl} --format=tar ${branchName} ${filePath} | tar xf -"
sh "svn export https://github.com/Akayrathee/cloudformation/trunk AakashCode --force"
sh "ls"
}
}
def updateCloudFormationStacksParallel(stackName, stackRegion, cfnParams) {
cfnUpdateTasks["${stackName}"] = {
node {
stage("${stackName}") {
script {
try {
sendSlackMessage(
"WAF Config Sync in Progress...",
" ► Stack: ${stackName} \n ► Region: ${stackRegion}\n",
'good',
slackMessageChannel
)
def gitUrl = "[email protected]:Akayrathee/cloudformation.git"
def branchName = "master"
def filePath = "waf.yaml"
downloadFileFromGit(gitUrl, branchName, filePath)
// def waf = readYaml file: 'waf.yaml'
echo "We are here"
withAWS(credentials: 'aakashawscredntials', region: stackRegion){
def outputs = cfnUpdate(
stack:"${stackName}",
file:"AakashCode/waf.yaml",
params:cfnParams,
timeoutInMinutes:180,
pollInterval:10000
)
print(outputs)
}}
catch(error) {
allCfnUpdateSuccessful = false
// Alert to slack about failure
echo("Updation of the stack ${stackName} failed. Error = " + error.toString())
sendSlackMessage(
"WAF Sync Failure",
" ► Stack: ${stackName} \n ► Region: ${stackRegion}\n ► buildUrl: ${env.BUILD_URL}\n",
'danger',
slackMessageChannel
)
}
}
}
}
}
}
pipeline {
agent any
stages {
stage("EB configuration check") {
steps {
timestamps {
script {
def gitUrl = "[email protected]:Akayrathee/cloudformation.git"
def branchName = "master"
def filePath = "config.yaml"
def datas = readYaml file: 'config.yaml'
cfnUpdateTasks = [:]
allCfnUpdateSuccessful = true
echo "Data : ${datas}"
for(data in datas.wafV2Config) {
echo "Updating WAF-${data.stackName} ..."
def cfnParams = new JsonSlurperClassic().parseText("{}")
cfnParams["ActivateHttpFloodProtectionParam"] = data.activateHttpFloodProtectionParam
cfnParams["AssociatedResourceArn"] = data.albArn
cfnParams["EndpointType"] = wafEndpointType
cfnParams["RateLimitRuleAction"] = data.rateLimitRuleAction
cfnParams["RequestThreshold"] = data.requestThreshold
echo "Cloudformation params: ${cfnParams}"
updateCloudFormationStacksParallel("WAF-${data.stackName}", data.region, cfnParams)
}
echo "Updating stacks in parallel..."
echo "${cfnUpdateTasks}"
parallel cfnUpdateTasks
echo "Parallel tasks complete."
if (!allCfnUpdateSuccessful) {
error("One or more stackUpdation failed; Please check the log for more information.")
}
}
}
}
}
}
post {
failure {
sendSlackMessage(
"WafDeploymentPipeline Failure",
" Sync Failed\n ► buildUrl: ${env.BUILD_URL}\n",
'danger',
slackMessageChannel
)
echo "Failure"
}
cleanup {
echo "Cleaning Workspace.."
cleanWs()
echo "Exiting Script"
}
}
}