Skip to content

Commit

Permalink
build: introduce support for reproducible builds (#1995) (#2038)
Browse files Browse the repository at this point in the history
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 <[email protected]>
(cherry picked from commit 6da253b)

Co-authored-by: Leonidas Spyropoulos <[email protected]>
  • Loading branch information
github-actions[bot] and inglor authored Feb 2, 2022
1 parent d8fb3d8 commit d715e18
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ allprojects {
javadoc.options.addStringOption('Xdoclint:all,-missing', '-quiet')
}

// support for reproducible builds
tasks.withType(AbstractArchiveTask).configureEach {
// ignore file timestamps
// be consistent in archive file order
preserveFileTimestamps = false
reproducibleFileOrder = true
}

project.afterEvaluate {
// Handle javadoc dependencies across projects. Order matters: the linksOffline for
// org.opensearch:opensearch must be the last one or all the links for the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void apply(Project project) {
params.setGradleJavaVersion(Jvm.current().getJavaVersion());
params.setGitRevision(gitInfo.getRevision());
params.setGitOrigin(gitInfo.getOrigin());
params.setBuildDate(ZonedDateTime.now(ZoneOffset.UTC));
params.setBuildDate(Util.getBuildDate(ZonedDateTime.now(ZoneOffset.UTC)));
params.setTestSeed(getTestSeed());
params.setIsCi(System.getenv("JENKINS_URL") != null);
params.setIsInternal(isInternal);
Expand Down
16 changes: 16 additions & 0 deletions buildSrc/src/main/java/org/opensearch/gradle/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Supplier;
Expand Down Expand Up @@ -187,4 +190,17 @@ public String toString() {
}
};
}

public static ZonedDateTime getBuildDate(ZonedDateTime defaultValue) {
final String sourceDateEpoch = System.getenv("SOURCE_DATE_EPOCH");
if (sourceDateEpoch != null) {
try {
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(sourceDateEpoch)), ZoneOffset.UTC);
} catch (NumberFormatException e) {
throw new GradleException("Sysprop [SOURCE_DATE_EPOCH] must be of type [long]", e);
}
} else {
return defaultValue;
}
}
}

0 comments on commit d715e18

Please sign in to comment.