This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
/
Jenkinsfile.groovy
153 lines (136 loc) · 4.78 KB
/
Jenkinsfile.groovy
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
/**
* Jenkins script that creates AWS infrastructure for fn.saintsxctf.com.
* @author Andrew Jarombek
* @since 11/21/2020
*/
@Library(['global-jenkins-library@master']) _
pipeline {
agent {
label 'master'
}
parameters {
booleanParam(
name: 'autoApply',
defaultValue: true,
description: "Whether the Terraform infrastructure should be automatically approved."
)
choice(
name: 'environment',
choices: ['dev', 'prod'],
description: 'Environment to build the infrastructure in.'
)
}
options {
ansiColor('xterm')
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(daysToKeepStr: '10', numToKeepStr: '5'))
}
stages {
stage("Clean Workspace") { steps { script { cleanWs() } } }
stage("Checkout Repositories") { steps { script { checkoutRepos() } } }
stage("Get Lambda Zip Files") { steps { script { createLambdaZipFiles() } } }
stage("Terraform Init") { steps { script { terraformInit() } } }
stage("Terraform Validate") { steps { script { terraformValidate() } } }
stage("Terraform Plan") { steps { script { terraformPlan() } } }
stage("Terraform Apply") {
when {
allOf {
environment name: 'TERRAFORM_NO_CHANGES', value: 'false'
environment name: 'TERRAFORM_PLAN_ERRORS', value: 'false'
}
}
steps { script { terraformApply() } }
}
}
post {
always {
script {
postScript()
}
}
}
}
def checkoutRepos() {
genericsteps.checkoutRepo('saints-xctf-infrastructure', 'master')
genericsteps.checkoutRepo('saints-xctf-functions', 'main')
}
def createLambdaZipFiles() {
dir('repos/saints-xctf-functions/forgot-password') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFForgotPasswordEmail.zip .
cp SaintsXCTFForgotPasswordEmail.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/email-lambda
"""
}
dir('repos/saints-xctf-functions/send-activation') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFActivationCodeEmail.zip .
cp SaintsXCTFActivationCodeEmail.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/email-lambda
"""
}
dir('repos/saints-xctf-functions/report') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFReportEmail.zip .
cp SaintsXCTFReportEmail.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/email-lambda
"""
}
dir('repos/saints-xctf-functions/welcome') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFWelcomeEmail.zip .
cp SaintsXCTFWelcomeEmail.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/email-lambda
"""
}
dir('repos/saints-xctf-functions/upload-profile-picture') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFUassetUser.zip .
cp SaintsXCTFUassetUser.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/uasset-lambda
"""
}
dir('repos/saints-xctf-functions/upload-group-picture') {
sh """
yarn install --production=true
zip -r9 SaintsXCTFUassetGroup.zip .
cp SaintsXCTFUassetGroup.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/uasset-lambda
"""
}
dir('repos/saints-xctf-functions/signed-url-profile-picture') {
sh """
zip -r9 SaintsXCTFUassetSignedUrlUser.zip .
cp SaintsXCTFUassetSignedUrlUser.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/uasset-lambda
"""
}
dir('repos/saints-xctf-functions/signed-url-group-picture') {
sh """
zip -r9 SaintsXCTFUassetSignedUrlGroup.zip .
cp SaintsXCTFUassetSignedUrlGroup.zip ../../saints-xctf-infrastructure/saints-xctf-com-fn/modules/uasset-lambda
"""
}
}
def terraformInit() {
INFRA_DIR = "repos/saints-xctf-infrastructure/saints-xctf-com-fn/env/$params.environment"
terraform.terraformInit(INFRA_DIR)
}
def terraformValidate() {
terraform.terraformValidate(INFRA_DIR)
}
def terraformPlan() {
terraform.terraformPlan(INFRA_DIR)
}
def terraformApply() {
terraform.terraformApply(INFRA_DIR, params.autoApply)
}
def postScript() {
email.sendEmail(
"Create ${params.environment.toUpperCase()} SaintsXCTF Functions AWS Infrastructure",
"",
env.JOB_NAME,
currentBuild.result,
env.BUILD_NUMBER,
env.BUILD_URL
)
cleanWs()
}