Skip to content

Commit 6da253b

Browse files
authored
build: introduce support for reproducible builds (opensearch-project#1995)
Reproducible builds is an initiative to create an independently-verifiable path from source to binary code [1]. This can be done by: - Make all archive tasks in gradle reproducible by ignoring timestamp on files [2] - Preserve the order in side the archives [2] - Ensure GlobalBuildInfoPlugin.java use [SOURCE_DATE_EPOCH] when available [SOURCE_DATE_EPOCH]: https://reproducible-builds.org/docs/source-date-epoch/ [1]: https://reproducible-builds.org/ [2]: https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives Signed-off-by: Leonidas Spyropoulos <artafinde@gmail.com>
1 parent 1f9517c commit 6da253b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ allprojects {
274274
javadoc.options.addStringOption('Xdoclint:all,-missing', '-quiet')
275275
}
276276

277+
// support for reproducible builds
278+
tasks.withType(AbstractArchiveTask).configureEach {
279+
// ignore file timestamps
280+
// be consistent in archive file order
281+
preserveFileTimestamps = false
282+
reproducibleFileOrder = true
283+
}
284+
277285
project.afterEvaluate {
278286
// Handle javadoc dependencies across projects. Order matters: the linksOffline for
279287
// org.opensearch:opensearch must be the last one or all the links for the

buildSrc/src/main/java/org/opensearch/gradle/info/GlobalBuildInfoPlugin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void apply(Project project) {
121121
params.setGradleJavaVersion(Jvm.current().getJavaVersion());
122122
params.setGitRevision(gitInfo.getRevision());
123123
params.setGitOrigin(gitInfo.getOrigin());
124-
params.setBuildDate(ZonedDateTime.now(ZoneOffset.UTC));
124+
params.setBuildDate(Util.getBuildDate(ZonedDateTime.now(ZoneOffset.UTC)));
125125
params.setTestSeed(getTestSeed());
126126
params.setIsCi(System.getenv("JENKINS_URL") != null);
127127
params.setIsInternal(isInternal);

buildSrc/src/main/java/org/opensearch/gradle/util/Util.java

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
import java.io.UncheckedIOException;
4949
import java.net.URI;
5050
import java.net.URISyntaxException;
51+
import java.time.Instant;
52+
import java.time.ZoneOffset;
53+
import java.time.ZonedDateTime;
5154
import java.util.Locale;
5255
import java.util.Optional;
5356
import java.util.function.Supplier;
@@ -187,4 +190,17 @@ public String toString() {
187190
}
188191
};
189192
}
193+
194+
public static ZonedDateTime getBuildDate(ZonedDateTime defaultValue) {
195+
final String sourceDateEpoch = System.getenv("SOURCE_DATE_EPOCH");
196+
if (sourceDateEpoch != null) {
197+
try {
198+
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(sourceDateEpoch)), ZoneOffset.UTC);
199+
} catch (NumberFormatException e) {
200+
throw new GradleException("Sysprop [SOURCE_DATE_EPOCH] must be of type [long]", e);
201+
}
202+
} else {
203+
return defaultValue;
204+
}
205+
}
190206
}

0 commit comments

Comments
 (0)