forked from 1701-jan09-java/interview-evaluations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
128 lines (101 loc) · 4.74 KB
/
Jenkinsfile
File metadata and controls
128 lines (101 loc) · 4.74 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
#!/usr/bin/env groovy
// pipeline links:
// https://github.com/jenkinsci/kubernetes-plugin#pipeline-support
// https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes/blob/master/sample-app/Jenkinsfile
// https://jenkins.io/doc/pipeline/steps/credentials-binding/
// https://medium.com/garbage-collection/jenkins-pipelines-what-i-wish-i-knew-starting-out-6e3d4eb2ff5b#.plbehje7t
// sonar maven links:
// https://docs.sonarqube.org/display/PLUG/GitHub+Plugin
// https://docs.sonarqube.org/display/SONAR/Analysis+Parameters
podTemplate(label: 'jenkins-worker', containers: [
containerTemplate(name: 'maven-docker-kubectl', image: 'tjkemper/maven-docker-kubectl:0.1', ttyEnabled: true, command: 'cat')
],
volumes: [hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')]
) {
node ('jenkins-worker') {
try {
stage ('Checkout Source Code') {
checkout scm
notifyBuild("STARTED")
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
imageTag = "revature/interview-evaluations:${gitCommit}"
sh "env" //print all environment variables
}
container('maven-docker-kubectl') {
stage ('Test') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "8301f826-3f5d-4b20-8902-f34ea98aebd7",
usernameVariable: 'INTEVAL_USER', passwordVariable: 'INTEVAL_PASS']]) {
withCredentials([string(credentialsId: 'f9ca6321-287d-4402-b0f6-9f661e06d399', variable: 'INTEVAL_URL')]) {
sh "mvn clean \
-DINTEVAL_URL=${INTEVAL_URL} \
-DINTEVAL_USER=${INTEVAL_USER} \
-DINTEVAL_PASS=${INTEVAL_PASS} \
package"
} // withCredentials
} // withCredentials
}
if (BRANCH_NAME != "master") {
stage ('SonarQube Analysis') {
withSonarQubeEnv("My SonarQube") {
withCredentials([string(credentialsId: '504252c1-ed1f-42cb-9208-8d9b355158bd', variable: 'GITHUB_ACCESS_TOKEN')]) {
sh "mvn ${SONAR_MAVEN_GOAL} \
-Dsonar.analysis.mode=preview \
-Dsonar.host.url=${SONAR_HOST_URL} \
-Dsonar.login=${SONAR_AUTH_TOKEN} \
-Dsonar.github.oauth=${GITHUB_ACCESS_TOKEN} \
-Dsonar.github.repository=1701-jan09-java/interview-evaluations \
-Dsonar.github.pullRequest=${CHANGE_ID} \
"
}
}
}
}
stage ('Docker Build') {
sh "docker build -t ${imageTag} ."
}
if (BRANCH_NAME == "master") {
stage ('Docker Push') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "43d121d5-8c7c-4234-b2d9-2b4279f04053",
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh "docker login -u ${USERNAME} -p ${PASSWORD}"
sh "docker push ${imageTag}"
}
}
stage ('Kubernetes Deploy') {
sh "sed -i.bak 's#revature/interview-evaluations:0.1#${imageTag}#' ./k8s/deployment.yaml"
sh "kubectl apply -f ./k8s/ingress.yaml -f ./k8s/service.yaml -f ./k8s/deployment.yaml"
}
}
}
notifyBuild("SUCCESSFUL")
} catch(Exception e) {
notifyBuild("FAILURE")
throw e
}
}
}
def notifyBuild(String buildStatus) {
def colorCode
// Override default color based on build status
switch (buildStatus) {
case "STARTED":
colorCode = "#FFFF00"
break
case "SUCCESSFUL":
colorCode = "#00FF00"
break
case "FAILURE":
colorCode = "#FF0000"
break
default:
colorCode = "#000000"
}
buildType = BRANCH_NAME == "master" ? "DEPLOYMENT" : ""
if (buildType == "DEPLOYMENT") {
colorCode = '#0000FF'
}
// vars
def commitAuthor = sh script: "git show -s --pretty=%an", returnStdout: true
def message = "$buildType $buildStatus\n*Job:* ${JOB_NAME} [${BUILD_NUMBER}]\n*Job Link:* ${BUILD_URL}\n*Author:* $commitAuthor"
slackSend (color: colorCode, message: message)
}