-
Notifications
You must be signed in to change notification settings - Fork 5
/
Jenkinsfile
168 lines (150 loc) · 4.9 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
#!/usr/bin/env groovy
// Global Environment variables
FAILURE_EMAIL = "[email protected]"
DESIRED_REPOSITORY = "https://github.com/GeographicaGS/UrboCore-api.git"
pipeline{
agent { node {
label 'master'
} }
stages {
stage('Preparing for build') {
// agent{ docker{
// image 'debian'
// } }
agent { node {
label 'master'
} }
steps {
prepareBuild()
}
}
stage ('Building') {
agent { node {
label 'docker'
} }
steps {
sh "docker build --pull=true -f Dockerfile.new -t geographica/urbocore_api:${git_commit} ."
}
}
stage ('Linter') {
agent { node {
label 'docker'
} }
steps {
sh "docker run -i --rm --name urbocore_api--${build_name} -e 'NODE_ENV=development' geographica/urbocore_api:${git_commit} npm run lint"
}
}
stage('Confirm deployment') {
agent { node {
label 'master'
} }
when { anyOf {
branch 'master';
branch 'staging';
branch 'dev';
} }
steps {
script {
env.DEPLOY_TYPE = "ansible"
if ( env.BRANCH_NAME == "master" ) {
env.DEPLOY_TO = "production"
} else {
env.DEPLOY_TO = "${env.BRANCH_NAME}"
}
}
// // Ask for confirmation
// script {
// if ( env.DEPLOY_TO == "prod" ) {
// timeout(time:2, unit:'MINUTES') {
// // https://jenkins.io/doc/pipeline/steps/pipeline-input-step/
// input message: "Are you sure you want to deploy '${env.BRANCH_NAME}' of repository '${env.BUILD_URL}' to '${env.DEPLOY_URL}'?"
// }
// }
// }
}
}
stage ('Deploying') {
agent { node {
label 'docker'
} }
when { expression {
env.DEPLOY_TYPE != null
} }
steps{
script {
echo "Deploy type: ${env.DEPLOY_TYPE}"
echo "Deploy to: ${env.DEPLOY_TO}"
// Rebuilding the image in order to set the API in production mode
if (env.DEPLOY_TO == "production" ) {
sh "docker build --pull=true --build-arg NODE_ENV=production -f Dockerfile.new -t geographica/urbocore_api:${git_commit} ."
}
withCredentials([[$class: 'UsernamePasswordMultiBinding',credentialsId: 'dockerhub',usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh "docker login -u ${ USERNAME } -p ${ PASSWORD }"
sh "docker tag geographica/urbocore_api:${ git_commit } geographica/urbocore_api:${ env.DEPLOY_TO }"
sh "docker tag geographica/urbocore_api:${ git_commit } geographica/urbocore_api:${ env.DEPLOY_TO }-${ build_name }"
sh "docker push geographica/urbocore_api:${ env.DEPLOY_TO }"
sh "docker push geographica/urbocore_api:${ env.DEPLOY_TO }-${ build_name }"
}
if (env.DEPLOY_TYPE == "ansible") {
sh "ansible urbo-${ env.DEPLOY_TO } -a 'sh /data/app/urbo/urbocore-api/deploy.sh'"
} else {
error("Unknown DEPLOY_TYPE: '${env.DEPLOY_TYPE}'")
}
}
}
}
}
post {
always {
deleteDir() /* clean up our workspace */
}
unstable {
notifyStatus(currentBuild.currentResult)
}
failure {
notifyStatus(currentBuild.currentResult)
}
}
}
def prepareBuild() {
script {
checkout scm
sh "git rev-parse --short HEAD > .git/git_commit"
sh "git --no-pager show -s --format='%ae' HEAD > .git/git_committer_email"
workspace = pwd()
branch_name = "${ env.BRANCH_NAME }".replaceAll("/", "_")
git_commit = readFile(".git/git_commit").replaceAll("\n", "").replaceAll("\r", "")
build_name = "${git_commit}"
job_name = "${ env.JOB_NAME }".replaceAll("%2F", "/")
committer_email = readFile(".git/git_committer_email").replaceAll("\n", "").replaceAll("\r", "")
GIT_URL = sh(returnStdout: true, script: "git config --get remote.origin.url").trim()
if ( GIT_URL != DESIRED_REPOSITORY ) {
error("This jenkinsfile is configured for '${ DESIRED_REPOSITORY }' but it was executed from '${ GIT_URL }'.")
}
}
}
def notifyStatus(buildStatus) {
def status
def send_to
try {
switch (branch_name) {
case 'master':
send_to = "${ committer_email }, ${ FAILURE_EMAIL }"
break
default:
send_to = "${ committer_email }"
break
}
} catch(Exception ex) {
send_to = "${ FAILURE_EMAIL }"
}
echo "Sending error email to: ${ send_to }"
try {
mail to: "${ send_to }",
from: "Jenkins Geographica <[email protected]>",
subject: "[${ buildStatus }] ${currentBuild.fullDisplayName}",
body: "Something is wrong in '${currentBuild.fullDisplayName}'. \n\nSee ${env.BUILD_URL} for more details."
} catch(Exception ex) {
echo "Something went wrong while sending an error email :("
}
}