-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
150 lines (137 loc) · 5.86 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
#!/usr/bin/env groovy
// docker inspect e1a2a16cab23 | grep IPAddress | grep -v '""' | grep -v null | cut -d '"' -f 4
pipeline {
agent {
docker {
image 'jedster1111/pollingappe2e:latest'
args '-u root'
alwaysPull 'true'
}
}
environment {
GITHUB_CLIENT_ID = credentials('DEV_GITHUB_CLIENT_ID')
GITHUB_CLIENT_SECRET = credentials('DEV_GITHUB_CLIENT_SECRET')
GOOGLE_CLIENT_ID = credentials('GOOGLE_CLIENT_ID')
GOOGLE_CLIENT_SECRET = credentials('GOOGLE_CLIENT_SECRET')
SECRET_KEY = credentials('SECRET_KEY')
TEST_USERNAME = credentials('TEST_USERNAME')
TEST_PASSWORD = credentials('TEST_PASSWORD')
}
stages {
stage('Setup') {
steps {
echo 'Setting up...'
sh 'printenv | sort'
echo 'Installing node dependencies'
sh 'yarn'
echo "${env.CLIENT_ID}"
}
}
stage('Unit Tests') {
steps {
echo 'Testing...'
sh 'yarn test'
}
}
stage('Building image') {
steps {
echo "Building image"
script {
imageId = sh(returnStdout: true, script: "docker build -t jedster1111/pollingapp:${GIT_COMMIT} -t jedster1111/pollingapp:latest -q -f dockerfiles/pollingapp/Dockerfile .").trim()
}
echo "Finished building image. Tagged as pollingappdev:${GIT_COMMIT} and pollingappdev:latest"
}
}
stage("Dev Deploy") {
when {
branch 'development'
}
options {
lock('devServer')
}
stages {
stage('Stopping pollingappdev container') {
when {
not {
expression {
sh(returnStdout: true, script: 'docker container ls --all -f name=pollingappdev --format "{{.ID}}"').trim() == ""
}
}
}
steps {
echo 'Container exists so we need to stop it...'
sh 'docker stop pollingappdev'
sh 'docker rm pollingappdev'
}
}
stage('Deploying to dev') {
environment {
DEV_URL = 'https://dev.pollingapp.jedthompson.co.uk'
}
steps {
echo "Deploying commit ${GIT_COMMIT} to dev server"
sh "docker run -v /database/dev:/usr/src/app/database -e GITHUB_CLIENT_ID -e GITHUB_CLIENT_SECRET -e GOOGLE_CLIENT_ID -e GOOGLE_CLIENT_SECRET -e SECRET_KEY -e VIRTUAL_HOST=dev.pollingapp.jedthompson.co.uk -e LETSENCRYPT_HOST=dev.pollingapp.jedthompson.co.uk -e [email protected] -e POLLING_APP_URL=${DEV_URL} --restart=on-failure --name pollingappdev --net docker-compose_default -d ${imageId}"
echo 'Deployment succesful! Should be accessible at dev.pollingapp.jedthompson.co.uk'
}
}
stage("E2E tests") {
environment {
TESTCAFE_URL = 'https://dev.pollingapp.jedthompson.co.uk'
}
steps {
echo 'Running E2E tests'
sh 'testcafe \"chromium --headless --no-sandbox --disable-gpu --window-size=1920x1080\" testcafe/'
}
}
}
}
stage ('Prod deploy') {
environment {
DOCKERHUB_CREDS = credentials('DOCKERHUB_CREDS')
GITHUB_CLIENT_ID = credentials('PROD_GITHUB_CLIENT_ID')
GITHUB_CLIENT_SECRET = credentials('PROD_GITHUB_CLIENT_SECRET')
GOOGLE_CLIENT_ID = credentials('GOOGLE_CLIENT_ID')
GOOGLE_CLIENT_SECRET = credentials('GOOGLE_CLIENT_SECRET')
POLLING_APP_URL = 'https://pollingapp.jedthompson.co.uk'
}
when {
beforeInput true
branch 'master'
}
input {
message 'Should we deploy to production?'
}
stages {
stage('Tagging and pushing images to jedster1111/pollingapp:release') {
steps {
sh "docker login -u ${DOCKERHUB_CREDS_USR} -p ${DOCKERHUB_CREDS_PSW}"
sh "docker tag ${imageId} jedster1111/pollingapp:release"
sh 'docker push jedster1111/pollingapp:release'
}
}
stage('Stopping and restarting pollingApp container') {
stages {
stage ('Stopping existing pollingapp container') {
when {
not {
expression {
sh(returnStdout: true, script: 'docker container ls --all -f name=pollingapp --format "{{.ID}}"').trim() == ""
}
}
}
steps {
sh 'docker stop pollingapp'
sh 'docker rm pollingapp'
}
}
stage ('Starting up polling app production container') {
steps {
sh 'docker-compose -f /docker-compose/docker-compose.yml up -d'
}
}
}
}
}
}
}
}