Skip to content

Commit 30da73f

Browse files
authored
Add tasks to build Docker build context artifacts (#41819)
This commit adds some tasks to generate dedicated Docker build context artifacts.
1 parent 354118f commit 30da73f

File tree

4 files changed

+48
-44
lines changed

4 files changed

+48
-44
lines changed

distribution/docker/build.gradle

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,17 @@ dependencies {
1717
ossDockerSource project(path: ":distribution:archives:oss-linux-tar")
1818
}
1919

20-
ext.expansions = { oss ->
20+
ext.expansions = { oss, local ->
2121
final String classifier = 'linux-x86_64'
2222
final String elasticsearch = oss ? "elasticsearch-oss-${VersionProperties.elasticsearch}-${classifier}.tar.gz" : "elasticsearch-${VersionProperties.elasticsearch}-${classifier}.tar.gz"
2323
return [
2424
'elasticsearch' : elasticsearch,
2525
'license' : oss ? 'Apache-2.0' : 'Elastic License',
26-
'source_elasticsearch': local() ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
26+
'source_elasticsearch': local ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
2727
'version' : VersionProperties.elasticsearch
2828
]
2929
}
3030

31-
/*
32-
* We need to be able to render a Dockerfile that references the official artifacts on https://artifacts.elastic.co. For this, we use a
33-
* substitution in the Dockerfile template where we can either replace source_elasticsearch with a COPY from the Docker build context, or
34-
* a RUN curl command to retrieve the artifact from https://artifacts.elastic.co. The system property build.docker.source, which can be
35-
* either "local" (default) or "remote" controls which version of the Dockerfile is produced.
36-
*/
37-
private static boolean local() {
38-
final String buildDockerSource = System.getProperty("build.docker.source")
39-
if (buildDockerSource == null || "local".equals(buildDockerSource)) {
40-
return true
41-
} else if ("remote".equals(buildDockerSource)) {
42-
return false
43-
} else {
44-
throw new IllegalArgumentException("expected build.docker.source to be [local] or [remote] but was [" + buildDockerSource + "]")
45-
}
46-
}
47-
4831
private static String files(final boolean oss) {
4932
return "build/${ oss ? 'oss-' : ''}docker"
5033
}
@@ -53,39 +36,38 @@ private static String taskName(final String prefix, final boolean oss, final Str
5336
return "${prefix}${oss ? 'Oss' : ''}${suffix}"
5437
}
5538

56-
void addCopyDockerContextTask(final boolean oss) {
57-
task(taskName("copy", oss, "DockerContext"), type: Sync) {
58-
into files(oss)
59-
60-
into('bin') {
61-
from 'src/docker/bin'
62-
}
63-
64-
into('config') {
65-
from 'src/docker/config'
66-
}
39+
project.ext {
40+
dockerBuildContext = { boolean oss, boolean local ->
41+
copySpec {
42+
into('bin') {
43+
from project.projectDir.toPath().resolve("src/docker/bin")
44+
}
6745

68-
if (local()) {
69-
if (oss) {
70-
from configurations.ossDockerSource
71-
} else {
72-
from configurations.dockerSource
46+
into('config') {
47+
from project.projectDir.toPath().resolve("src/docker/config")
7348
}
7449

75-
from configurations.dockerPlugins
50+
from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) {
51+
MavenFilteringHack.filter(it, expansions(oss, local))
52+
}
7653
}
7754
}
7855
}
7956

80-
void addCopyDockerfileTask(final boolean oss) {
81-
task(taskName("copy", oss, "Dockerfile"), type: Copy) {
82-
dependsOn taskName("copy", oss, "DockerContext")
83-
inputs.properties(expansions(oss)) // ensure task is run when ext.expansions is changed
57+
void addCopyDockerContextTask(final boolean oss) {
58+
task(taskName("copy", oss, "DockerContext"), type: Sync) {
59+
inputs.properties(expansions(oss, true))
8460
into files(oss)
8561

86-
from('src/docker/Dockerfile') {
87-
MavenFilteringHack.filter(it, expansions(oss))
62+
with dockerBuildContext(oss, true)
63+
64+
if (oss) {
65+
from configurations.ossDockerSource
66+
} else {
67+
from configurations.dockerSource
8868
}
69+
70+
from configurations.dockerPlugins
8971
}
9072
}
9173

@@ -104,7 +86,6 @@ check.dependsOn postProcessFixture
10486
void addBuildDockerImage(final boolean oss) {
10587
final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) {
10688
dependsOn taskName("copy", oss, "DockerContext")
107-
dependsOn taskName("copy", oss, "Dockerfile")
10889
List<String> tags
10990
if (oss) {
11091
tags = [
@@ -132,7 +113,6 @@ void addBuildDockerImage(final boolean oss) {
132113

133114
for (final boolean oss : [false, true]) {
134115
addCopyDockerContextTask(oss)
135-
addCopyDockerfileTask(oss)
136116
addBuildDockerImage(oss)
137117
}
138118

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'base'
2+
3+
task buildDockerBuildContext(type: Tar) {
4+
extension = 'tar.gz'
5+
compression = Compression.GZIP
6+
archiveClassifier = "docker-build-context"
7+
archiveBaseName = "elasticsearch"
8+
with dockerBuildContext(false, false)
9+
}
10+
11+
assemble.dependsOn buildDockerBuildContext
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'base'
2+
3+
task buildOssDockerBuildContext(type: Tar) {
4+
extension = 'tar.gz'
5+
compression = Compression.GZIP
6+
archiveClassifier = "docker-build-context"
7+
archiveBaseName = "elasticsearch-oss"
8+
with dockerBuildContext(true, false)
9+
}
10+
11+
assemble.dependsOn buildOssDockerBuildContext

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ List projects = [
2929
'distribution:archives:oss-no-jdk-linux-tar',
3030
'distribution:archives:no-jdk-linux-tar',
3131
'distribution:docker',
32+
'distribution:docker:oss-docker-build-context',
33+
'distribution:docker:docker-build-context',
3234
'distribution:packages:oss-deb',
3335
'distribution:packages:deb',
3436
'distribution:packages:oss-no-jdk-deb',

0 commit comments

Comments
 (0)