-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
247 lines (238 loc) · 11.1 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
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
#!/usr/bin/env groovy
properties(
[
parameters(
[
string(
name: 'Branch_To_Checkout',
defaultValue: '',
description: "Please enter the name of the Branch you would like to checkout."
),
choice(
name: 'build_option',
choices: ['', 'plan', 'apply', 'destroy'],
description: 'Apply will deploy the resources, Destroy will destroy the resources'
)
]
),
buildDiscarder(
logRotator(
artifactDaysToKeepStr: '5',
artifactNumToKeepStr: '1',
daysToKeepStr: '30',
numToKeepStr: '20'
)
),
disableConcurrentBuilds(),
gitLabConnection(),
pipelineTriggers(
[[
$class : "GitLabPushTrigger",
triggerOnPush : true,
triggerOnMergeRequest : true,
triggerOpenMergeRequestOnPush : "never",
triggerOnNoteRequest : true,
noteRegex : "Jenkins please retry a build",
skipWorkInProgressMergeRequest : true,
ciSkip : false,
setBuildDescription : true,
branchFilterType : "NameBasedFilter"
includeBranchesSpec : "master",
excludeBranchesSpec : ""
]]
)
]
)
def gitSource = ''
def gitCredentialsId = 's.rft.ciBuild_ssh'
def useTag = true
def tag = params.tag
def build_option = params.build_option
def TF_WORKING_FOLDER = 'terraform-infra/env/us-east-1/dev'
def TF_VAR_BUILD_NUMBER = 'dev-${BUILD_NUMBER}'
def TF_VERSION = '1.0.5'
def envType = 'dev'
def branch = branchToBuild
class AwsInfo implements Serializable {
public String awsRegion
public String awsRole
public String awsRoleAccount
public String awsProfile
public String assetID
}
def awsInfo = new AwsInfo(
awsRegion : '',
assetID : '',
awsProfile : 'default',
awsRole : '',
awsRoleAccount : ''
)
class TfInfo implements Serializable {
public String state_bucket
public String state_key
public String state_lockable_table
}
def tfInfo = new TfInfo(
state_bucket : "a${awsInfo.assetID}-sem2dm-terraform-state-${envType}-use1",
state_lockable_table : "a${awsInfo.assetID}-sem2dm-terraform-state-lock-${envType}-use1",
state_key : "${envType}/sem2dm-infra-resources/terraform.tfstate"
)
echo '\u2600 ftInfo = ' + tfInfo.state_bucket
if (build_option == null || !['plan', 'apply', 'destroy'].contains(build_option)) {
currentBuild.result = 'Failure'
error('unsupported build option: ' + build_option)
}
timestamps {
try {
node(jenkinsDockerImage) {
echo '\u2600 Environment Variables'
sh "env"
stage('preparation') {
echo '\u2756 Preparation'
echo '\u2600 parameter tag = ' + tag
if (tag == null tag == '') {
echo '\u27A1 Checkout branch ' + branch
useTag = false
} else {
echo '\u27A1 Checkout by tag ' + tag
useTag = true
}
if (!useTag) {
checkout(
[
$class : 'GitSCM',
branches : [[name: '*/' + branch]],
doGenerateSubmoduleconfigurations : false
extensions : [[
$class : 'submoduleOption',
disableSubmodules : false,
parentCredentials : true,
recursiveSubmodules : true,
reference : '',
trackingSubmodules : false
]],
gitTool : 'Default',
submoduleCfg : [],
userRemoteConfigs : [[
credentialsId : gitCredentialsId,
url : gitSource
]],
]
)
} else {
checkout scm:
[
$class : 'GitSCM',
branches: : [[
name : 'refs/tags/' + tag
]],
doGenerateSubmoduleconfigurations : false,
extensions : [[
$class : 'SubmoduleOption',
disableSubmodules : false,
parentCredentials : true,
recursiveSubmodules : true,
reference : '',
trackingSubmodules : false
]],
gitTool : 'Default',
submoduleCfg : [],
userRemoteConfigs : [[
credentialsId : gitCredentialsId,
url : gitSource
]]
],
poll: false
}
}
withAWS(region: awsInfo.awsRegion, role: awsInfo.awsRole, roleAccount: awsInfo.awsRoleAccount) {
dir("${TF_WORKING_FOLDER}") {
environment {
TF_PLUGIN_CACHE_DIR = "../.plugins"
}
stage ('parameters') {
sh """
echo "You have selected: ${params.build_option}"
"""
}
stage ('Build Phase: Plan') {
// Remove terraform state so we always start from a clean state
if (fileExists(".terraform/terraform.tfstate"))
{
sh "rm -rf .terraform/terraform.tfstate"
}
if (fileExists("status"))
{
sh "rm status"
}
sh """
rm -rf tf_installer.zip terraform
wget -O tf_installer.zip https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip
unzip tf_installer.zip
chmod a+x terraform
./terraform init -no-color -input=true \
-backend-config="bucket=${tfInfo.state_bucket}" \
-backend-config="region=${awsInfo.awsRegion}" \
-backend-config="dynamodb_table=${tfInfo.state_lock_table}" \
-backend-config="key=${tfInfo.state_key}" \
| grep -v 'Downloading git'
ls -a
./terraform -version
./terraform plan -var-file="./env/us-east-1/${envType}/${envType}.tfvars" \
-lock=false -no-color -input=false
"""
}
stage('Build Phase: Apply') {
if (params.build_option == 'apply') {
sh """
rm -rf tf_installer.zip terraform
wget -O tf_installer.zip https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip
unzip tf_installer.zip
chmod a+x terraform
./terraform init -no-color -input=true \
-backend-config="bucket=${tfInfo.state_bucket}" \
-backend-config="region=${awsInfo.awsRegion}" \
-backend-config="dynamodb_table=${tfInfo.state_lock_table}" \
-backend-config="key=${tfInfo.state_key}" \
| grep -v 'Downloading git'
ls -a
./terraform -version
./terraform apply -var-file="./env/us-east-1/${envType}/${envType}.tfvars" -no-color -input=false -auto-approve
"""
}
}
stage('Build Phase: destroy') {
if (params.build_option == destroy) {
// Remove terraform state so we always start from a clean state
if (fileExists(".terraform/terraform.tfstate")) {
sh "rm -rf .terraform/terraform.tfstate"
}
if (fileExists("status")) {
sh "rm status"
}
sh """
rm -rf tf_installer.zip terraform
wget -O tf_installer.zip https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip
unzip tf_installer.zip
chmod a+x terraform
./terraform init -no-color -input=true \
-backend-config="bucket=${tfInfo.state_bucket}" \
-backend-config="region=${awsInfo.awsRegion}" \
-backend-config="dynamodb_table=${tfInfo.state_lock_table}" \
-backend-config="key=${tfInfo.state_key}" \
| grep -v 'Downloading git'
ls -a
./terraform -version
./terraform destroy -var-file="./env/us-east-1/${envType}/${envType}.tfvars" -no-color -input=false -auto-approve
"""
}
}
}
}
currentBuild.result = 'SUCCESS'
}
} catch (exc) {
currentBuild.result = 'FAILURE'
throw exc
}
}