-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathJenkinsfile
206 lines (180 loc) · 5.51 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
pipeline {
agent {
label 'docker'
}
stages {
stage('Setup') {
steps {
script {
if (env.GIT_BRANCH == 'origin/develop' || env.GIT_BRANCH ==~ /(.+)feature-(.+)/) {
target = 'dev'
} else if (env.GIT_BRANCH ==~ /(.+)release-(.+)/) {
target = 'pre'
} else if (env.GIT_BRANCH == 'origin/master') {
target = 'pro'
} else {
error "Unknown branch type: ${env.GIT_BRANCH}"
}
appversion = sh(script: 'echo $(cat src/package.json | grep version | cut -d\' \' -f4 | sed \'s|[",]||g\')', returnStdout: true).trim()
version = appversion.take(10) + '-' + env.BUILD_NUMBER
appname = env.JOB_NAME
prjname = 'oss-opensource'
packname = appname + '-v' + version + '.tar.gz'
publish_url = env.NEXUS_URL + '/repository/raw-nodejs/' + prjname + '/' + packname
}
}
}
stage('APP Build') {
steps {
sh 'node --version'
sh 'npm --version'
sh 'cd src && npm install'
}
}
stage('APP Quality'){
steps {
withEnv(["PATH+SONAR=${tool 'sonarqube-scanner-v3.0.3'}/bin"]) {
withCredentials([usernamePassword(credentialsId: 'sonarqube-credential', passwordVariable: 'SONARQUBE_PASSWORD', usernameVariable: 'SONARQUBE_USERNAME')]) {
sh """sonar-scanner \
-Dsonar.sources=src \
-Dsonar.projectKey=${env.JOB_NAME} \
-Dsonar.projectName=${env.JOB_NAME} \
-Dsonar.projectBaseDir=${env.WORKSPACE} \
-Dsonar.login=$SONARQUBE_PASSWORD \
-Dsonar.host.url=${env.SONARQUBE_URL} \
-Dsonar.exclusions=src/test/**,src/node_modules/**,**/migrations/**,**/libs/transactional_db/** \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.language=js \
-Dsonar.projectDescription='OSS OpenSource - API Gateway' \
-Dsonar.projectVersion=${version}
"""
}
}
}
}
stage('APP Unit Test') {
steps {
sh 'cd src && npm test'
junit 'src/mocha-report.xml'
}
}
stage('APP Package'){
steps {
sh "tar --exclude='./.git' --exclude='./Jenkinsfile' --exclude='./Dockerfile' -czv src/ -f " + packname
}
}
stage('APP Publish') {
steps {
withCredentials([usernamePassword(credentialsId: 'nexus-credential', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USERNAME')]) {
sh 'curl -v -u $NEXUS_USERNAME:$NEXUS_PASSWORD --upload-file ' + packname + ' ' + publish_url
}
}
}
stage('Docker Build') {
steps {
script {
docker.build(appname, "-f src/Dockerfile src")
}
}
}
stage('Docker Publish') {
steps {
script {
docker.withRegistry(env.REGISTRY_URL) {
docker.image(appname).push(version)
docker.image(appname).push("latest")
}
}
}
}
stage('DEV Deploy') {
steps {
sh 'echo Deploy to DEV . . .'
}
}
stage('PRE Deploy') {
when {
expression { return target == 'pre' || target == 'pro' }
}
steps {
sh 'echo Deploy to PRE . . .'
}
}
stage('UAT Test') {
when {
expression { target == 'pro' }
}
steps {
sh 'echo UAT Test . . .'
}
}
stage('Approval') {
when {
expression { target == 'pro' }
}
steps {
timeout(time:30, unit:'MINUTES') {
input message: "Deploy to Production?", id: "approval"
}
}
}
stage('PRO Deploy') {
when {
expression { return target == 'pro' }
}
steps {
sh 'echo Deploy to PRO . . .'
}
}
}
post {
success {
script {
currentBuild.result = "SUCCESS"
/* Custom data map for InfluxDB */
def custom = [:]
custom['branch'] = env.GIT_BRANCH
custom['environment'] = target
custom['part'] = 'jenkins'
custom['version'] = version
step([$class: 'InfluxDbPublisher', customData: custom, target: 'devops-kpi'])
}
}
failure {
script {
currentBuild.result = "FAILURE"
/* Custom data map for InfluxDB */
def custom = [:]
custom['branch'] = env.GIT_BRANCH
custom['environment'] = target
custom['part'] = 'jenkins'
custom['version'] = version
step([$class: 'InfluxDbPublisher', customData: custom, target: 'devops-kpi'])
}
}
unstable {
script {
currentBuild.result = "FAILURE"
/* Custom data map for InfluxDB */
def custom = [:]
custom['branch'] = env.GIT_BRANCH
custom['environment'] = target
custom['part'] = 'jenkins'
custom['version'] = version
step([$class: 'InfluxDbPublisher', customData: custom, target: 'devops-kpi'])
}
}
aborted {
script {
currentBuild.result = "FAILURE"
/* Custom data map for InfluxDB */
def custom = [:]
custom['branch'] = env.GIT_BRANCH
custom['environment'] = target
custom['part'] = 'jenkins'
custom['version'] = version
step([$class: 'InfluxDbPublisher', customData: custom, target: 'devops-kpi'])
}
}
}
}