-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
320 lines (255 loc) · 10.3 KB
/
build.gradle
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import com.avast.gradle.dockercompose.ContainerInfo
import com.avast.gradle.dockercompose.tasks.ComposeDown
import com.avast.gradle.dockercompose.tasks.ComposeUp
import com.bmuschko.gradle.docker.tasks.container.DockerWaitContainer
import java.util.concurrent.Callable
plugins {
id "eu.xenit.docker" version "5.1.1" apply false
id "eu.xenit.docker-compose" version "5.1.1" apply false
}
static String getBranchName(Project project) {
return project.hasProperty('branchName') ? project.branchName : (System.getenv('BRANCH_NAME') ?: 'local')
}
static String getTagQualifier(String branchName) {
if (branchName.endsWith('release')) {
return ''
}
if (branchName.endsWith('master')) {
return '-SNAPSHOT'
}
return '-' + branchName.split('/').last() + '-SNAPSHOT'
}
static List<String> calcTags(Project project) {
def java = project.java
def os = project.os
def extraTags = project.hasProperty('extraTags') ? project.extraTags : []
def javaTags = []
javaTags << "${java.flavor}-${java.version.major}u${java.version.update}"
javaTags << "${java.flavor}-${java.version.major}"
def osTags = []
osTags << "${os.vendor}-${os.version}"
if (os.name) osTags << os.name
if (isDefaultOsVersion(os)) osTags << os.vendor
// create every java- & os-tag combination
def tags = [javaTags, osTags]
.combinations()
.collect { it.join('-') }
// add another tag that omits distro-information if
// it's our default OS release (currently Ubuntu 16.04)
if (isDefaultOsRelease(os)) {
tags += javaTags
}
def allTags = extraTags + tags
def tagsWithQualifier = allTags.collect { it + getTagQualifier(getBranchName(project)) }
return tagsWithQualifier
}
ext {
temurin = [
8: [major: 8, update: 312, extra: 'b07', esum: '699981083983b60a7eeb511ad640fae3ae4b879de5a3980fe837e8ade9c34a08'],
11: [major: 11, update: 13, extra: '8', esum: '3b1c0c34be4c894e64135a454f2d5aaa4bd10aea04ec2fa0c0efe6bb26528e30']
]
ORACLEOPENJDK = [
7: [
'ubuntu': [major: 7, update: 211]
]
]
}
static Map getDefaultOsVersion(vendor) {
[
[vendor: 'ubuntu', version: '18.04']
].find { it.vendor == vendor }
}
static boolean isDefaultOsVersion(os) {
def defaultRelease = getDefaultOsVersion(os.vendor)
return defaultRelease.version == os.version
}
static boolean isDefaultOsRelease(os) {
return os.vendor == 'ubuntu' && isDefaultOsVersion(os)
}
static ContainerInfo getFirstContainer(String serviceName, ComposeUp composeTask) {
composeTask.servicesInfos.get(serviceName).firstContainer
}
subprojects { Project project ->
repositories {
mavenCentral()
}
project.projectDir.mkdir()
apply plugin: 'eu.xenit.docker'
apply plugin: 'eu.xenit.docker-compose'
apply plugin: 'base'
apply from: "${project.projectDir}.gradle"
task copyDocker(type: Copy) {
if (new File("$project.parent.projectDir/src/main/docker/Dockerfile.${project.os.vendor}.${project.java.vendor}").exists()) {
from("$project.parent.projectDir/src/main/docker") {
include "Dockerfile.${project.os.vendor}.${project.java.vendor}"
rename "Dockerfile.${project.os.vendor}.${project.java.vendor}", 'Dockerfile'
include 'docker-entrypoint.sh'
include 'docker-entrypoint.d/'
}
into "${project.buildDir}/docker"
}
}
buildDockerImage.dependsOn(copyDocker)
dockerBuild {
dockerFile = file("${project.buildDir}/docker/Dockerfile")
repositories = ["docker.io/xenit/openjdk"]
tags = calcTags(project)
}
buildDockerImage {
buildArgs = [
'BASE_IMAGE' : "${project.os.vendor}:${project.os.version}",
'JAVA_FLAVOR' : project.java.flavor,
'JAVA_VERSION_MAJOR' : "${project.java.version.major}",
'JAVA_VERSION_UPDATE': "${project.java.version.update}"
]
if (project.java.version.get('extra')) {
if (java.version.major < 11) {
buildArgs.put('JAVA_VERSION', "${project.java.version.major}u${project.java.version.update}-${project.java.version.extra}");
buildArgs.put('JAVA_VERSION_COMPACT', "${project.java.version.major}u${project.java.version.update}${project.java.version.extra}");
} else {
buildArgs.put('JAVA_VERSION', "-${project.java.version.major}.0.${project.java.version.update}+${project.java.version.extra}");
buildArgs.put('JAVA_VERSION_COMPACT', "${project.java.version.major}.0.${project.java.version.update}_${project.java.version.extra}");
}
}
if (project.java.version.get('esum')) {
buildArgs.put('JAVA_VERSION_ESUM', "${project.java.version.esum}")
}
buildArgs.putAll(project.findProperty('extraBuildArgs') ?: [:])
doFirst {
println buildArgs
}
}
clean.doLast {
project.projectDir.delete()
}
docker {
registryCredentials {
username = System.getenv("DOCKER_USER")
password = System.getenv("DOCKER_PASSWORD")
}
}
dockerCompose {
captureContainersOutput = true
goss {
useComposeFiles = [
"${project.parent.projectDir}/src/test/goss/docker-compose.yml"
]
if (project.hasProperty('extraBuildArgs') && project.extraBuildArgs.get('JAVA_VERSION_REPORTED'))
environment.put 'GOSS_JAVA_VERSION', project.extraBuildArgs.get('JAVA_VERSION_REPORTED')
else if (project.java.version.major > 8)
environment.put 'GOSS_JAVA_VERSION', "${project.java.version.major}.0.${project.java.version.update}"
else
environment.put 'GOSS_JAVA_VERSION', "1.${project.java.version.major}.0_${project.java.version.update}"
environment.put 'GOSS_OS_VENDOR', "${project.os.vendor}"
environment.put 'GOSS_OS_VERSION', "${project.os.version}"
environment.put 'JAVA_OPTS_GOSS', "-Dtest.java.opts=goss-test"
}
basic {
useComposeFiles = [
"${project.parent.projectDir}/src/test/compose/docker-compose.yml"
]
}
}
// Create a task that runts the compose-configuration and verifies
// that the java container exits with status code 0
def createComposeTestTask = { String config ->
String taskname = "test${config.capitalize()}ComposeUp"
def waitTask = project.tasks.create(taskname, DockerWaitContainer.class) { DockerWaitContainer task1 ->
group = 'verification'
ComposeUp composeUp = project.tasks["${config}ComposeUp"]
task1.dependsOn(composeUp)
task1.targetContainerId(new Callable<String>() {
@Override
String call() throws Exception {
ContainerInfo javaContainer = getFirstContainer('java', composeUp)
return javaContainer.getContainerId()
}
})
onNext { r ->
ContainerInfo container = getFirstContainer('java', composeUp)
println "Container ${container} exited (exit code '${r.statusCode}'"
if (r.statusCode) {
throw new GradleException("Docker image test failed: ${container.inspection.Config.Image}")
}
}
}
ComposeDown composeDown = project.tasks["${config}ComposeDown"]
project.tasks.composeBasedImageTests.finalizedBy(composeDown)
project.tasks.composeDownAll.dependsOn(composeDown)
return waitTask
}
task composeDownAll {
}
task composeBasedImageTests {
group = 'verification'
['basic', 'goss'].each {
Task test = createComposeTestTask(it)
dependsOn test
}
}
check {
dependsOn(composeBasedImageTests)
}
// TODO this can be removed once DEVEM-343 is resolved
buildDockerImage {
doLast {
project.tasks.withType(ComposeUp.class) { ComposeUp composeUp ->
composeUp.settings.environment.put("DOCKER_IMAGE", buildDockerImage.getImageId())
}
}
}
// All ComposeUp tasks should depend on `buildDockerImage` in our case ?
project.tasks.withType(ComposeUp.class) { ComposeUp composeTask ->
composeTask.dependsOn(project.tasks.buildDockerImage)
}
}
task printSupportedTags {
doFirst {
def currentTags = (subprojects).collect { calcTags(it) }
println '## Supported tags'
println ''
currentTags.each { tags ->
println "* ${tags.collect { "`$it`" }.join(', ')}"
}
}
}
task printTags {
doFirst {
project.subprojects.collect().forEach { p ->
println "* ${calcTags(p).reverse().collect { tag -> "`${tag}`" }.join(', ')}"
}
}
}
task verifyImageTags(group: 'verification') {
doLast {
// allTags is a map from tag -> [ projects ]
def allTags = subprojects.collectEntries {
[(it): calcTags(it)]
}
.collectMany { project, tags ->
tags.collect { new MapEntry(it, project) }
}
.groupBy { it.key.toString() }
.collectEntries { [(it.key), it.value.collect { entry -> entry.value }] }
// Check there are no duplicate tag declarations for the same java repo
allTags
.findAll { it.value.size() > 1 }
.each {
throw new GradleException("Image tag '$it.key' is defined in multiple projects: $it.value")
}
// Check that a few common/expected tags are actually published
println allTags.keySet().sort()
def branchName = getBranchName(project)
if (branchName.endsWith("release")) {
def expectedTags = ['jdk-8', 'jdk-11']
expectedTags.each {
if (!allTags.containsKey(it)) {
throw new GradleException("Tag '$it' is expected, but not defined in any project")
}
}
}
}
}
task check {
dependsOn(verifyImageTags)
}