-
Notifications
You must be signed in to change notification settings - Fork 124
/
Jenkinsfile-oss
174 lines (148 loc) · 4.61 KB
/
Jenkinsfile-oss
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
// Library this Jenkinsfile uses - configured in Manage Jenkins/Configure System
library 'CraigsLibs'
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr:'10')) // Keep only the 10 most recent builds
}
environment {
//SONAR = credentials('sonar') // Sonar Credentials
SONAR_SERVER = "http://sonar.beedemo.net:9000" // Sonar Server Address
DOCKERHUB = credentials('dockerhub') // Docker Hub Credentials
DOCKERHUB_REPO = "craigcloudbees" // Repo on Docker Hub to push our image to
APP_VERSION = "0.0.1" // Version of the app, used to tag the Docker image
DOCKER_IMG_NAME = "sample-rest-server" // Name of our Docker image
CONTAINER_ADDRESS = "localhost" // Address at which running container can be reached
// for http based testing ex: http://localhost:4567/hello
}
stages {
// Extract the application version number from the pom.xml file
stage('Parse POM') {
steps {
script {
pom = readMavenPom file: 'pom.xml'
APP_VERSION = pom.version
}
}
}
// Use CraigsLibs.githubChangeset() to get recent commits
stage('Github Changeset') {
steps {
echo githubChangeset()
}
}
// Maven Buld Steps
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
}
}
// Run Tests in Parallel
stage('Quality Analysis') {
steps {
parallel (
"JUnit Tests" : {
sh 'mvn test'
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/TEST-*.xml'
},
"Findbugs Tests" : {
sh 'mvn findbugs:findbugs'
echo 'Run integration tests here...'
},
"UX Testing" : {
echo 'Run UX tests here...'
},
"Sonar Scan" : {
//sh "mvn sonar:sonar -Dsonar.host.url=$SONAR_SERVER -Dsonar.organization=$SONAR_USR -Dsonar.login=$SONAR_PSW"
echo 'Skipping sonar run...'
}, failFast: true
)
}
}
stage('Create Docker Image') {
steps {
// First make sure that a version of this image isn't already running
sh 'docker stop $(docker ps -q --filter ancestor="${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}:${APP_VERSION}") || true'
// Then delete any images that already exist for this version of the API
sh 'docker images | grep "${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}" | xargs docker rmi -f || true'
// Now build a new docker image for this version of the API
sh "docker build -t ${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}:${APP_VERSION} ./"
}
}
stage('Run Docker Image') {
steps {
// Start the new image we just created
sh 'docker run -d -p 4567:4567 ${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}:${APP_VERSION}'
}
}
stage('Test Docker Image') {
steps {
echo 'Docker tests commented out for now'
//retry (10) {
// Use httpRequest to check default API endpoint, will throw an error if the endpoint
// isn't accessible at the address specified, retry utilized here to give the container
// time to start
// script {
// env.RESULT = httpRequest "http://${CONTAINER_ADDRESS}:4567/hello"
//
// Write the test results to a file we can archive
// writeFile file: "target/restApiTests.txt", text: "${RESULT}"
// }
//}
}
}
// Pushes the Docker image to Docker Hub - Master only
stage('Push Docker Image') {
when {
branch 'master'
}
steps {
sh """
docker login -u $DOCKERHUB_USR -p $DOCKERHUB_PSW
docker push ${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}:${APP_VERSION}
"""
}
}
stage('Create Site') {
when {
branch 'master'
}
steps {
sh 'mvn site:site'
}
}
stage('Archive Artifacts') {
when {
branch 'master'
}
steps {
// Archive the jar files created
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
// Zip up the site directory and archive it
//sh 'zip -r target/site.zip target/site'
//archiveArtifacts artifacts: '**/target/*.zip', fingerprint: true
// Archive API test results
//archiveArtifacts artifacts: '**/target/*.txt', fingerprint: true
}
}
stage('Debug Output') {
when {
branch 'development'
}
steps {
sh 'pwd'
sh 'ls -l'
sh 'ls -l target'
}
}
}
post {
// Clean up our environment
always {
// Stop the docker container if started
sh 'docker stop $(docker ps -q --filter ancestor="${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}:${APP_VERSION}") || true'
// Delete the docker image created
sh 'docker images | grep "${DOCKERHUB_REPO}/${DOCKER_IMG_NAME}" | xargs docker rmi -f || true'
}
}
}