-
Notifications
You must be signed in to change notification settings - Fork 40
/
Jenkinsfile.template
238 lines (210 loc) · 7.41 KB
/
Jenkinsfile.template
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
/* generated jenkins file used for building and deploying AWS-infrastructure in projects */
def tfBackendPrefix
def stackConfig
def stackdeploy = false
def awsAccessKeyId
def awsSecretAccessKey
def awsRegion
def tfBackendS3Key
def workspaceKeyPrefix
def awsAccessKeyIdTesting
def awsSecretAccessKeyTesting
def awsRegionTesting
def tfvarsjsonfile
def tfvarsjson
@Library('ods-jenkins-shared-library@@shared_library_ref@') _
import org.ods.services.ServiceRegistry
import org.ods.services.BitbucketService
import org.ods.util.GitCredentialStore
odsComponentPipeline(
imageStreamTag: '@ods_namespace@/jenkins-agent-terraform:@agent_image_tag@',
branchToEnvironmentMapping: [
'master': 'dev',
]
) { context ->
stageGetConfig(context)
stageInit(context)
stageTest(context)
stagePlan(context)
stageDeploy(context)
stageSmoke(context)
stageReport(context)
}
def getEnvSettings(def tfBackendPrefix, def tfBackendS3Key, def workspace, def region ) {
def config = [
"TF_BACKEND_PREFIX=${tfBackendPrefix}",
"TF_BACKEND_S3KEY=${tfBackendS3Key}"
]
if (workspace) {
config << "TF_WORKSPACE=${workspace}"
}
if (region) {
config << "AWS_DEFAULT_REGION=${region}"
}
return config
}
//read and decide stage-environment
def stageGetConfig(def context) {
stage('GetConfig') {
if (context.environment) {
environmentFile = readYaml file: "./environments/${context.environment}.yml"
environmentFileTesting = readYaml file: "./environments/testing.yml"
stackdeploy = true
awsAccessKeyId = environmentFile.credentials.key.toLowerCase()
awsSecretAccessKey = environmentFile.credentials.secret.toLowerCase()
awsRegion = environmentFile.region.toLowerCase()
awsAccessKeyIdTesting = environmentFileTesting.credentials.key.toLowerCase()
awsSecretAccessKeyTesting = environmentFileTesting.credentials.secret.toLowerCase()
awsRegionTesting = environmentFileTesting.region.toLowerCase()
workspaceKeyPrefix = environmentFile.account
tfBackendS3Key = "${environmentFile.account}/${context.projectId}/${context.componentId}/${context.environment}"
echo "Stack Environment ${context.environment} "
echo "AWS Access Key ID ${awsAccessKeyId}"
echo "AWS Secret Access ${awsSecretAccessKey}"
echo "AWS Region ${awsRegion}"
// handle environment specific variables
// copy json from /environments/${context.environment}.auto.tfvars.json to /
if (fileExists("./environments/${context.environment}.json")) {
withEnv(["VARIABLESFILE=./environments/${context.environment}.json"])
{
def statusVarEnv = sh(script: '''
cp $VARIABLESFILE env.auto.tfvars.json && echo "Variables file $VARIABLESFILE" ''',
returnStatus: true)
if (statusVarEnv != 0) {
error "Can not copy json file!"
}
}
}
} else { error "No environment specified!"}
}
}
//initialize pre-requisites
def stageInit(def context) {
if (stackdeploy)
{
stage('Init')
{
def bitbucketService = ServiceRegistry.instance.get(BitbucketService)
bitbucketService.withTokenCredentials { String username, String pw ->
GitCredentialStore.configureAndStore(this, context.bitbucketUrl, username, pw)
}
def workspace = env.WORKSPACE
def statusTF = sh(script: '''
make create-tfvars''',
returnStatus: true)
if (statusTF != 0) {
error "Creation of tfvars failed!"
}
tfvarsjson = readJSON file: './terraform.tfvars.json'
}
} else { print "Stage Init has been skipped."}
}
def stageReport(def context) {
if (stackdeploy == true) {
stage('Report')
{
def status = sh(script: '''
make install-report ''',
returnStatus: true)
if (status != 0) {
error "Report failed!"
}
stash(name: "installation-test-reports-junit-xml-${context.componentId}-${context.buildNumber}", includes: 'build/test-results/test/default.xml' , allowEmpty: true)
stash(name: "changes-${context.componentId}-${context.buildNumber}", includes: 'reports/install/tf_created.log' , allowEmpty: true)
stash(name: "target-${context.componentId}-${context.buildNumber}", includes: 'reports/install/aws_deploy_account.log' , allowEmpty: true)
stash(name: "state-${context.componentId}-${context.buildNumber}", includes: 'reports/install/tf_show.log' , allowEmpty: true)
}
} else { print "Stage Report has been skipped."}
}
def stageSmoke(def context) {
if (stackdeploy == true) {
stage('Smoke')
{
withCredentials([
string(credentialsId: awsAccessKeyId, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: awsSecretAccessKey, variable: 'AWS_SECRET_ACCESS_KEY')
])
{
withEnv(getEnvSettings(workspaceKeyPrefix, tfBackendS3Key, tfvarsjson['meta_environment'], awsRegion))
{
def status = sh(script: '''
eval \"\$(rbenv init -)\" && \
make smoke-test ''',
returnStatus: true)
if (status != 0) {
error "Smoke-Test failed!"
}
}
}
}
} else { print "Stage Smoke Test port has been skipped."}
}
//execute make test of the project
def stageTest(def context) {
if (stackdeploy == true) {
stage('Test') {
withCredentials([
string(credentialsId: awsAccessKeyIdTesting, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: awsSecretAccessKeyTesting, variable: 'AWS_SECRET_ACCESS_KEY')
])
{
withEnv(getEnvSettings(workspaceKeyPrefix, tfBackendS3Key, null, awsRegionTesting))
{
def status = sh(script: '''
make test''',
returnStatus: true)
if (status != 0) {
error "Test failed!"
}
}
}
}
} else { print "Stage Test has been skipped." }
}
//execute make plan of the project
def stagePlan(def context) {
if (stackdeploy == true) {
stage('Plan')
{
withCredentials([
string(credentialsId: awsAccessKeyId, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: awsSecretAccessKey, variable: 'AWS_SECRET_ACCESS_KEY')
])
{
withEnv(getEnvSettings(workspaceKeyPrefix, tfBackendS3Key, tfvarsjson['meta_environment'], awsRegion))
{
def status = sh(script: '''
eval \"\$(rbenv init -)\" && \
make plan''',
returnStatus: true)
if (status != 0) {
error "Plan failed!"
}
}
}
}
} else { print "Stage Plan has been skipped." }
}
//execute make deploy of the stack
def stageDeploy(def context) {
if (stackdeploy == true) {
stage('Deploy') {
withCredentials([
string(credentialsId: awsAccessKeyId, variable: 'AWS_ACCESS_KEY_ID'),
string(credentialsId: awsSecretAccessKey, variable: 'AWS_SECRET_ACCESS_KEY')
])
{
withEnv(getEnvSettings(workspaceKeyPrefix, tfBackendS3Key, tfvarsjson['meta_environment'], awsRegion))
{
def status = sh(script: '''
eval \"\$(rbenv init -)\" && \
make deploy''',
returnStatus: true)
if (status != 0) {
error "Deploy failed!"
}
}
}
}
} else { print "Stage Deploy has been skipped." }
}