forked from DBJRdev/ditt-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
175 lines (148 loc) · 6 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
// Folder on server where the backup of the old site saved
def backupFolder = '~/deploy-backups'
// Name of the folder where the new application is being prepared.
// Must not exist in the application that is being deployed.
def deployFolder = "deployment"
// Name of the archive that contains the new application.
// Must not exist in the application that is being deployed.
def deployTar = 'deployment.tar.gz'
// Folder on server where the applications exist
def targetFolder = '~/webapps-data'
// A map of deployment environments.
// Each element is a map with the following keys:
// - host: The ssh host url in format [email protected]
// - folder: The folder where the app is located on server
// - dbName: Name of the database to back up
// - siteUrl: Url where the deployed site is accessible
def deployBranches = [
develop: [
host: '[email protected]',
folder: 'ditt_api_dev',
dbName: 'ditt_api_dev',
siteUrl: 'http://ditt-api.dev.visionapps.cz',
credentials: 'deploy_lutra',
],
master: [
host: '[email protected]',
folder: 'ditt_api_staging',
dbName: 'ditt_api_staging',
siteUrl: 'http://ditt-api.staging.visionapps.cz',
credentials: 'deploy_lutra',
],
]
node {
try {
stage('Checkout code') {
timeout(1) {
checkout scm
}
}
stage('Prepare docker containers') {
timeout(20) {
sh 'docker-compose -f docker-compose-build.yml up -d'
}
}
stage('Build app and run tests') {
timeout(20) {
sh 'docker-compose -f docker-compose-build.yml run --no-deps web bash -c "sh /root/init-container.sh /www && su www-data -c \'composer install && vendor/bin/robo install\'"'
}
}
if (deployBranches.containsKey(env.BRANCH_NAME)) {
stage('Deploy') {
timeout(5) {
createDeployTar(deployTar)
deploy(deployBranches[env.BRANCH_NAME], targetFolder, deployTar, deployFolder, backupFolder)
checkStatus(deployBranches[env.BRANCH_NAME].siteUrl, 200)
notifyOfDeploy(deployBranches, env.BRANCH_NAME)
}
}
}
} catch (err) {
currentBuild.result = 'FAILURE'
echo "BUILD ERROR: ${err.toString()}"
emailext (
recipientProviders: [[$class: 'DevelopersRecipientProvider']],
subject: "Build ${env.JOB_NAME} [${env.BUILD_NUMBER}] failed",
body: err.toString(),
attachLog: true,
)
if (deployBranches.containsKey(env.BRANCH_NAME)) {
slackSend(color: 'danger', message: "DITT API: Build a nasazení větve `${env.BRANCH_NAME}` selhaly :thunder_cloud_and_rain:")
}
} finally {
stage('Cleanup') {
timeout(5) {
sh 'docker-compose -f docker-compose-build.yml stop'
sh 'docker-compose -f docker-compose-build.yml rm --all --force'
}
}
}
}
def deploy(deployBranch, targetFolder, deployTar, deployFolder, backupFolder) {
sshagent (credentials: [deployBranch.credentials]) {
sh """ssh ${deployBranch.host} /bin/bash << EOF
set -e
cd ${targetFolder}/${deployBranch.folder}
echo 'Creating deploy folder'
rm -rf ${deployFolder}
mkdir ${deployFolder}
cp .env ${deployFolder}/.env
mkdir --parents ${deployFolder}/var
cp -r var/log ${deployFolder}/var/log
cp php.ini ${deployFolder}/php.ini
cp .htaccess ${deployFolder}/.htaccess
cp .htpasswd ${deployFolder}/.htpasswd 2>/dev/null || :
mkdir ${deployFolder}/config
cp -r config/jwt ${deployFolder}/config/jwt
echo 'Moving deploy folder to targetFolder'
rm -rf ${targetFolder}/${deployBranch.folder}.deploy
mv ${targetFolder}/${deployBranch.folder}/${deployFolder} ${targetFolder}/${deployBranch.folder}.deploy
"""
sh "scp ${deployTar} ${deployBranch.host}:${targetFolder}/${deployBranch.folder}.deploy"
sh """ssh ${deployBranch.host} /bin/bash << EOF
set -e
cd ${targetFolder}/${deployBranch.folder}.deploy
echo 'Extracting deployTar'
tar -mxzf ${deployTar}
chmod 744 bin/console
echo 'Running post upload Robo tasks'
vendor/bin/robo deploy:finalize /usr/local/bin/php71
echo 'Backing up old deploy'
cp public/static/503.php ${targetFolder}/${deployBranch.folder}/public/index.php
pg_dump ${deployBranch.dbName} > ${targetFolder}/${deployBranch.folder}/db_dump.sql
rm -rf ${backupFolder}/${deployBranch.folder}
mv ${targetFolder}/${deployBranch.folder} ${backupFolder}/${deployBranch.folder}
echo 'Switching to new deploy'
mv ${targetFolder}/${deployBranch.folder}.deploy ${targetFolder}/${deployBranch.folder}
rm -f ${targetFolder}/${deployBranch.folder}/${deployTar}
"""
}
}
def createDeployTar(deployTar) {
sh """tar \
-zcf ${deployTar} \
--exclude=var/log/* \
--exclude=var/cache/* \
--exclude=var/sessions/* \
bin config public src templates var vendor composer.json RoboFile.php"""
}
def notifyOfDeploy(deployBranches, currentBranch) {
echo 'DEPLOY SUCCESSFUL'
slackSend(
color: 'good',
message: "DITT API: Větev `${currentBranch}` byla nasazena na: ${deployBranches[currentBranch].siteUrl} :sunny:"
)
}
def checkStatus(url, code) {
echo 'Checking website status'
sh """
httpCode=\$(curl -sL --connect-timeout 50 -w "%{http_code}\\n" $url -o /dev/null)
if [ \$httpCode -eq $code ]; then
echo 'Website status check passed.'
exit 0
else
echo 'Website status check failed.'
exit 1
fi
"""
}