forked from abdelsfane/spring-music-app
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Jenkinsfile
153 lines (141 loc) · 5.38 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
#!/usr/bin/env groovy
node {
//Delete current directory
deleteDir()
// Using BuildUser Plugin
wrap([$class: 'BuildUser']) {
// Checkout our source code from Github
checkout scm
// ------------------------------- Define Variables ------------------------------------------------
SPRING_APP = "spring-music-app"
APPLICATION_NAME = "${BUILD_USER_ID}-${SPRING_APP}"
DEPLOY_SPACE = "Development"
PCF_ORG = "YOUR_PCF_ORG_NAME"
ARTIFACT_URL = "http://18.216.57.173:8081/artifactory/sample-test/"
SONARQUBE_ENDPOINT = "http://18.188.152.100:9000"
PCF_ENDPOINT = "https://api.run.pivotal.io"
SLEEP_SECONDS = 5
// ------------------------------- Use Jenkins Credential Store ------------------------------------------------
withCredentials([
[
$class : 'StringBinding',
credentialsId : 'sonarqube',
variable : 'SONARQUBE_TOKEN'
],
[
$class : 'UsernamePasswordMultiBinding',
credentialsId : 'abdel_pcf_user',
passwordVariable: 'PCF_PASSWORD',
usernameVariable: 'PCF_USERNAME'
],[
$class : 'UsernamePasswordMultiBinding',
credentialsId : 'abdel_art_user',
passwordVariable: 'ART_PASSWORD',
usernameVariable: 'ART_USERNAME'
]]){
// ------------------------------- Spin Up Docker Container ------------------------------------------------
docker.image('maven:3-ibmjava-8-alpine').inside(){
withEnv(['HOME=.']) {
env.APPLICATION_NAME = APPLICATION_NAME
env.PCF_ENDPOINT = PCF_ENDPOINT
env.DEPLOY_SPACE = DEPLOY_SPACE
env.PCF_ORG = PCF_ORG
env.SPRING_APP = SPRING_APP
env.SONARQUBE_ENDPOINT = SONARQUBE_ENDPOINT
env.ARTIFACT_URL = ARTIFACT_URL
env.PCF_USERNAME = PCF_USERNAME
env.PCF_PASSWORD = PCF_PASSWORD
env.ART_USERNAME = ART_USERNAME
env.ART_PASSWORD = ART_PASSWORD
env.SONARQUBE_TOKEN = SONARQUBE_TOKEN
// ------------------------------- Run Jenkins Stages (Steps) ------------------------------------------------
// Download our Spring Application Artifacts from Artifactory
stage("Pull Spring Music Artifacts") {
sh '''
curl -u${ART_USERNAME}:${ART_PASSWORD} -O "${ARTIFACT_URL}${SPRING_APP}.zip"
unzip ${SPRING_APP}.zip
'''
}
// Run SonarQube Code Quality and Security Scan
stage('SonarQube analysis') {
withSonarQubeEnv() {
sh '''
cd ${SPRING_APP}
./gradlew sonarqube \
-Dsonar.projectName=${APPLICATION_NAME} \
-Dsonar.projectKey=${APPLICATION_NAME} \
-Dsonar.host.url=${SONARQUBE_ENDPOINT} \
-Dsonar.login=${SONARQUBE_TOKEN}
'''
}
}
// Build & Test our spring application using Gradle Build Automation
stage("Clean & Build") {
sh '''
cd ~/$PROJECT_NAME/${SPRING_APP}
./gradlew clean build
'''
}
// Upload our application jar file to Artifactory
stage("Upload to Artifactory") {
sh '''
cd ~/$PROJECT_NAME/${SPRING_APP}/build/libs
curl -u${ART_USERNAME}:${ART_PASSWORD} -T spring-music-1.0.${BUILD_NUMBER}.jar "${ARTIFACT_URL}${APPLICATION_NAME}_${BUILD_NUMBER}.jar"
'''
}
}
}
// Deploy our application to Pivotal Web Services
docker.image('pcvolkmer/cloudfoundry-cli').inside(){
withEnv(['HOME=.']) {
env.APPLICATION_NAME = APPLICATION_NAME
env.PCF_ENDPOINT = PCF_ENDPOINT
env.DEPLOY_SPACE = DEPLOY_SPACE
env.PCF_ORG = PCF_ORG
env.SPRING_APP = SPRING_APP
env.PCF_USERNAME = PCF_USERNAME
env.PCF_PASSWORD = PCF_PASSWORD
env.SLEEP_SECONDS = SLEEP_SECONDS
stage("Deploy to PCF ${DEPLOY_SPACE}") {
sh '''
cd ~/$PROJECT_NAME/${SPRING_APP}/build/libs
cf login -a ${PCF_ENDPOINT} -u ${PCF_USERNAME} -p ${PCF_PASSWORD} --skip-ssl-validation
cf target -o ${PCF_ORG} -s ${DEPLOY_SPACE}
cf push ${APPLICATION_NAME} -p spring-music-1.0.${BUILD_NUMBER}.jar -b https://github.com/cloudfoundry/java-buildpack.git
'''
}
stage("View Results"){
sh '''
echo "Please visit the Following URLs to View ${APPLICATION_NAME}'s Results"
echo
echo "Artifactory: http://18.216.57.173:8081/artifactory/webapp/#/artifacts/browse/tree/General/sample-test"
echo
echo "SonarQube User/Password = system/csnpworkshop01"
echo
echo "SonarQube URL: http://18.188.152.100:9000/projects"
echo
echo "See your running application on PCF: https://console.run.pivotal.io"
echo
echo "Your Application Direct URL:" https://${APPLICATION_NAME}.cfapps.io
echo
echo sleeping for ${SLEEP_SECONDS} before stoping your application
sleep ${SLEEP_SECONDS}
'''
}
stage("Stopping ${APPLICATION_NAME}"){
sh '''
cf login -a ${PCF_ENDPOINT} -u ${PCF_USERNAME} -p ${PCF_PASSWORD} --skip-ssl-validation
cf target -o ${PCF_ORG} -s ${DEPLOY_SPACE}
cf stop ${APPLICATION_NAME}
cf logout
echo "Your app has been stopped. Please adjust the sleep timer above if you don't want the app to be stopped"
'''
}
stage("Cleaning Worksapce") {
cleanWs()
}
}
}
}
}
}