-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJenkinsfile
More file actions
336 lines (324 loc) · 15.1 KB
/
Copy pathJenkinsfile
File metadata and controls
336 lines (324 loc) · 15.1 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
pipeline {
options {
timeout(time: 1, unit: 'HOURS')
}
agent none
environment {
registry = 'bitflowstream/bitflow-pipeline'
registryCredential = 'dockerhub'
image = "" // Define global variables for sharing between stages
staticImage = ""
image_arm32v7 = ""
staticImage_arm32v7 = ""
image_arm64v8 = ""
staticImage_arm64v8 = ""
}
stages {
stage('Git') {
agent {
docker {
image 'bitflowstream/golang-build:debian'
}
}
steps {
script {
env.GIT_COMMITTER_EMAIL = sh(script: "git --no-pager show -s --format='%ae'", returnStdout: true).trim()
}
}
}
stage('Unit tests') {
agent {
docker {
image 'bitflowstream/golang-build:debian'
args '-v /tmp/go-mod-cache/debian:/go'
}
}
steps {
sh 'go clean -i -v ./...'
sh 'go install -v ./...'
sh 'rm -rf reports && mkdir -p reports'
sh 'stdbuf -o 0 go test -v ./... -coverprofile=reports/test-coverage.txt | tee reports/test-output.txt' // Use stdbuf to disable buffering of the tee output
sh 'cat reports/test-output.txt | go-junit-report -set-exit-code > reports/test.xml'
sh 'go vet ./... &> reports/vet.txt'
sh 'golint $(go list -f "{{.Dir}}" ./...) &> reports/lint.txt'
stash includes: 'reports/*', name: 'unitStage'
}
post {
always {
archiveArtifacts 'reports/*'
junit 'reports/test.xml'
}
}
}
stage('Linting & static analyis') {
parallel {
stage('SonarQube') {
agent {
docker {
image 'bitflowstream/golang-build:debian'
args '-v /tmp/go-mod-cache/debian:/go'
}
}
steps {
dir('reports') {
unstash 'unitStage'
}
script {
def scannerHome = tool 'sonar-scanner-linux'
withSonarQubeEnv('CIT SonarQube') {
sh """
${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=go-bitflow -Dsonar.branch.name=$BRANCH_NAME \
-Dsonar.sources=. -Dsonar.tests=. \
-Dsonar.inclusions="**/*.go" -Dsonar.test.inclusions="**/*_test.go" \
-Dsonar.exclusions="script/script/internal/*.go" \
-Dsonar.go.golint.reportPath=reports/lint.txt \
-Dsonar.go.govet.reportPaths=reports/vet.txt \
-Dsonar.go.coverage.reportPaths=reports/test-coverage.txt \
-Dsonar.test.reportPath=reports/test.xml
"""
}
}
timeout(time: 10, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage ("Lint Dockerfiles") {
agent {
docker {
image 'hadolint/hadolint:latest-debian'
}
}
steps {
sh 'hadolint --format checkstyle build/*.Dockerfile | tee -a hadolint.xml'
}
post {
always {
archiveArtifacts 'hadolint.xml'
}
}
}
}
}
stage('Build docker images') {
parallel {
stage('amd64') {
agent {
docker {
image 'bitflowstream/golang-build:alpine'
args '-v /tmp/go-mod-cache/alpine:/go -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh './build/native-build.sh'
script {
image = docker.build registry + ":$BRANCH_NAME-build-$BUILD_NUMBER", '-f build/alpine-prebuilt.Dockerfile build/_output'
}
sh "./build/test-image.sh $BRANCH_NAME-build-$BUILD_NUMBER"
sh './build/native-static-build.sh'
script {
staticImage = docker.build registry + ":static-$BRANCH_NAME-build-$BUILD_NUMBER", '-f build/static-prebuilt.Dockerfile build/_output/static'
}
sh "./build/test-image.sh static-$BRANCH_NAME-build-$BUILD_NUMBER"
}
post {
always {
sh 'mv build/_output/bitflow-pipeline build/_output/bitflow-pipeline-amd64'
archiveArtifacts 'build/_output/bitflow-pipeline-amd64'
}
success {
script {
if (env.BRANCH_NAME == 'master') {
docker.withRegistry('', registryCredential) {
image.push("build-$BUILD_NUMBER")
image.push("latest-amd64")
staticImage.push("static-build-$BUILD_NUMBER")
staticImage.push("static")
}
}
}
}
}
}
stage('arm32v7') {
agent {
docker {
image 'bitflowstream/golang-build:arm32v7'
args '-v /tmp/go-mod-cache/alpine:/go -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh './build/native-build.sh'
script {
image_arm32v7 = docker.build registry + ":$BRANCH_NAME-build-$BUILD_NUMBER-arm32v7", '-f build/arm32v7-prebuilt.Dockerfile build/_output'
}
sh "./build/test-image.sh $BRANCH_NAME-build-$BUILD_NUMBER-arm32v7"
}
post {
always {
sh 'mv build/_output/bitflow-pipeline build/_output/bitflow-pipeline-arm32v7'
archiveArtifacts 'build/_output/bitflow-pipeline-arm32v7'
}
success {
script {
if (env.BRANCH_NAME == 'master') {
docker.withRegistry('', registryCredential) {
image_arm32v7.push("build-$BUILD_NUMBER-arm32v7")
image_arm32v7.push("latest-arm32v7")
}
}
}
}
}
}
stage('arm32v7 static') {
agent {
docker {
image 'bitflowstream/golang-build:static-arm32v7'
args '-v /tmp/go-mod-cache/debian:/go -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh './build/native-static-build.sh'
script {
staticImage_arm32v7 = docker.build registry + ":static-$BRANCH_NAME-build-$BUILD_NUMBER-arm32v7", '-f build/static-prebuilt.Dockerfile build/_output/static'
}
sh "./build/test-image.sh static-$BRANCH_NAME-build-$BUILD_NUMBER-arm32v7"
}
post {
always {
sh 'mv build/_output/static/bitflow-pipeline build/_output/static/bitflow-pipeline-arm32v7'
archiveArtifacts 'build/_output/static/bitflow-pipeline-arm32v7'
}
success {
script {
if (env.BRANCH_NAME == 'master') {
docker.withRegistry('', registryCredential) {
staticImage_arm32v7.push("static-build-$BUILD_NUMBER-arm32v7")
staticImage_arm32v7.push("static-arm32v7")
}
}
}
}
}
}
stage('arm64v8') {
agent {
docker {
image 'bitflowstream/golang-build:arm64v8'
args '-v /tmp/go-mod-cache/alpine:/go -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh './build/native-build.sh'
script {
image_arm64v8 = docker.build registry + ":$BRANCH_NAME-build-$BUILD_NUMBER-arm64v8", '-f build/arm64v8-prebuilt.Dockerfile build/_output'
}
sh "./build/test-image.sh $BRANCH_NAME-build-$BUILD_NUMBER-arm64v8"
}
post {
always {
sh 'mv build/_output/bitflow-pipeline build/_output/bitflow-pipeline-arm64v8'
archiveArtifacts 'build/_output/bitflow-pipeline-arm64v8'
}
success {
script {
if (env.BRANCH_NAME == 'master') {
docker.withRegistry('', registryCredential) {
image_arm64v8.push("build-$BUILD_NUMBER-arm64v8")
image_arm64v8.push("latest-arm64v8")
}
}
}
}
}
}
stage('arm64v8 static') {
agent {
docker {
image 'bitflowstream/golang-build:static-arm64v8'
args '-v /tmp/go-mod-cache/debian:/go -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh './build/native-static-build.sh'
script {
staticImage_arm64v8 = docker.build registry + ":static-$BRANCH_NAME-build-$BUILD_NUMBER-arm64v8", '-f build/static-prebuilt.Dockerfile build/_output/static'
}
sh "./build/test-image.sh static-$BRANCH_NAME-build-$BUILD_NUMBER-arm64v8"
}
post {
always {
sh 'mv build/_output/static/bitflow-pipeline build/_output/static/bitflow-pipeline-arm64v8'
archiveArtifacts 'build/_output/static/bitflow-pipeline-arm64v8'
}
success {
script {
if (env.BRANCH_NAME == 'master') {
docker.withRegistry('', registryCredential) {
staticImage_arm64v8.push("static-build-$BUILD_NUMBER-arm64v8")
staticImage_arm64v8.push("static-arm64v8")
}
}
}
}
}
}
}
}
stage('Docker manifest') {
when {
branch 'master'
}
agent {
docker {
image 'bitflowstream/golang-build:debian'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',
credentialsId: 'dockerhub', usernameVariable: 'DOCKERUSER', passwordVariable: 'DOCKERPASS'
]]) {
// Dockerhub Login
sh 'echo "$DOCKERPASS" | docker login -u "$DOCKERUSER" --password-stdin'
sh "docker manifest create ${registry}:latest ${registry}:latest-amd64 ${registry}:latest-arm32v7 ${registry}:latest-arm64v8"
sh "docker manifest annotate ${registry}:latest ${registry}:latest-arm32v7 --os=linux --arch=arm --variant=v7"
sh "docker manifest annotate ${registry}:latest ${registry}:latest-arm64v8 --os=linux --arch=arm64 --variant=v8"
sh "docker manifest push --purge ${registry}:latest"
}
}
}
}
post {
success {
node('master') {
withSonarQubeEnv('CIT SonarQube') {
slackSend channel: '#jenkins-builds-all', color: 'good',
message: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} was successful (<${env.BUILD_URL}|Open Jenkins>) (<${env.SONAR_HOST_URL}|Open SonarQube>)"
}
}
}
failure {
node('master') {
slackSend channel: '#jenkins-builds-all', color: 'danger',
message: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} failed (<${env.BUILD_URL}|Open Jenkins>)"
}
}
fixed {
node('master') {
withSonarQubeEnv('CIT SonarQube') {
slackSend channel: '#jenkins-builds', color: 'good',
message: "Thanks to ${env.GIT_COMMITTER_EMAIL}, build ${env.JOB_NAME} ${env.BUILD_NUMBER} was successful (<${env.BUILD_URL}|Open Jenkins>) (<${env.SONAR_HOST_URL}|Open SonarQube>)"
}
}
}
regression {
node('master') {
slackSend channel: '#jenkins-builds', color: 'danger',
message: "What have you done ${env.GIT_COMMITTER_EMAIL}? Build ${env.JOB_NAME} ${env.BUILD_NUMBER} failed (<${env.BUILD_URL}|Open Jenkins>)"
}
}
}
}