-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJenkinsfile
174 lines (150 loc) · 5.88 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@Library('releng-pipeline@main') _
pipeline {
agent {
kubernetes {
yaml loadOverridableResource(
libraryResource: 'org/eclipsefdn/container/agent.yml'
)
}
}
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
disableConcurrentBuilds()
timeout(time: 90, unit: 'MINUTES')
}
environment {
HOME = '${env.WORKSPACE}'
CREDENTIALS_ID = 'e93ba8f9-59fc-4fe4-a9a7-9a8bd60c17d9'
BUILD_DIR = './target'
}
triggers {
cron('H H * * */3')
}
stages {
stage('Build Agents') {
steps {
container('containertools') {
sh '''
mkdir -p "${BUILD_DIR}"
jsonnet agents.jsonnet > "${BUILD_DIR}/agents.json"
'''
}
}
}
stage('Build Images') {
steps {
script {
def agentIds = sh(script: "jq -r '. | keys[]' ${env.BUILD_DIR}/agents.json", returnStdout: true).trim().split('\n')
def stages = [:]
agentIds.each { id ->
// do not build centos images
if (id != "centos-7" && id != "centos-8") {
stages.putAll(buildAgent(id))
}
}
stash(name: 'workspace', includes: '**')
parallel(stages)
}
}
}
}
post {
failure {
mail to: '[email protected]',
subject: "[CBI] Build Failure ${currentBuild.fullDisplayName}",
mimeType: 'text/html',
body: "Project: ${env.JOB_NAME}<br/>Build Number: ${env.BUILD_NUMBER}<br/>Build URL: ${env.BUILD_URL}<br/>Console: ${env.BUILD_URL}/console"
}
fixed {
mail to: '[email protected]',
subject: "[CBI] Back to normal ${currentBuild.fullDisplayName}",
mimeType: 'text/html',
body: "Project: ${env.JOB_NAME}<br/>Build Number: ${env.BUILD_NUMBER}<br/>Build URL: ${env.BUILD_URL}<br/>Console: ${env.BUILD_URL}/console"
}
cleanup {
deleteDir() /* clean up workspace */
}
}
}
def buildAgent(id) {
String configDir = "${env.BUILD_DIR}/${id}"
String config = "${configDir}/agent.json"
String AGENTS_JSON="${env.BUILD_DIR}/agents.json"
def stages = [:]
println "Building jiro-agent spec '${id}'"
sh "mkdir -p ${configDir}"
sh "jq -r '.[\"${id}\"]' ${AGENTS_JSON} > ${config}"
sh "jq -r '.spec.docker.dockerfile' ${config} > ${configDir}/Dockerfile"
String name = sh(script: "jq -r '.spec.docker.registry' ${config}", returnStdout: true).trim() +
"/" + sh(script: "jq -r '.spec.docker.repository' ${config}", returnStdout: true).trim() +
"/" + sh(script: "jq -r '.spec.docker.image' ${config}", returnStdout: true).trim()
String version = sh(script: "jq -r '.spec.docker.tag' ${config}", returnStdout: true).trim()
String context = sh(script: "jq -r '.spec.docker.context' ${config}", returnStdout: true).trim()
String buildArgsString = sh(script: "jq -r '.spec.docker.build_args' ${config}", returnStdout: true).trim()
def buildArgsMap = [:]
if (buildArgsString != 'null') {
buildArgsMap = buildArgsString.replaceAll("[\\[\\]\' ]","").tokenize(",").collectEntries{
it.tokenize(":").with{
[(it[0]): it[1]]
}
}
}
stages["${name}:${version}"] = {
buildImage(name, version, "", "${configDir}/Dockerfile", context, buildArgsMap)
}
def result = sh(script: "jq -r '.variants | keys[]' ${config}", returnStdout: true).trim()
result.split('\n').each { variant ->
stages.putAll(buildAgentVariant(id, variant, config))
}
return stages
}
def buildAgentVariant(id, variant, agentConfig) {
def configDir = "${env.BUILD_DIR}/${id}/${variant}"
def config = "${configDir}/variant.json"
def stages = [:]
println "Building jiro-agent '${id}' - variant ${variant}"
sh "mkdir -p ${configDir}"
sh "jq -r '.variants[\"${variant}\"]' ${agentConfig} > ${config}"
sh "jq -r '.docker.dockerfile' ${config} > ${configDir}/Dockerfile"
String name = sh(script: "jq -r '.spec.docker.registry' ${agentConfig}", returnStdout: true).trim() +
"/" + sh(script: "jq -r '.spec.docker.repository' ${agentConfig}", returnStdout: true).trim() +
"/" + sh(script: "jq -r '.spec.docker.image' ${agentConfig}", returnStdout: true).trim()
String version = sh(script: "jq -r '.docker.tag' ${config}", returnStdout: true).trim()
String aliases = sh(script: "jq -r '.docker.aliases | join(\",\")' ${config}", returnStdout: true).trim()
String buildArgsString = sh(script: "jq -r '.spec.docker.build_args' ${config}", returnStdout: true).trim()
def buildArgsMap = [:]
if (buildArgsString != 'null') {
buildArgsMap = buildArgsString.replaceAll("[\\[\\]\' ]","").tokenize(",").collectEntries{
it.tokenize(":").with{
[(it[0]): it[1]]
}
}
}
stages["${name}:${version}"] = {
buildImage(name, version, aliases, "${configDir}/Dockerfile", "${configDir}", buildArgsMap)
}
return stages
}
def buildImage(String name, String version, String aliases = "", String dockerfile, String context, Map<String, String> buildArgs = [:], boolean latest = false) {
String distroName = name + ':' + version + ' (' + aliases + ')'
println '############ buildImage ' + distroName + ' ############'
def containerBuildArgs = buildArgs.collect { k, v -> '--opt build-arg:' + k + '=' + v }.join(' ')
podTemplate(yaml: loadOverridableResource(libraryResource: 'org/eclipsefdn/container/agent.yml')) {
node(POD_LABEL) {
container('containertools') {
unstash('workspace')
containerBuild(
credentialsId: env.CREDENTIALS_ID,
name: name,
version: version,
aliases: aliases,
dockerfile: dockerfile,
context: context,
buildArgs: containerBuildArgs,
push: env.GIT_BRANCH == 'master',
latest: latest
)
}
}
}
}