-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.gradle
295 lines (255 loc) · 12 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
import org.apache.tools.ant.filters.ReplaceTokens
import com.bmuschko.gradle.docker.tasks.image.Dockerfile
buildscript {
apply from: "https://raw.githubusercontent.com/blackducksoftware/integration-resources/master/gradle_common/buildscript-repositories.gradle", to: buildscript
apply from: 'https://raw.githubusercontent.com/blackducksoftware/integration-resources/master/gradle_common/buildscript-cgp-version.gradle'
ext {
javaTargetCompatibility = 8
javaSourceCompatibility = 8
}
ext['logback.version'] = '1.2.13'
dependencies { classpath "com.blackduck.integration:common-gradle-plugin:${managedCgpVersion}" }
dependencies {
classpath 'com.bmuschko:gradle-docker-plugin:6.7.0'
}
}
plugins {
id 'org.springframework.boot' version '2.6.6'
}
final def externalRepoHost = "https://repo.blackduck.com"
final def internalRepoHost = System.getenv('SNPS_INTERNAL_ARTIFACTORY')
repositories {
println "Checking if environment property SNPS_INTERNAL_ARTIFACTORY is configured: ${internalRepoHost}"
maven {
url = uri("${internalRepoHost}/artifactory/bds-integrations-release")
}
maven {
url = uri("${internalRepoHost}/artifactory/bds-integrations-snapshot")
}
maven {
url = uri("${internalRepoHost}/artifactory/bds-integration-placeholder-release")
}
maven {
url = uri("${externalRepoHost}/artifactory/bds-integration-placeholder-release")
}
maven {
url "${externalRepoHost}/bds-bdio-release"
}
}
version = '6.1.0-SNAPSHOT'
def appName='blackduck-imageinspector'
apply plugin: 'eclipse'
apply plugin: "io.spring.dependency-management"
apply plugin: 'com.bmuschko.docker-remote-api'
apply plugin: 'com.blackduck.integration.solution'
// TODO these don't need to be arrays:
def linuxFlavorsDpkg = ["debian:11"] // the image is still named "ubuntu"
def linuxFlavorsRpm = ["fedora:33"] // the image is still named "centos"
def linuxFlavorsApk = ["alpine:latest"]
def linuxFlavors = linuxFlavorsDpkg + linuxFlavorsRpm + linuxFlavorsApk
String blackduckDir = "/opt/blackduck"
String imagePgmDir = "${blackduckDir}/${appName}"
String dockerInspectorDir = "${blackduckDir}/blackduck-docker-inspector"
final def versionFile = new File("${projectDir}/src/main/resources/version.properties")
versionFile.delete()
versionFile << "program.version=${version}"
publishing {
publications {
mavenJava(MavenPublication) {
def artifactName = "${buildDir}/libs/${appName}-${version}.jar"
artifact (artifactName)
}
}
}
ext['log4j2.version'] = '2.16.0'
dependencies {
implementation 'com.blackduck.integration:hub-imageinspector-lib:15.0.1'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'ch.qos.logback:logback-classic:1.2.13'
constraints {
implementation('ch.qos.logback:logback-core:1.2.13') {
because 'previous version has a vulnerability'
}
}
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation 'org.apache.commons:commons-exec:1.3'
testImplementation 'io.fabric8:kubernetes-client:3.1.8'
testImplementation 'io.fabric8:kubernetes-model:2.0.8'
}
// Prepare test config file
// System.getProperty("user.dir")
task buildKubeTestPodConfig(type: Copy) {
from 'src/test/resources/kube-test-pod.yml'
into 'build/classes/java/test/com/blackduck/integration/blackduck/imageinspectorws/app'
filter(ReplaceTokens, tokens: [CURRENT_DIR: System.getProperty("user.dir"), VERSION: version])
}
task testPrep(dependsOn: buildKubeTestPodConfig) {}
//////////// Create Docker Containers ///////////
task createApkDockerfileTasks() {
linuxFlavorsApk.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
task "create${linuxFlavorDirName}Dockerfile"(type: Dockerfile) {
destFile = project.file("${buildDir}/images/${linuxFlavorDirName}/${appName}/Dockerfile")
logger.lifecycle("destFile: ${destFile}")
from linuxFlavor
exposePort 8080
runCommand 'apk update && apk add bash && apk add openjdk11-jre'
runCommand "mkdir -p ${imagePgmDir}/shared/target"
runCommand "mkdir -p ${imagePgmDir}/shared/output"
addFile("${appName}-${version}.jar", "${imagePgmDir}/${appName}.jar")
runCommand "chown -R 10001 ${imagePgmDir}"
runCommand "chmod -R g+w /lib/apk"
runCommand "mkdir -p ${dockerInspectorDir}/config"
runCommand "mkdir -p ${dockerInspectorDir}/target"
runCommand "mkdir -p ${dockerInspectorDir}/output"
runCommand "mkdir -p ${dockerInspectorDir}/working"
runCommand "chmod -R g+w ${dockerInspectorDir}"
user '10001'
defaultCommand "java", "-jar", "${imagePgmDir}/${appName}.jar", "--server.port=8080", "--current.linux.distro=${linuxFlavorDirName}"
}
}
}
task createRpmDockerfileTasks() {
linuxFlavorsRpm.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
task "create${linuxFlavorDirName}Dockerfile"(type: Dockerfile) {
destFile = project.file("${buildDir}/images/${linuxFlavorDirName}/${appName}/Dockerfile")
logger.lifecycle("destFile: ${destFile}")
from linuxFlavor
exposePort 8081
runCommand 'yum update -y && yum install -y java-11-openjdk && yum clean all -y'
runCommand "mkdir -p ${imagePgmDir}/shared/target"
runCommand "mkdir -p ${imagePgmDir}/shared/output"
addFile("${appName}-${version}.jar", "${imagePgmDir}/${appName}.jar")
runCommand "chown -R 10001 ${imagePgmDir}"
runCommand "chmod -R g+w /var/lib"
runCommand "mkdir -p ${dockerInspectorDir}/config"
runCommand "mkdir -p ${dockerInspectorDir}/target"
runCommand "mkdir -p ${dockerInspectorDir}/output"
runCommand "mkdir -p ${dockerInspectorDir}/working"
runCommand "chmod -R g+w ${dockerInspectorDir}"
environmentVariable('LANG', 'en_US.UTF-8')
user '10001'
defaultCommand "java", "-jar", "${imagePgmDir}/${appName}.jar", "--server.port=8081", "--current.linux.distro=centos"
}
}
}
task createDpkgDockerfileTasks() {
linuxFlavorsDpkg.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
task "create${linuxFlavorDirName}Dockerfile"(type: Dockerfile) {
destFile = project.file("${buildDir}/images/${linuxFlavorDirName}/${appName}/Dockerfile")
logger.lifecycle("destFile: ${destFile}")
from linuxFlavor
exposePort 8082
runCommand 'apt-get update -y && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y locales && apt-get -y clean'
runCommand "mkdir -p ${imagePgmDir}/shared/target"
runCommand "mkdir -p ${imagePgmDir}/shared/output"
addFile("${appName}-${version}.jar", "${imagePgmDir}/${appName}.jar")
runCommand "chown -R 10001 ${imagePgmDir}"
runCommand "chmod -R g+w /var/lib/dpkg"
runCommand "mkdir -p ${dockerInspectorDir}/config"
runCommand "mkdir -p ${dockerInspectorDir}/target"
runCommand "mkdir -p ${dockerInspectorDir}/output"
runCommand "mkdir -p ${dockerInspectorDir}/working"
runCommand "chmod -R g+w ${dockerInspectorDir}"
runCommand 'locale-gen "en_US.UTF-8"'
runCommand 'apt -y purge locales && apt-get -y autoremove && apt-get -y clean'
environmentVariable('LANG', 'en_US.UTF-8')
user '10001'
defaultCommand "java", "-jar", "${imagePgmDir}/${appName}.jar", "--server.port=8082", "--current.linux.distro=ubuntu"
}
}
}
task createDockerfiles(dependsOn: [createdebianDockerfile, createfedoraDockerfile, createalpineDockerfile]) {
}
task createCopyJarToDockerfileDirTasks(type: Copy, dependsOn: [createDockerfiles]) {
linuxFlavors.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
logger.lifecycle("Creating task to copy jar from build/libs/${project.name}-${version}.jar to Dockerfile dir for ${linuxFlavorDirName}, renaming to ${appName}-${version}.jar")
task "copyJarToDockerfileDir_${linuxFlavorDirName}"(type: Copy, dependsOn: build) {
from "build/libs/${project.name}-${version}.jar"
into "${buildDir}/images/${linuxFlavorDirName}/${appName}"
rename ("${project.name}-${version}.jar", "${appName}-${version}.jar")
}
}
}
task copyJarFileToDockerfileDirs(dependsOn: [copyJarToDockerfileDir_debian, copyJarToDockerfileDir_fedora, copyJarToDockerfileDir_alpine]) {
}
task createRemoveDockerImageTasks() {
linuxFlavors.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
def taskName = "removeDockerImage_${linuxFlavorDirName}"
String suffix = getSuffix(linuxFlavorDirName)
logger.lifecycle("creating task ${taskName}")
task "${taskName}"(type: Exec) {
ignoreExitValue true
commandLine "docker", "rmi", "blackducksoftware/${appName}${suffix}:${version}"
}
}
}
task buildImageDirs(dependsOn: [createDockerfiles, copyJarFileToDockerfileDirs]) {
}
task createBuildDockerImageTasks() {
linuxFlavors.eachWithIndex {linuxFlavor, index ->
String linuxFlavorDirName = getNameWithoutVersion(linuxFlavor)
def taskName = "buildDockerImage_${linuxFlavorDirName}"
String suffix = getSuffix(linuxFlavorDirName)
logger.lifecycle("creating task ${taskName}")
task "${taskName}"(type: Exec, dependsOn: ["removeDockerImage_${linuxFlavorDirName}", buildImageDirs]) {
commandLine "docker", "build", "--tag", "blackducksoftware/${appName}${suffix}:${version}", \
"${buildDir}/images/${linuxFlavorDirName}/${appName}"
}
}
}
task buildImages(dependsOn: [buildDockerImage_alpine, buildDockerImage_fedora, buildDockerImage_debian]) {
}
test.dependsOn testPrep
task dockerLogin(type: Exec) {
String dockerLoginUsername = System.env.DOCKER_INT_BLACKDUCK_USER ?: '<Not Set>'
doFirst {
String dockerLoginPassword = System.env.DOCKER_INT_BLACKDUCK_PASSWORD
if (dockerLoginPassword?.trim()) {
standardInput = new ByteArrayInputStream(dockerLoginPassword.getBytes())
}
}
commandLine 'docker', 'login', '--username', dockerLoginUsername, '--password-stdin'
onlyIf {
System.env.DOCKER_INT_BLACKDUCK_USER != null && System.env.DOCKER_INT_BLACKDUCK_PASSWORD != null
}
}
task pushImageAlpine(type: Exec, dependsOn: [buildDockerImage_alpine, dockerLogin]) {
commandLine "docker", "push", "blackducksoftware/${appName}-alpine:${version}"
}
task pushImageFedora(type: Exec, dependsOn: [buildDockerImage_fedora, dockerLogin]) {
commandLine "docker", "push", "blackducksoftware/${appName}-centos:${version}"
}
task pushImageUbuntu(type: Exec, dependsOn: [buildDockerImage_debian, dockerLogin]) {
commandLine "docker", "push", "blackducksoftware/${appName}-ubuntu:${version}"
}
task pushImages(dependsOn: [pushImageAlpine, pushImageFedora, pushImageUbuntu]) {
}
//// helper methods ////
String getNameWithoutVersion(String linuxFlavor) {
String linuxFlavorDirName = linuxFlavor
int colonIndex = linuxFlavor.indexOf(':')
if (colonIndex >= 0) {
linuxFlavorDirName = linuxFlavor.subSequence(0, colonIndex)
}
linuxFlavorDirName
}
private String getSuffix(String linuxFlavorName) {
String suffix
if ("debian".equals(linuxFlavorName)) {
suffix = "-ubuntu"
} else if ("fedora".equals(linuxFlavorName)) {
suffix = "-centos"
} else {
suffix = "-${linuxFlavorName}"
}
suffix
}