forked from fecgov/fecfile-web-app
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
189 lines (175 loc) · 5.79 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
pipeline {
agent any
stages {
stage('Prepare Build'){
steps {
script{
hash = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
VERSION = hash.take(7)
currentBuild.displayName = "#${BUILD_ID}-${VERSION}"
sh("eval \$(aws ecr --region us-east-1 get-login --no-include-email)")
}
}
}
// Deploy to develop branch
stage('Development') {
when { branch 'develop'}
stages {
stage("Build Images") {
parallel {
stage("Backend") {
steps { buildBack("${VERSION}", "awsdev") }
}
stage("front-end") {
steps { imageBuild("${VERSION}", "awsdev") }
}
}
}
stage("Deploy App") {
steps {
//Deploy backend
deployToK8s("${VERSION}", "dev","fecfile-backend-api","fecnxg-django-backend")
//Deploy frontend
deployToK8s("${VERSION}", "dev", "fecfile-frontend","fecnxg-frontend")
}
}
stage('Code Quality') {
steps {
code_quality()
}
}
}
}
//End of developent stage
stage('QA'){
when { branch 'release'}
stages {
stage("Build Images"){
parallel {
stage("Backend") {
steps { buildBack("${VERSION}", "awsqa") }
}
stage("front-end") {
steps { imageBuild("${VERSION}qa", "awsqa") }
}
}
}
stage("Deploy App"){
steps{
//Deploy backend
deployToK8s("${VERSION}", "qa","fecfile-backend-api","fecnxg-django-backend")
//Deploy frontend
deployToK8s("${VERSION}qa", "qa", "fecfile-frontend","fecnxg-frontend")
}
}
}
}
//End of qa stage
stage('UAT'){
when { branch 'main'}
stages {
stage("Build Images"){
parallel {
stage("Backend") {
steps { buildBack("${VERSION}", "awsuat") }
}
stage("front-end") {
steps { imageBuild("${VERSION}uat", "awsuat") }
}
}
}
stage("Deploy App"){
steps{
//Deploy backend
deployToK8s("${VERSION}", "uat","fecfile-backend-api","fecnxg-django-backend")
//Deploy frontend
deployToK8s("${VERSION}uat", "uat", "fecfile-frontend","fecnxg-frontend")
}
}
}
}
//end of UAT stage
}
post {
always {
sh " docker image prune -a -f "
}
success {
message_on_dev("good", ": Deployed ${VERSION} to https://fecfile.efdev.fec.gov/")
}
failure {
message_on_dev("danger", ": Deployement of ${VERSION} to https://fecfile.efdev.fec.gov/ failed!")
}
}
}
def message_on_dev(String col, String mess){
if( env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'release' || env.BRANCH_NAME == 'master'){
slackSend color: col, message: env.BRANCH_NAME + mess
}
}
def code_quality() {
//Build python3.6 virtualenv
sh """
virtualenv -p python36 .venv
source .venv/bin/activate
pip3 install flake8 flake8-junit-report
mkdir -p reports
flake8 --exit-zero django-backend --output-file reports/${BUILD_ID}-${VERSION}-flake8.txt
flake8_junit reports/${BUILD_ID}-${VERSION}-flake8.txt reports/${BUILD_ID}-${VERSION}-flake8_junit.xml
deactivate
rm -fr .venv
"""
junit '**/reports/*.xml'
}
def imageBuild(String version, String frontend_env) {
if(frontend_env == "awsdev"){
sh("sed -i 's/=local/=${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/VERDEPLOYED/${version}/g' front-end/Dockerfile")
}
if (frontend_env == "awsqa") {
sh("sed -i 's/=local/=${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/awsdev/${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/VERDEPLOYED/${version}/g' front-end/Dockerfile")
}
if (frontend_env == "awsuat") {
sh("sed -i 's/=local/=${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/awsdev/${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/awsqa/${frontend_env}/g' front-end/Dockerfile")
sh("sed -i 's/VERDEPLOYED/${version}/g' front-end/Dockerfile")
sh("sed -i 's/--source-map=true//g' front-end/Dockerfile")
}
def imageB = docker.build("fecnxg-frontend:${version}", 'front-end/')
docker.withRegistry("https://813218302951.dkr.ecr.us-east-1.amazonaws.com/fecnxg-frontend") {
imageB.push()
}
}
def buildBack(String version, String denvi) {
if ( denvi == "awsdev") {
sh("sed -i 's/gunicorn/gunicorn --log-level=debug/g' django-backend/Dockerfile")
}
def imageBack = docker.build("fecnxg-django-backend:${version}", 'django-backend/')
docker.withRegistry('https://813218302951.dkr.ecr.us-east-1.amazonaws.com/fecnxg-django-backend') {
imageBack.push()
}
}
def build_flyway(String version) {
sh("sed -i '11d' data/flyway_migration.sh")
def imageC = docker.build("fecfile-flyway-db:${version}", "data/")
docker.withRegistry("https://813218302951.dkr.ecr.us-east-1.amazonaws.com/fecfile-flyway-db") {
imageC.push()
}
}
def build_functions(String version) {
def imageF = docker.build("fecnxg-functions:${version}", "scripts/lambda/")
docker.withRegistry("https://813218302951.dkr.ecr.us-east-1.amazonaws.com/fecnxg-functions") {
imageF.push()
}
}
def deployToK8s(String version, String environment, String deployment, String repo) {
sh """
kubectl16 \
--context=arn:aws:eks:us-east-1:813218302951:cluster/fecnxg-dev1 \
--namespace=${environment} \
set image deployment/${deployment} ${deployment}=813218302951.dkr.ecr.us-east-1.amazonaws.com/${repo}:${version}
"""
}