forked from csnp/devsecops-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
124 lines (113 loc) · 4.23 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
#!/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_FIRST_NAME}-${SPRING_APP}"
DEPLOY_SPACE = "Development"
PCF_ORG = "Cybersecurity NP"
ARTIFACT_URL = "http://3.17.145.188:8081/artifactory/chicago-workshop/"
SONARQUBE_ENDPOINT = "http://18.188.152.100:9000"
PCF_ENDPOINT = "https://api.run.pivotal.io"
// ------------------------------- Use Jenkins Credential Store ------------------------------------------------
withCredentials([
[
$class : 'StringBinding',
credentialsId : 'sonarqube',
variable : 'SONARQUBE_TOKEN'
],
[
$class : 'UsernamePasswordMultiBinding',
credentialsId : 'josh_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
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
cf logout
'''
}
stage("Cleaning Worksapce") {
cleanWs()
}
}
}
}
}
}