|
1 | 1 | /* |
2 | 2 | * Copyright OpenSearch Contributors |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
4 | 8 | */ |
5 | 9 |
|
6 | | -plugins { |
7 | | - id 'com.palantir.docker' version '0.35.0' |
| 10 | +def supportedArchitectures = architectures.get('linux') as String[] |
| 11 | + |
| 12 | +class DockerBuildxTask extends Exec { |
| 13 | + @Input |
| 14 | + String platform |
| 15 | + |
| 16 | + @Input |
| 17 | + String tag |
| 18 | + |
| 19 | + @Input |
| 20 | + boolean push = false |
| 21 | + |
| 22 | + @TaskAction |
| 23 | + @Override |
| 24 | + protected void exec() { |
| 25 | + def args = [ |
| 26 | + 'docker', 'buildx', 'build', |
| 27 | + '--platform', platform, |
| 28 | + '--build-arg', 'CONFIG_FILEPATH=/usr/share/data-prepper/config/data-prepper-config.yaml', |
| 29 | + '--build-arg', 'PIPELINE_FILEPATH=/usr/share/data-prepper/pipelines/pipelines.yaml', |
| 30 | + '-t', tag |
| 31 | + ] |
| 32 | + |
| 33 | + if (push) { |
| 34 | + args << '--push' |
| 35 | + } else if (!platform.contains(',')) { |
| 36 | + // Only use --load for single-arch builds |
| 37 | + args << '--load' |
| 38 | + } |
| 39 | + |
| 40 | + args << '.' |
| 41 | + |
| 42 | + commandLine args |
| 43 | + super.exec() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +tasks.register('dockerBuildxSetup', Exec) { |
| 48 | + commandLine 'docker', 'buildx', 'create', '--name', 'data-prepper-builder', '--use' |
| 49 | + ignoreExitValue = true |
| 50 | +} |
| 51 | + |
| 52 | +tasks.register('dockerPrepare') { |
| 53 | + dependsOn ':release:releasePrerequisites' |
| 54 | + supportedArchitectures.each { arch -> |
| 55 | + dependsOn ":release:archives:linux:linux${arch}DistTar" |
| 56 | + } |
| 57 | + |
| 58 | + doLast { |
| 59 | + supportedArchitectures.each { arch -> |
| 60 | + def archiveTask = project(':release:archives:linux').tasks.getByName("linux${arch}DistTar") |
| 61 | + def dockerArch = arch == 'x64' ? 'amd64' : arch |
| 62 | + def sourceFile = archiveTask.archiveFile.get().asFile |
| 63 | + def targetFile = new File("${buildDir}/docker", sourceFile.name.replace("-linux-${arch}", "-linux-${dockerArch}")) |
| 64 | + |
| 65 | + copy { |
| 66 | + from sourceFile |
| 67 | + into "${buildDir}/docker" |
| 68 | + rename { targetFile.name } |
| 69 | + } |
| 70 | + } |
| 71 | + copy { |
| 72 | + from "${project.projectDir}/config/default-data-prepper-config.yaml", |
| 73 | + "${project.projectDir}/config/default-keystore.p12", |
| 74 | + 'adoptium.repo', |
| 75 | + 'Dockerfile' |
| 76 | + into "${buildDir}/docker" |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +tasks.register('docker', DockerBuildxTask) { |
| 82 | + dependsOn dockerPrepare |
| 83 | + workingDir "${buildDir}/docker" |
| 84 | + |
| 85 | + def currentArch = System.getProperty("os.arch") |
| 86 | + platform = currentArch == 'aarch64' ? 'linux/arm64' : 'linux/amd64' |
| 87 | + tag = "${project.rootProject.name}:${project.version}" |
| 88 | + push = false |
| 89 | +} |
| 90 | + |
| 91 | +tasks.register('dockerMultiArchitecture', DockerBuildxTask) { |
| 92 | + dependsOn dockerPrepare, dockerBuildxSetup |
| 93 | + workingDir "${buildDir}/docker" |
| 94 | + |
| 95 | + platform = supportedArchitectures.collect { arch -> |
| 96 | + arch == 'x64' ? 'linux/amd64' : "linux/${arch}" |
| 97 | + }.join(',') |
| 98 | + tag = "${project.rootProject.name}:${project.version}" |
| 99 | + push = false |
8 | 100 | } |
9 | 101 |
|
10 | | -docker { |
11 | | - name "${project.rootProject.name}:${project.version}" |
12 | | - tag "${project.rootProject.name}", "${project.version}" |
13 | | - files project(':release:archives:linux').tasks.getByName('linuxx64DistTar').archiveFile.get().asFile |
14 | | - files "${project.projectDir}/config/default-data-prepper-config.yaml", "${project.projectDir}/config/default-keystore.p12" |
15 | | - files 'adoptium.repo' |
16 | | - buildArgs(['ARCHIVE_FILE' : project(':release:archives:linux').tasks.getByName('linuxx64DistTar').archiveFileName.get(), |
17 | | - 'ARCHIVE_FILE_UNPACKED' : project(':release:archives:linux').tasks.getByName('linuxx64DistTar').archiveFileName.get().replace('.tar.gz', ''), |
18 | | - 'CONFIG_FILEPATH' : '/usr/share/data-prepper/config/data-prepper-config.yaml', |
19 | | - 'PIPELINE_FILEPATH' : '/usr/share/data-prepper/pipelines/pipelines.yaml']) |
20 | | - dockerfile file('Dockerfile') |
| 102 | +tasks.register('dockerPush', Exec) { |
| 103 | + dependsOn docker |
| 104 | + |
| 105 | + commandLine 'docker', 'push', "${project.rootProject.name}:${project.version}" |
21 | 106 | } |
22 | 107 |
|
23 | | -dockerPrepare.dependsOn ':release:releasePrerequisites' |
24 | | -dockerPrepare.dependsOn ':release:archives:linux:linuxx64DistTar' |
25 | | -dockerPush.dependsOn docker |
| 108 | +tasks.register('dockerMultiArchitecturePush', DockerBuildxTask) { |
| 109 | + dependsOn dockerPrepare, dockerBuildxSetup |
| 110 | + workingDir "${buildDir}/docker" |
| 111 | + |
| 112 | + platform = supportedArchitectures.collect { arch -> |
| 113 | + arch == 'x64' ? 'linux/amd64' : "linux/${arch}" |
| 114 | + }.join(',') |
| 115 | + tag = project.findProperty('dockerRepository') ?: "${project.rootProject.name}:${project.version}" |
| 116 | + push = true |
| 117 | +} |
0 commit comments