-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
210 lines (183 loc) · 7.26 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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'
// Default configuration used for building other than deploy branches.
// - config: An object that lists what values to set in the config/envspecific.js configuration file
def defaultBuildConfig = [
config: [
environment: 'dev',
apiUrl: 'http://localhost:8080',
language: 'en',
],
]
// A map of deployment environments.
// Each element is a map with the following keys:
// - host: The host url in format [email protected]
// - credentials: Name of credentials to use for ssh
// - folder: The folder where the app is located on server
// - siteUrl: The url where the site can be accessed
// - config: The environment specific configuration for the given build
def deployBranches = [
develop: [
host: '[email protected]',
credentials: 'deploy_lutra',
folder: 'ditt_client_dev',
siteUrl: 'http://ditt-client.dev.visionapps.cz',
config: [
environment: 'dev',
apiUrl: 'http://ditt-api.dev.visionapps.cz',
language: 'en',
],
],
master: [
host: '[email protected]',
credentials: 'deploy_lutra',
folder: 'ditt_client_staging',
siteUrl: 'http://ditt-client.staging.visionapps.cz',
config: [
environment: 'production',
apiUrl: 'http://ditt-api.staging.visionapps.cz',
language: 'de',
],
],
]
node {
try {
stage('Checkout code') {
checkout scm
}
stage('Prepare docker containers') {
sh 'docker-compose build node'
}
def commit = sh(
returnStdout: true,
script: 'git rev-parse HEAD 2> /dev/null || exit 0'
).trim()
def lastUpdate = sh(
returnStdout: true,
script: 'date "+%d. %m. %Y %H:%M"'
).trim()
if (deployBranches.containsKey(env.BRANCH_NAME)) {
stage("Build ${env.BRANCH_NAME}") {
sh "sed -i \"s@__ASSET_VERSION__@${commit}@g\" public/index.html"
sh "sed -i \"s@__LAST_UPDATE__@${lastUpdate}@g\" public/index.html"
build(
deployBranches[env.BRANCH_NAME].config.environment,
commit,
deployBranches[env.BRANCH_NAME].config.apiUrl,
deployBranches[env.BRANCH_NAME].config.language,
)
}
stage('Deploy') {
createTar(deployTar)
deploy(deployBranches[env.BRANCH_NAME], targetFolder, deployTar, deployFolder, backupFolder)
checkStatus(deployBranches[env.BRANCH_NAME].siteUrl, 200)
notifyOfDeploy(deployBranches, env.BRANCH_NAME)
}
}
else {
stage("Build ${env.BRANCH_NAME}") {
build(
defaultBuildConfig.config.environment,
commit,
defaultBuildConfig.config.apiUrl,
defaultBuildConfig.config.language,
)
}
}
} catch (err) {
currentBuild.result = 'FAILURE'
echo "DEPLOY 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 client: Build větve `${env.BRANCH_NAME}` selhal :thunder_cloud_and_rain:')
}
} finally {
stage('Cleanup') {
sh 'docker-compose stop'
sh 'docker-compose rm --all --force'
}
}
}
def build(envName, commit, apiUrl, language) {
sh 'rm -rf node_modules'
sh 'rm -rf public/generated/bundle.js'
sh 'rm -f config/envspecific.js'
sh 'cp config/envspecific.example.js config/envspecific.js'
for (confOption in [
[orig: "export const ENVIRONMENT = 'dev';", new: "export const ENVIRONMENT = '${envName}';"],
[orig: "export const COMMIT = 'commithash';", new: "export const COMMIT = '${commit}';"],
[orig: "export const API_URL = 'http://localhost';", new: "export const API_URL = '${apiUrl}';"],
[orig: "export const LANGUAGE = 'en';", new: "export const LANGUAGE = '${language}';"],
]) {
sh "sed -i \"s@${confOption.orig}@${confOption.new}@g\" config/envspecific.js"
}
sh 'docker-compose run node bash -c "sh /root/init-container.sh /workspace && su docker-container-user ./build.sh"'
}
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 .htaccess ${deployFolder}/.htaccess 2>/dev/null || :
cp .htpasswd ${deployFolder}/.htpasswd 2>/dev/null || :
echo 'Moving deploy folder to targetFolder'
rm -rf ${targetFolder}/${deployBranch.folder}.deploy
mv ${targetFolder}/${deployBranch.folder}/${deployFolder} ${targetFolder}/${deployBranch.folder}.deploy
"""
sh "scp public/${deployTar} ${deployBranch.host}:${targetFolder}/${deployBranch.folder}.deploy"
sh """ssh ${deployBranch.host} /bin/bash << EOF
set -e
cd ${targetFolder}/${deployBranch.folder}.deploy
echo 'Finalizing deploy'
tar -mxzf ${deployTar}
echo 'Backing up old deploy'
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 createTar(deployTar) {
dir('public') {
sh "rm -f ${deployTar}"
sh "tar -zcf ${deployTar} *"
}
}
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
"""
}
def notifyOfDeploy(deployBranches, currentBranch) {
echo 'DEPLOY SUCCESSFUL'
slackSend(
color: 'good',
message: "DITT client: Větev `${currentBranch}` byla nasazena na: ${deployBranches[currentBranch].siteUrl} :sunny:"
)
}