Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tasks to build Docker build context artifacts #41819

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 36 additions & 39 deletions distribution/docker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,17 @@ dependencies {
ossDockerSource project(path: ":distribution:archives:oss-linux-tar")
}

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

/*
* We need to be able to render a Dockerfile that references the official artifacts on https://artifacts.elastic.co. For this, we use a
* substitution in the Dockerfile template where we can either replace source_elasticsearch with a COPY from the Docker build context, or
* a RUN curl command to retrieve the artifact from https://artifacts.elastic.co. The system property build.docker.source, which can be
* either "local" (default) or "remote" controls which version of the Dockerfile is produced.
*/
private static boolean local() {
final String buildDockerSource = System.getProperty("build.docker.source")
if (buildDockerSource == null || "local".equals(buildDockerSource)) {
return true
} else if ("remote".equals(buildDockerSource)) {
return false
} else {
throw new IllegalArgumentException("expected build.docker.source to be [local] or [remote] but was [" + buildDockerSource + "]")
}
}

private static String files(final boolean oss) {
return "build/${ oss ? 'oss-' : ''}docker"
}
Expand All @@ -53,10 +36,8 @@ private static String taskName(final String prefix, final boolean oss, final Str
return "${prefix}${oss ? 'Oss' : ''}${suffix}"
}

void addCopyDockerContextTask(final boolean oss) {
task(taskName("copy", oss, "DockerContext"), type: Sync) {
into files(oss)

CopySpec dockerBuildContext(final boolean oss, final boolean local) {
copySpec {
into('bin') {
from 'src/docker/bin'
}
Expand All @@ -65,27 +46,26 @@ void addCopyDockerContextTask(final boolean oss) {
from 'src/docker/config'
}

if (local()) {
if (oss) {
from configurations.ossDockerSource
} else {
from configurations.dockerSource
}

from configurations.dockerPlugins
from('src/docker/Dockerfile') {
MavenFilteringHack.filter(it, expansions(oss, local))
}
}
}

void addCopyDockerfileTask(final boolean oss) {
task(taskName("copy", oss, "Dockerfile"), type: Copy) {
dependsOn taskName("copy", oss, "DockerContext")
inputs.properties(expansions(oss)) // ensure task is run when ext.expansions is changed
void addCopyDockerContextTask(final boolean oss) {
task(taskName("copy", oss, "DockerContext"), type: Sync) {
inputs.properties(expansions(oss, true))
into files(oss)

from('src/docker/Dockerfile') {
MavenFilteringHack.filter(it, expansions(oss))
with dockerBuildContext(oss, true)

if (oss) {
from configurations.ossDockerSource
} else {
from configurations.dockerSource
}

from configurations.dockerPlugins
}
}

Expand All @@ -104,7 +84,6 @@ check.dependsOn postProcessFixture
void addBuildDockerImage(final boolean oss) {
final Task buildDockerImageTask = task(taskName("build", oss, "DockerImage"), type: LoggedExec) {
dependsOn taskName("copy", oss, "DockerContext")
dependsOn taskName("copy", oss, "Dockerfile")
List<String> tags
if (oss) {
tags = [
Expand All @@ -130,9 +109,27 @@ void addBuildDockerImage(final boolean oss) {
BuildPlugin.requireDocker(buildDockerImageTask)
}

Closure commonDockerBuildContextConfig(final boolean oss) {
return {
extension = 'tar.gz'
compression = Compression.GZIP
archiveClassifier = "docker-build-context"
destinationDir = file("${oss ? "oss-" : ""}docker-build-context/build/distributions")
archiveBaseName = "elasticsearch${oss ? "-oss" : ""}"
with dockerBuildContext(oss, false)
}
}

task buildDockerBuildContext(type: Tar) {
configure(commonDockerBuildContextConfig(false))
}

task buildOssDockerBuildContext(type: Tar) {
configure(commonDockerBuildContextConfig(true))
}

for (final boolean oss : [false, true]) {
addCopyDockerContextTask(oss)
addCopyDockerfileTask(oss)
addBuildDockerImage(oss)
}

Expand Down
3 changes: 3 additions & 0 deletions distribution/docker/docker-build-context/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apply plugin: 'base'

assemble.dependsOn ":distribution:docker:buildDockerBuildContext"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding new projects for thees ?
I'm not sure what value these bring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atorok It's the same structure as all of our other projects in distribution that produce artifacts, the only difference being there are explicit Gradle build files here instead of being wired up through Gradle magic in a root Gradle build file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the explanation!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we have individual projects is so each of those projects has the one artifact as the default artifact. Here we aren't actually consuming this build context anywhere in tests that need to depend on one or the other right? In fact, we can likely get rid of the layers we have distribution now that we don't use dependency substitution anymore for them.

The thing that gives me pause most is having a hard dependency back to the :distribution:docker project on 2 tasks there. I don't see any benefit to running :distribution:docker:docker-build-context:assemble instead of :distribution:docker:buildDockerBuildContext

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjernst I pushed a commit that I think addresses your concerns. The back dependency is gone.

3 changes: 3 additions & 0 deletions distribution/docker/oss-docker-build-context/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apply plugin: 'base'

assemble.dependsOn ":distribution:docker:buildOssDockerBuildContext"
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ List projects = [
'distribution:archives:oss-no-jdk-linux-tar',
'distribution:archives:no-jdk-linux-tar',
'distribution:docker',
'distribution:docker:oss-docker-build-context',
'distribution:docker:docker-build-context',
'distribution:packages:oss-deb',
'distribution:packages:deb',
'distribution:packages:oss-no-jdk-deb',
Expand Down