-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
158 lines (157 loc) · 5.69 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
pipeline {
agent any
options {
// Will set the build timeout at 5 minutes and disable concurrent builds.
timeout(time: 5, unit: 'MINUTES')
disableConcurrentBuilds()
}
triggers {
// All builds will happen upon github push
githubPush()
}
stages {
/*
Initialize all the pipline parameters and thresholds.
*/
stage("Initialize") {
steps {
initialize()
}
}
/*
The pre checks will spit out all the pipeline envs and parameters (This stage is added for debugging within Jenkins).
*/
stage("Pre-Checks") {
steps {
sh 'docker images'
}
}
/*
Will take the files from repo and will run them with a docker container at the root level. (If passes, will cache deps and destory container)
If no database is required use the following for testing.
----------
withDockerContainer(image: env.DOCKER_PYTHON_NAME, args: "-u root:root") {
sh "python --version"
sh "pip install --no-cache-dir pipenv"
sh "pipenv install --dev"
sh "pipenv run pytest"
}
*/
stage('Testing') {
steps {
script {
docker.image(env.DOCKER_DB_IMAGE_NAME).withRun("-h ${env.POSTGRES_HOST} -e POSTGRES_USER=${env.POSTGRES_USER} -e POSTGRES_PASSWORD=${env.POSTGRES_PASSWORD}") { db ->
docker.image(env.DOCKER_DB_IMAGE_NAME).inside("--link ${db.id}:db") {
sh '''
psql --version
export PGPASSWORD=${POSTGRES_PASSWORD}
export RETRIES=${RETRIES_DBPING_IN_SECONDS}
until psql -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
echo "Waiting for postgres server, $((RETRIES-=1)) remaining attempts..."
sleep 1
done
psql -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -c "CREATE DATABASE ${POSTGRES_DB_NAME}"
'''
}
docker.image(env.DOCKER_PYTHON_NAME).inside("-u root:root --link ${db.id}:db") {
sh "python --version"
sh "pip install --no-cache-dir pipenv"
sh "pipenv install --dev"
sh 'printenv'
sh "pipenv run pytest"
}
}
}
}
}
/*
If the testing stage passes, it will be proceed to building the image for the AWS ECR.
*/
stage('Building Image') {
when {
expression {
env.GIT_BRANCH == env.BRANCH_IMAGE_BUILD_PUSH
}
}
steps {
sh 'docker build -t $REGISTRY_NAME .'
}
}
/*
Will publish the image just build on Jenkins to AWS ECR as latest and the current build number
*/
stage('Publish Image') {
when {
expression {
env.GIT_BRANCH == env.BRANCH_IMAGE_BUILD_PUSH
}
}
steps {
sh 'aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $REGISTRY_URI/$REGISTRY_NAME'
sh 'docker tag $REGISTRY_NAME:latest $REGISTRY_URI/$REGISTRY_NAME:latest'
sh 'docker push $REGISTRY_URI/$REGISTRY_NAME:latest'
sh 'docker tag $REGISTRY_NAME:latest $REGISTRY_URI/$REGISTRY_NAME:$TAGNAME'
sh 'docker push $REGISTRY_URI/$REGISTRY_NAME:$TAGNAME'
}
}
/*
Will clean up the Jenkins server of any cached builds or source. (Only the python and db images are saved so they don't have to be pulled every time)
*/
stage('Clean Up') {
when {
expression {
env.GIT_BRANCH == env.BRANCH_IMAGE_BUILD_PUSH
}
}
steps {
sh 'docker rmi $REGISTRY_URI/$REGISTRY_NAME'
sh 'docker rmi $REGISTRY_NAME:latest'
sh 'docker rmi $REGISTRY_URI/$REGISTRY_NAME:$TAGNAME'
}
}
stage('Deploy') {
when {
expression {
env.GIT_BRANCH == env.BRANCH_ALLOW_DEPLOYMENT
}
}
steps {
echo 'Deploying to development server...'
sshagent([env.K8_SERVER_SSH_KEY_NAME]) {
sh 'ssh -o StrictHostKeyChecking=no -T $K8_USERNAME@$K8_HOST kubectl set image deployment $K8_APP_DEPLOYMENT_NAME $K8_APP_SERVICE_NAME=$REGISTRY_URI/$REGISTRY_NAME:$TAGNAME'
}
}
}
}
}
def initialize() {
// Docker Defs
env.DOCKER_DB_IMAGE_NAME = 'postgres:11.1'
env.DOCKER_PYTHON_NAME = 'python:3.7-slim'
// AWS ERC Parameters / Push Rules
env.REGISTRY_NAME = 'brighthive/google-pathways-web-app'
env.REGISTRY_URI = '396527728813.dkr.ecr.us-east-2.amazonaws.com'
env.BRANCH_IMAGE_BUILD_PUSH = 'master'
env.BRANCH_ALLOW_DEPLOYMENT = 'master'
env.SYSTEM_NAME = 'Jenkins'
env.IS_JENKINS_TEST = '1'
env.AWS_REGION = 'us-east-2'
env.MAX_ENVIRONMENTNAME_LENGTH = 32
env.BUILD_VERSION = '1.1.0'
env.TAGNAME = env.BUILD_VERSION + '-' + env.GIT_COMMIT.substring(0,5)
// DB Configs
env.POSTGRES_HOST = 'localhost'
env.POSTGRES_PORT = 5432
env.RETRIES_DBPING_IN_SECONDS = 60
env.POSTGRES_USER = 'dt_admin_test'
env.POSTGRES_PASSWORD = 'passw0rd'
env.POSTGRES_DB_NAME = 'pathways_test'
// Flask App Configs
env.BASE_URL = 'http://0.0.0.0:8000'
// K8 Deployment Parameters
env.K8_SERVER_SSH_KEY_NAME = 'dev-k8-server-ssh'
env.K8_APP_SERVICE_NAME = 'pathways-app'
env.K8_APP_DEPLOYMENT_NAME = 'pathways-app'
env.K8_USERNAME = 'ubuntu'
env.K8_HOST = 'ec2-3-90-205-205.compute-1.amazonaws.com'
}