-
Notifications
You must be signed in to change notification settings - Fork 14
/
buildImage.groovy
107 lines (99 loc) · 4.99 KB
/
buildImage.groovy
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
// Builds a container image and returns its name.
// Available parameters:
// dockerFile string -- DockerFile used for the buildconfig
// env dict -- Additional environment variables to set during build
// memory amount of RAM to request
// cpu amount of CPU to request
def call(params = [:]) {
def imageName
def bcObj
if (!params['env']) {
params['env'] = []
}
openshift.withCluster() {
openshift.withProject() {
stage('CleanUp') {
// Once we have the TTL controller enabled we can remove this stage
// More: https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/
def jobs = openshift.selector("jobs").names()
for (job in jobs) {
if (job.startsWith('job/coreos-ci')) {
def obj = openshift.selector(job).object()
if (obj.status.completionTime) {
openshift.selector(job).delete()
}
}
}
}
stage('Prepare Env') {
// TODO: Change buidlconfig to yaml and to use openshift.process
// Issue described in:
// https://github.com/coreos/coreos-ci-lib/pull/61#discussion_r569401940
def bcJSON = libraryResource 'com/github/coreos/buildconfig.json'
def jobYaml = libraryResource 'com/github/coreos/job.yaml'
def jobObj = readYaml text: jobYaml
bcObj = readJSON text: bcJSON
def UUID = UUID.randomUUID()
def scmVars = checkout scm
def repo = (scmVars.GIT_URL.substring(scmVars.GIT_URL.lastIndexOf('/') + 1)).replace('.git','')
jobObj = openshift.process(jobObj, "-p", "NAME=coreos-ci-$repo-${UUID}")
def app = openshift.create(jobObj)
def job = app.narrow('job')
def jobMap = job.object()
def uid = jobMap.metadata.get('uid')
imageName = "coreos-ci-$repo-${UUID}".toString()
//Create and unique name for image and bc
bcObj['parameters'][0] +=['name': 'NAME', 'value': imageName]
//Add OwnerReference for cascading deletion with the Job timeout
bcObj['parameters'][3] +=['name': 'UID', 'value': "${uid}".toString()]
bcObj['parameters'][2] +=['name': 'OWNERNAME', 'value': imageName]
if (params['dockerFile']) {
bcObj['parameters'][1] +=['name': 'DOCKERFILE', 'value': "${params['dockerFile']}".toString()]
}
// these should check 'kind' instead of hardcoding 1
params['env'].each{ name, val ->
bcObj['objects'][1]['spec']['strategy']['dockerStrategy']['env'] += ['name': name, 'value': val]
}
// initialize so we can populate it more easily
bcObj['objects'][1]['spec']['resources'] = [requests: [:], limits: [:]]
if (params['memory']) {
bcObj['objects'][1]['spec']['resources']['requests']['memory'] = params['memory'].toString()
}
if (params['cpu']) {
bcObj['objects'][1]['spec']['resources']['requests']['cpu'] = params['cpu'].toString()
// Also propagate CPU count to NCPUS, because it can be hard in a Kubernetes environment
// to determine how much CPU one should really use.
bcObj['objects'][1]['spec']['strategy']['dockerStrategy']['env'] += ['name': 'NCPUS', 'value': params['cpu'].toString()]
}
}
stage('Create BuildConfig') {
openshift.create(openshift.process(bcObj))
}
stage('Build') {
shwrap("""
touch dir.tar # pre-create it so the directory doesn't change when tar scans it
tar --exclude dir.tar --exclude=./pod* -zcf dir.tar .
""")
def build = openshift.selector('bc', imageName).startBuild("--from-archive=dir.tar")
shwrap("rm dir.tar")
// Showing logs in Jenkins is also a way
// to wait for the build to finsih
build.logs('-f')
// Wait up to 2h for the build to finish
timeout(60*2) {
build.untilEach(1) {
def phase = it.object().status.phase
if (phase in ["New", "Pending", "Running"]) {
return false
}
if (phase != "Complete") {
error("Error build image: status phase ${phase}")
}
return true
}
}
}
}
}
return imageName.toString()
}