-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsFile
More file actions
244 lines (244 loc) · 12.9 KB
/
JenkinsFile
File metadata and controls
244 lines (244 loc) · 12.9 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
// IMPORTANT NOTE, USE OF STATE FILES NOT REQUIRED IF USING BACKEND / WORKSPACES
def refresh = false
pipeline{
options {
buildDiscarder(logRotator(numToKeepStr: '2', daysToKeepStr: '1', artifactDaysToKeepStr: '3', artifactNumToKeepStr: '4'))
}
agent any
parameters {
string(name: "deploymentId", defaultValue: "5e41386ca2bd9c0012eb54aa", description: 'DeploymentId, Group Resources/Configuration and Tagging', trim: true)
}
environment{
AWS_ACCESS_KEY = credentials("AWS_ACCESS_KEY")
AWS_SECRET_KEY = credentials("AWS_SECRET_KEY")
AWS_REGION = credentials("AWS_REGION")
TF_IN_AUTOMATION = "true"
}
stages{
stage("Clone"){
steps{
// ALWAYS DICARD LOCAL REPO
echo "======== Clone Repo ========"
dir("./"){
script{
try {
def file ="./terraform-aws-state/state.tf"
def fileFound = fileExists(file)
println("state.tf, exists = ${fileFound}")
if(!fileFound){
sh "rm -R ./terraform-aws-state/ -f"
}
sh "git init"
sh "git clone https://github.com/molupini/terraform-aws-state.git"
} catch (Exception err){
println(err)
throw new Exception("Unable to perform clone, try again!")
}
}
}
}
}
stage("Init"){
steps{
// CACHE WORKING PLUGINS
echo "======== Executing Init ========"
dir("./terraform-aws-state/bin"){
script{
try {
// CACHE ONLY SPECIFIC DEPLOYMENT, #
// def file = "../../${params.deploymentId}/.terraform/plugins/linux_amd64/lock.json"
// CACHE ENTIRE WORKSPACE
def file = "../../.terraform/plugins/linux_amd64/lock.json"
def fileFound = fileExists(file)
println("lock.json, exists = ${fileFound}")
if(fileFound){
sh "terraform init \
-plugin-dir ../../.terraform/plugins/linux_amd64"
// #
// -plugin-dir ../../${params.deploymentId}/.terraform/plugins/linux_amd64"
}
else {
sh "terraform init"
// #
// sh "cp -r ./.terraform/ ../../${params.deploymentId}/"
sh "cp -r ./.terraform/ ../../"
}
sh "python3 ./util/helper.py status ${params.deploymentId} 1"
} catch (Exception err){
// #
// sh "rm -R ../../${params.deploymentId}/.terraform/ -f"
sh "rm -R ../../.terraform/ -f"
sh "terraform init \
-plugin-dir ''"
// println(err)
// COMMENT OUT THROW FOR THE INTERIM
// throw new Exception("Unable to perform init, try again!")
sh "python3 ./util/helper.py status ${params.deploymentId} 2"
}
}
}
}
}
stage("Validate"){
steps{
// VERIFY WORKING HCL / TERRAFORM SOURCE CODE
echo "======== Executing Validation ========"
dir("./terraform-aws-state/bin"){
script{
try {
sh "terraform validate"
} catch (Exception err){
println(err)
throw new Exception("Unable to perform validation, try again!")
}
}
}
}
}
stage("Plan"){
steps{
// PLAN WILL VERIFY IF STATE EXISTS, IF FALSE - OUTPUT TO DEPLOYMENT-ID SUB FOLDER, IF TRUE - INPUT FROM DEPLOYMENT-ID SUB FOLDER
echo "======== Executing Plan ========"
dir("./terraform-aws-state/bin"){
script{
try {
def file ="../../${params.deploymentId}/terraform.tfstate"
def fileFound = fileExists(file)
println("terraform.tfstate, exists = ${fileFound}")
if(!fileFound){
sh "mkdir -p ../../${params.deploymentId}"
sh(script: """
terraform plan \
-var "aws_access_key=${AWS_ACCESS_KEY}" \
-var "aws_secret_key=${AWS_SECRET_KEY}" \
-var "aws_region=${AWS_REGION}" \
-var "dyn_tag=`python3 ./util/helper.py app ${params.deploymentId} tag dyn`" \
-var "s3_tag=`python3 ./util/helper.py app ${params.deploymentId} tag s3`" \
-var "grp_tag=`python3 ./util/helper.py app ${params.deploymentId} tag grp`" \
-var "state_config=`python3 ./util/helper.py app ${params.deploymentId} config state`" \
-out ../../${params.deploymentId}/apply.tfplan
""")
sh "terraform show ../../${params.deploymentId}/apply.tfplan"
} else {
sh(script: """
terraform plan \
-var "aws_access_key=${AWS_ACCESS_KEY}" \
-var "aws_secret_key=${AWS_SECRET_KEY}" \
-var "aws_region=${AWS_REGION}" \
-var "dyn_tag=`python3 ./util/helper.py app ${params.deploymentId} tag dyn`" \
-var "s3_tag=`python3 ./util/helper.py app ${params.deploymentId} tag s3`" \
-var "grp_tag=`python3 ./util/helper.py app ${params.deploymentId} tag grp`" \
-var "state_config=`python3 ./util/helper.py app ${params.deploymentId} config state`" \
-state ../../${params.deploymentId}/terraform.tfstate \
-out ../../${params.deploymentId}/apply.tfplan
""")
//
sh "terraform show ../../${params.deploymentId}/apply.tfplan"
}
sh "python3 ./util/helper.py status ${params.deploymentId} 3"
} catch (Exception err){
println(err)
throw new Exception("Unable perform plan, try again!")
sh "python3 ./util/helper.py status ${params.deploymentId} 4"
}
}
}
}
}
stage("Apply"){
steps{
// INPUT REQUIRED IF TO APPLY OR ABOUT WHICH WILL DESTROY
// APPLY FALSE - WILL PLAN THE DESTROY AND THEN APPLY
// APPLY TRUE - SEE BELOW #
echo "======== Executing Apply ========"
script {
def apply = false
try {
input message: "Confirm Apply", ok: "Apply Config"
apply = true
} catch (Exception err) {
dir("./terraform-aws-state/bin"){
// PLAN DESTROY
sh(script: """
terraform plan -destroy \
-var "aws_access_key=${AWS_ACCESS_KEY}" \
-var "aws_secret_key=${AWS_SECRET_KEY}" \
-var "aws_region=${AWS_REGION}" \
-var "dyn_tag=`python3 ./util/helper.py app ${params.deploymentId} tag dyn`" \
-var "s3_tag=`python3 ./util/helper.py app ${params.deploymentId} tag s3`" \
-var "grp_tag=`python3 ./util/helper.py app ${params.deploymentId} tag grp`" \
-var "state_config=`python3 ./util/helper.py app ${params.deploymentId} config state`" \
-out ../../${params.deploymentId}/destroy.tfplan \
-state ../../${params.deploymentId}/terraform.tfstate
""")
sh "terraform show ../../${params.deploymentId}/destroy.tfplan"
// APPLY DESTROY
// TODO MIGHT REQUIRE ANOTHER INPUT, IF NECESSARY TO VERIFY ACTION
def file ="../../${params.deploymentId}/destroy.tfplan"
def fileFound = fileExists(file)
println("destroy.tfplan, exists = ${fileFound}")
if(fileFound){
sh "terraform apply \
-state ../../${params.deploymentId}/terraform.tfstate \
../../${params.deploymentId}/destroy.tfplan"
}
// sh "terraform output -state ../../${params.deploymentId}/terraform.tfstate -json > ${params.deploymentId}-output.json"
sh "python3 ./util/helper.py status ${params.deploymentId} 8"
}
currentBuild.result = "UNSTABLE"
}
// # APPLY - TRUE, BUILD
// IF ANY ERROR IN CATCH FORCE REFRESH
if(apply){
dir("./terraform-aws-state/bin"){
def file ="../../${params.deploymentId}/apply.tfplan"
def fileFound = fileExists(file)
println("apply.tfplan, exists = ${fileFound}")
if(fileFound){
try {
sh "terraform apply \
-state ../../${params.deploymentId}/terraform.tfstate \
../../${params.deploymentId}/apply.tfplan"
} catch (Exception err) {
refresh = true
}
}
// sh "terraform output -state ../../${params.deploymentId}/terraform.tfstate -json > ${params.deploymentId}-output.json"
sh "python3 ./util/helper.py status ${params.deploymentId} 6"
}
}
}
}
}
// TODO VERIFY IF NECESSARY
stage("Refresh"){
steps{
echo "======== Executing Refresh ========"
dir("./terraform-aws-state/bin"){
script{
try {
println("refresh = ${refresh}")
if(refresh){
sh(script: """
terraform refresh \
-var "aws_access_key=${AWS_ACCESS_KEY}" \
-var "aws_secret_key=${AWS_SECRET_KEY}" \
-var "aws_region=${AWS_REGION}" \
-var "dyn_tag=`python3 ./util/helper.py app ${params.deploymentId} tag dyn`" \
-var "s3_tag=`python3 ./util/helper.py app ${params.deploymentId} tag s3`" \
-var "grp_tag=`python3 ./util/helper.py app ${params.deploymentId} tag grp`" \
-var "state_config=`python3 ./util/helper.py app ${params.deploymentId} config state`" \
-state ../../${params.deploymentId}/terraform.tfstate
""")
currentBuild.result = "UNSTABLE"
}
} catch (Exception err){
println(err)
throw new Exception("Unable to perform refresh, try again!")
}
}
}
}
}
}
}