forked from cvitter/sample-rest-server
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Jenkinsfile
94 lines (87 loc) · 2.19 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
library 'github.com/cloudy-demos/pipeline-libraries'
pipeline {
agent any
options {
timestamps()
buildDiscarder(logRotator(numToKeepStr:'5')) //delete old builds
ansiColor('xterm')
}
environment {
DOCKERHUB = credentials('dockerhub')
IMAGE_NAME = "$DOCKERHUB_USR/sample-rest-server"
IMAGE_TAG = dockerImageTag()
DOCKER_NETWORK = "cjt-network"
}
stages {
stage('Build') {
steps {
withMaven(mavenOpts: '-Djansi.force=true') {
sh 'mvn clean package -Dstyle.color=always'
}
sh "docker build -t $IMAGE_NAME:$IMAGE_TAG ."
}
}
stage('Test') {
steps {
parallel (
"Sonar Scan" : {
withMaven(publisherStrategy: 'EXPLICIT', mavenOpts: '-Djansi.force=true') {
sh 'mvn sonar:sonar -Dsonar.host.url=http://sonar:9000 -Dstyle.color=always'
}
},
"Functional Test" : {
lock('sample-rest-server') {
//fire up the app
sh """
docker run -d \
--name sample-rest-server \
--network $DOCKER_NETWORK \
-p 4567:4567 \
$IMAGE_NAME:$IMAGE_TAG
"""
//hit the /hello endpoint and collect result
retry(3) {
sleep 2
sh 'curl -v http://sample-rest-server:4567/hello > functionalTest.txt'
}
//store result
archiveArtifacts artifacts: 'functionalTest.txt', fingerprint: true
//stop and remove container
sh """
docker stop sample-rest-server || true
docker rm sample-rest-server || true
"""
}
}
)
}
}
stage('Deploy') {
when {
branch 'master'
}
steps {
parallel (
"Publish Docs" : {
withMaven(publisherStrategy: 'EXPLICIT', mavenOpts: '-Djansi.force=true') {
sh 'mvn site -Dstyle.color=always'
}
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'target/site', reportFiles: 'index.html', reportName: 'API Documentation', reportTitles: ''])
},
"Push Docker Image" : {
sh """
docker login -u $DOCKERHUB_USR -p $DOCKERHUB_PSW
docker push $IMAGE_NAME:$IMAGE_TAG
"""
}
)
}
}
}
post {
always {
//clean up
dockerNuke(IMAGE_NAME, IMAGE_TAG)
}
}
}