From 540af6b0d9ce48ab449dd04cba87f0e047faf2db Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 8 Aug 2019 17:07:41 -0700 Subject: [PATCH] Make git revision loading lazy (#45358) This commit makes the gitRevision property a lazy loaded value by returning an Object implementing toString(). The Dockerfile template is also changed to use groovy templates instead of the mavenfilter hack, so converting to String will not happen until runtime. --- .../gradle/info/GlobalBuildInfoPlugin.java | 47 +++++++++++++------ distribution/docker/build.gradle | 9 ++-- distribution/docker/src/docker/Dockerfile | 8 ++-- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java index 1b6880277fe96..d3cf23468ec70 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -45,6 +46,8 @@ public void apply(Project project) { File compilerJavaHome = findCompilerJavaHome(); File runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome); + Object gitRevisionResolver = createGitRevisionResolver(project); + final List javaVersions = new ArrayList<>(); for (int version = 8; version <= Integer.parseInt(minimumCompilerVersion.getMajorVersion()); version++) { if (System.getenv(getJavaHomeEnvVarName(Integer.toString(version))) != null) { @@ -92,7 +95,7 @@ public void apply(Project project) { ext.set("minimumCompilerVersion", minimumCompilerVersion); ext.set("minimumRuntimeVersion", minimumRuntimeVersion); ext.set("gradleJavaVersion", Jvm.current().getJavaVersion()); - ext.set("gitRevision", gitRevision(project)); + ext.set("gitRevision", gitRevisionResolver); ext.set("buildDate", ZonedDateTime.now(ZoneOffset.UTC)); }); } @@ -203,21 +206,35 @@ private static int findDefaultParallel(Project project) { return _defaultParallel; } - private String gitRevision(final Project project) { - final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); - final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); - final ExecResult result = project.exec(spec -> { - spec.setExecutable("git"); - spec.setArgs(Arrays.asList("rev-parse", "HEAD")); - spec.setStandardOutput(stdout); - spec.setErrorOutput(stderr); - spec.setIgnoreExitValue(true); - }); + private Object createGitRevisionResolver(final Project project) { + return new Object() { + private final AtomicReference gitRevision = new AtomicReference<>(); + + @Override + public String toString() { + if (gitRevision.get() == null) { + final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + final ExecResult result = project.exec(spec -> { + spec.setExecutable("git"); + spec.setArgs(Arrays.asList("rev-parse", "HEAD")); + spec.setStandardOutput(stdout); + spec.setErrorOutput(stderr); + spec.setIgnoreExitValue(true); + }); + + final String revision; + if (result.getExitValue() != 0) { + revision = "unknown"; + } else { + revision = stdout.toString(UTF_8).trim(); + } + this.gitRevision.compareAndSet(null, revision); + } + return gitRevision.get(); + } + }; - if (result.getExitValue() != 0) { - return "unknown"; - } - return stdout.toString(UTF_8).trim(); } } diff --git a/distribution/docker/build.gradle b/distribution/docker/build.gradle index a4a45f4b7d659..f2c62689c608b 100644 --- a/distribution/docker/build.gradle +++ b/distribution/docker/build.gradle @@ -1,8 +1,7 @@ import org.elasticsearch.gradle.BuildPlugin import org.elasticsearch.gradle.LoggedExec -import org.elasticsearch.gradle.MavenFilteringHack import org.elasticsearch.gradle.VersionProperties -import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin +import org.elasticsearch.gradle.testfixtures.TestFixturesPlugin apply plugin: 'base' apply plugin: 'elasticsearch.test.fixtures' @@ -56,7 +55,7 @@ project.ext { } from(project.projectDir.toPath().resolve("src/docker/Dockerfile")) { - MavenFilteringHack.filter(it, expansions(oss, local)) + expand(expansions(oss, local)) } } } @@ -64,7 +63,9 @@ project.ext { void addCopyDockerContextTask(final boolean oss) { task(taskName("copy", oss, "DockerContext"), type: Sync) { - inputs.properties(expansions(oss, true)) + expansions(oss, true).each { k, v -> + inputs.property(k, { v.toString() }) + } into files(oss) with dockerBuildContext(oss, true) diff --git a/distribution/docker/src/docker/Dockerfile b/distribution/docker/src/docker/Dockerfile index 827471a7a26f0..82188eac69d4a 100644 --- a/distribution/docker/src/docker/Dockerfile +++ b/distribution/docker/src/docker/Dockerfile @@ -13,7 +13,7 @@ FROM centos:7 AS builder -ENV PATH /usr/share/elasticsearch/bin:$PATH +ENV PATH /usr/share/elasticsearch/bin:\$PATH RUN groupadd -g 1000 elasticsearch && \ adduser -u 1000 -g 1000 -d /usr/share/elasticsearch elasticsearch @@ -41,8 +41,8 @@ ENV ELASTIC_CONTAINER true RUN for iter in {1..10}; do yum update -y && \ yum install -y nc && \ - yum clean all && exit_code=0 && break || exit_code=$? && echo "yum error: retry $iter in 10s" && sleep 10; done; \ - (exit $exit_code) + yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \ + (exit \$exit_code) RUN groupadd -g 1000 elasticsearch && \ adduser -u 1000 -g 1000 -G 0 -d /usr/share/elasticsearch elasticsearch && \ @@ -57,7 +57,7 @@ COPY --from=builder --chown=1000:0 /usr/share/elasticsearch /usr/share/elasticse # REF: https://github.com/elastic/elasticsearch-docker/issues/171 RUN ln -sf /etc/pki/ca-trust/extracted/java/cacerts /usr/share/elasticsearch/jdk/lib/security/cacerts -ENV PATH /usr/share/elasticsearch/bin:$PATH +ENV PATH /usr/share/elasticsearch/bin:\$PATH COPY --chown=1000:0 bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh