-
Notifications
You must be signed in to change notification settings - Fork 43
/
Jenkinsfile
144 lines (122 loc) · 4.56 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
#!groovy
@Library('Utils')
import com.redhat.*
properties([disableConcurrentBuilds()])
node {
def source = ""
def dockerfiles = null
String scmUrl = scm.browser.url
String scmRef = "master"
String buildName = ""
def gitHubUtils = new com.redhat.GitHubUtils()
echo sh(returnStdout: true, script: 'env')
if(env.CHANGE_BRANCH) {
scmRef = "${env.CHANGE_BRANCH}"
}
else if(!(env.BRANCH_NAME.contains("PR"))) {
scmRef = "${env.BRANCH_NAME}"
}
/* if CHANGE_URL is defined then this is a pull request
* additional steps are required to determine the git url
* and branch name to pass to new-build.
* Otherwise just use the scm.browser.url and scm.branches[0]
* for new-build.
*/
else if (env.CHANGE_URL) {
def pull = null
stage('Github Url and Ref') {
// Query the github repo api to return the clone_url and the ref (branch name)
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "github", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
pull = gitHubUtils.getGitHubPR(env.USERNAME, env.PASSWORD, env.CHANGE_URL)
scmUrl = pull.url
scmRef = pull.ref
}
}
}
/* Checkout source and find all the Dockerfiles.
* This will not include Dockerfiles with extensions. Currently the issue
* with using a Dockerfile with an extension is the oc new-build command
* does not offer an option to provide the dockerfilePath.
*/
stage('checkout') {
checkout scm
dockerfiles = findFiles(glob: '**/Dockerfile')
}
for (int i = 0; i < dockerfiles.size(); i++) {
def resources = null
def is = ""
def dockerImageRepository = ""
String path = dockerfiles[i].path.replace(dockerfiles[i].name, "")
String normalizeRef = scmRef.replace('_', '-').toLowerCase()
String normalizePath = path.replace('/','').replace('_','-').toLowerCase()
buildName = "${normalizePath}-${normalizeRef}"
if(buildName.contains("scratch")) {
continue
}
try {
/* Execute oc new-build on each dockerfile available
* in the repo. The context-dir is the path removing the
* name (i.e. Dockerfile)
*/
println(">>>>> buildName: ${buildName}")
println(">>>>> scmRef: ${scmRef}")
println(">>>>> scmUrl: ${scmUrl}")
newBuild = newBuildOpenShift() {
name = buildName
url = scmUrl
branch = scmRef
contextDir = path
deleteBuild = false
randomName = false
}
dockerImageRepository = getImageStreamRepo(newBuild.buildConfigName).dockerImageRepository
runOpenShift {
name = buildName
deletePod = false
branch = scmRef
image = dockerImageRepository
env = ["foo=goo"]
}
/* move this in another revision of openshift-pipeline-library */
stage('OpenShift exec') {
openshift.withCluster() {
openshift.withProject() {
def exec = null
try {
exec = openshift.exec("${buildName} ps aux")
println("${exec.out}")
} catch(err) {
println("ps aux failed: ${err}")
}
try {
exec = openshift.exec("${buildName} whoami")
println("whoami: ${exec.out}")
} catch(err) {
println("whoami failed: ${err}")
}
}
}
}
resources = newBuild.names
currentBuild.result = 'SUCCESS'
}
catch(all) {
currentBuild.result = 'FAILURE'
echo "Exception: ${all}"
}
finally {
stage('Clean Up Resources') {
echo "Clean up..."
openshift.withCluster() {
openshift.withProject() {
openshift.selector("pod/${buildName}").delete()
for (r in resources) {
openshift.selector(r).delete()
}
}
}
}
}
}
}
// vim: ft=groovy