-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
149 lines (147 loc) · 6.04 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
updateGitlabCommitStatus state: 'pending'
pipeline {
agent any
options {
gitLabConnection('gitlab-jenkins')
}
post {
failure {
updateGitlabCommitStatus name: 'jenkins', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'jenkins', state: 'success'
}
aborted {
updateGitlabCommitStatus name: 'jenkins', state: 'canceled'
}
}
stages {
stage('Build and Test') {
when { expression { env.gitlabSourceBranch != 'master' } }
steps {
echo 'Jenkins Build and Test'
sh 'cp /var/jenkins_home/.env .env'
sh 'docker build -t djangovue_test --network onepaper_default -f Dockerfile-test .'
}
post {
failure {
updateGitlabCommitStatus name: 'build', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'build', state: 'success'
}
}
}
stage('Deploy to Staging') {
when { expression { env.gitlabSourceBranch != 'master' } }
steps {
echo 'Jenkins Staging'
sh 'docker run -i djangovue_test python manage.py collectstatic --no-input -i admin -i summernote -i debug_toolbar -i rest_framework -i MaterialIcons*;'
sh 'ssh -o StrictHostKeyChecking=no [email protected] "cd /home/ubuntu/onepaper/; \
git checkout .; \
git checkout master; \
git pull --all; \
git checkout -f $gitlabMergeRequestLastCommit; \
git push -f --all old-origin; \
sudo docker-compose -f docker-compose.staging.yml exec -T django pip install -r requirements.txt; \
sudo docker-compose -f docker-compose.staging.yml exec -T django python3 manage.py migrate; \
sudo docker-compose -f docker-compose.staging.yml restart django; \
sudo docker-compose -f docker-compose.staging.yml exec -T nginx-proxy nginx -s reload; \
sudo docker-compose -f docker-compose.staging.yml exec -T nginx-proxy sh -c \'rm -rf /etc/nginx/cache/*\'" '
}
post {
failure {
updateGitlabCommitStatus name: 'deploy', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'deploy', state: 'success'
}
}
}
// Deploy to Production
stage('Deploy to Green') {
when {
expression { env.gitlabSourceBranch == 'master' }
}
steps {
sh 'ssh -o StrictHostKeyChecking=no [email protected] "source djangovenv/bin/activate; \
cd onepaper-green; \
git pull origin master; \
pip install -r requirements.txt --no-warn-script-location; \
python manage.py migrate; \
deactivate; \
sudo systemctl restart onepaper-green; \
sudo systemctl reload nginx; \
sudo rm -rf ~/web-info/cache/nginx-green/*;" '
}
post {
aborted {
updateGitlabCommitStatus name: 'jenkins', state: 'canceled'
}
}
}
stage('Switch Blue to Green and deploy Blue') {
when {
beforeInput true
expression { env.gitlabSourceBranch == 'master' }
}
input {
message "Shall we switch blue to green?"
submitter "admin"
}
steps {
//Switch Blue to green
sh 'ssh -o StrictHostKeyChecking=no [email protected] "source djangovenv/bin/activate; \
cd onepaper; \
git checkout .; \
git pull origin master; \
sh config/nginx/blue-green-deploy.sh g; \
sudo systemctl reload nginx;" '
//Deploy to Blue : Deploy static file.
sh 'ssh -o StrictHostKeyChecking=no [email protected] "cd /home/ubuntu/onepaper/; \
git checkout .; \
git checkout master; \
git pull origin master; \
sudo docker build -t djangovue_deploy --network onepaper_default -f Dockerfile-test-deploy .; \
sudo docker run -i -e GREEN=False djangovue_deploy python manage.py collectstatic --no-input -i admin -i summernote -i debug_toolbar -i rest_framework -i MaterialIcons*;" '
//Deploy to Blue : Deploy django server.
sh 'ssh -o StrictHostKeyChecking=no [email protected] "source djangovenv/bin/activate; \
cd onepaper; \
git checkout .; \
git pull origin master; \
deactivate; \
sudo systemctl restart onepaper; \
sudo systemctl reload nginx; \
sudo rm -rf ~/web-info/cache/nginx/*;" '
}
post {
aborted {
updateGitlabCommitStatus name: 'jenkins', state: 'canceled'
}
}
}
stage('Switch Green to Blue') {
when {
beforeInput true
expression { env.gitlabSourceBranch == 'master' }
}
input {
message "Shall we switch blue to green?"
submitter "admin"
}
steps {
sh 'ssh -o StrictHostKeyChecking=no [email protected] "source djangovenv/bin/activate; \
cd onepaper; \
git checkout .; \
git pull origin master; \
sh config/nginx/blue-green-deploy.sh b; \
sudo systemctl reload nginx;" '
}
post {
aborted {
updateGitlabCommitStatus name: 'jenkins', state: 'canceled'
}
}
}
}
}