forked from jfrogtraining/kubernetes_example
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Jenkinsfile
158 lines (141 loc) · 5.68 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
154
155
156
157
158
#!/usr/bin/env groovy
node {
// Cleanup workspace
deleteDir()
//Clone example project from GitHub repository
git url: 'https://github.com/jfrogtraining/kubernetes_example.git', branch: 'master'
def rtServer = Artifactory.server SERVER_ID
def rtDocker = Artifactory.docker server: rtServer
def buildInfo = Artifactory.newBuildInfo()
def tagDockerApp
buildInfo.env.capture = true
//Fetch all depedencies from Artifactory
stage ('Dependencies') {
dir ('docker-app') {
try {
println "Gather Released Docker Framework and Gradle War file"
def gradleWarDownload = """{
"files": [
{
"pattern": "gradle-release-local/org/jfrog/example/gradle/webservice/1.1.2/*.war",
"target": "war/webservice.war",
"props": "unit-test=pass",
"flat": "true"
}
]
}"""
rtServer.download(gradleWarDownload, buildInfo )
} catch (Exception e) {
println "Caught Exception during resolution. Message ${e.message}"
throw e
}
}
}
//Build docker image named docker-app
stage ('Build & Deploy') {
dir ('docker-app') {
sh "sed -i 's/docker.artifactory/${ARTDOCKER_REGISTRY}/' Dockerfile"
tagDockerApp = "${ARTDOCKER_REGISTRY}/docker-app:${env.BUILD_NUMBER}"
println "Docker App Build"
docker.build(tagDockerApp)
println "Docker push" + tagDockerApp + " : " + REPO
buildInfo = rtDocker.push(tagDockerApp, REPO, buildInfo)
println "Docker Buildinfo"
rtServer.publishBuildInfo buildInfo
}
}
stage ("Retag latest image") {
dir('docker-app') {
reTagLatest (SOURCE_REPO)
}
}
//Test docker image
stage ('Test') {
dir('docker-app/app-test') {
tagDockerApp = "${ARTDOCKER_REGISTRY}/docker-app:${env.BUILD_NUMBER}"
if (testApp(tagDockerApp)) {
println "Setting property and promotion"
sh 'docker rmi '+tagDockerApp+' || true'
} else {
currentBuild.result = 'UNSTABLE'
return
}
}
}
//Scan Build Artifacts in Xray
stage('Xray Scan') {
if (XRAY_SCAN == "YES") {
def xrayConfig = [
'buildName' : env.JOB_NAME,
'buildNumber' : env.BUILD_NUMBER,
'failBuild' : false
]
def xrayResults = rtServer.xrayScan xrayConfig
echo xrayResults as String
} else {
println "No Xray scan performed. To enable set XRAY_SCAN = YES"
}
sleep 60
}
//Promote docker image from staging local repo to production repo in Artifactory
/* stage ('Promote') {
dir('docker-app/app-test') {
def promotionConfig = [
'buildName' : env.JOB_NAME,
'buildNumber' : env.BUILD_NUMBER,
'targetRepo' : PROMOTE_REPO,
'comment' : 'App works with latest released version of gradle swampup app, tomcat and jdk',
'sourceRepo' : SOURCE_REPO,
'status' : 'Released',
'includeDependencies': false,
'copy' : true
]
rtServer.promote promotionConfig
reTagLatest (SOURCE_REPO)
reTagLatest (PROMOTE_REPO)
}
// promote war file from gradle-dev-local to gradle-release-local
}*/
}
def testApp (tag) {
docker.image(tag).withRun('-p 9191:8181') {c ->
sleep 10
//def stdout = sh(script: 'curl "http://localhost:9191/swampup/"', returnStdout: true)
//if (stdout.contains("Welcome Docker Lifecycle Training")) {
// println "*** Passed Test: " + stdout
println "*** Passed Test"
return true
// } else {
// println "*** Failed Test: " + stdout
// return false
// }
}
}
//Tag docker image
def reTagLatest (targetRepo) {
def BUILD_NUMBER = env.BUILD_NUMBER
sh 'sed -E "s/@/$BUILD_NUMBER/" retag.json > retag_out.json'
switch (targetRepo) {
case PROMOTE_REPO :
sh 'sed -E "s/TARGETREPO/${PROMOTE_REPO}/" retag_out.json > retaga_out.json'
break
case SOURCE_REPO :
sh 'sed -E "s/TARGETREPO/${SOURCE_REPO}/" retag_out.json > retaga_out.json'
break
}
sh 'cat retaga_out.json'
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
def curlString = "curl -u " + env.USERNAME + ":" + env.PASSWORD + " " + SERVER_URL
def regTagStr = curlString + "/api/docker/$targetRepo/v2/promote -X POST -H 'Content-Type: application/json' -T retaga_out.json"
println "Curl String is " + regTagStr
sh regTagStr
}
}
def updateProperty (property) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
def curlString = "curl -u " + env.USERNAME + ":" + env.PASSWORD + " " + "-X PUT " + SERVER_URL
def updatePropStr = curlString + "/api/storage/${SOURCE_REPO}/docker-app/${env.BUILD_NUMBER}?properties=${property}"
println "Curl String is " + updatePropStr
sh updatePropStr
}
}