Skip to content

Commit f25b10a

Browse files
marko-bekhtasebersole
authored andcommitted
Make the build reproducible and add a check
1 parent cd70d95 commit f25b10a

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
6+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
7+
8+
name: Java CI Reproducible build check
9+
10+
on:
11+
push:
12+
branches: [ "main" ]
13+
pull_request:
14+
branches: [ "main" ]
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
build:
21+
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- uses: actions/checkout@v3
26+
- name: Set up JDK 17
27+
uses: actions/setup-java@v3
28+
with:
29+
java-version: '17'
30+
distribution: 'temurin'
31+
- name: Setup Gradle
32+
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
33+
- name: Check artifacts
34+
shell: bash
35+
run: |
36+
./gradlew --no-daemon clean publishToMavenLocal --no-build-cache -Dmaven.repo.local=${GITHUB_WORKSPACE}/build1
37+
./gradlew --no-daemon clean publishToMavenLocal --no-build-cache -Dmaven.repo.local=${GITHUB_WORKSPACE}/build2
38+
./ci/compare-build-results.sh ${GITHUB_WORKSPACE}/build1 ${GITHUB_WORKSPACE}/build2

buildSrc/src/main/groovy/java-module.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ tasks.withType( JavaCompile ).configureEach {javaCompile->
5050
options.warnings false
5151
}
5252

53+
// To force the build produce the same byte-for-byte archives and hence make Hibernate Models build reproducible.
54+
// See also https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives
55+
tasks.withType( AbstractArchiveTask ).configureEach {
56+
preserveFileTimestamps = false
57+
reproducibleFileOrder = true
58+
}
5359

5460
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5561
// Javadoc
@@ -150,4 +156,4 @@ tasks.named( "check" ) {
150156
dependsOn enforceRulesTask
151157
dependsOn tasks.named( "spotlessCheck" )
152158
dependsOn jacocoReportTask
153-
}
159+
}

ci/compare-build-results.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
# This is a simple script to check if builds are reproducible. The steps are:
4+
# 1. Build Hibernate Models with `./gradlew --no-daemon clean publishToMavenLocal --no-build-cache -Dmaven.repo.local=some-path/out/build1`
5+
# 2. Build Hibernate Models with `./gradlew --no-daemon clean publishToMavenLocal --no-build-cache -Dmaven.repo.local=some-path/out/build2` second time pointing to a different local maven repository to publish
6+
# 3. Compare the build results with sh ./ci/compare-build-results.sh some-path/out/build1 some-path/out/build2
7+
# 4. The generated .buildcompare file will also contain the diffscope commands to see/compare the problematic build artifacts
8+
set -xv
9+
10+
outputDir1=$1
11+
outputDir2=$2
12+
outputDir1=${outputDir1%/}
13+
outputDir2=${outputDir2%/}
14+
15+
ok=()
16+
okFiles=()
17+
ko=()
18+
koFiles=()
19+
20+
for f in `find ${outputDir1} -type f | grep -v "javadoc.jar$" | grep -v "maven-metadata-local.xml$" | sort`
21+
do
22+
flocal=${f#$outputDir1}
23+
# echo "comparing ${flocal}"
24+
sha1=`shasum -a 512 $f | cut -f 1 -d ' '`
25+
sha2=`shasum -a 512 $outputDir2$flocal | cut -f 1 -d ' '`
26+
# echo "$sha1"
27+
# echo "$sha2"
28+
if [ "$sha1" = "$sha2" ]; then
29+
ok+=($flocal)
30+
okFiles+=(${flocal##*/})
31+
else
32+
ko+=($flocal)
33+
koFiles+=(${flocal##*/})
34+
fi
35+
done
36+
37+
# generate .buildcompare
38+
buildcompare=".buildcompare"
39+
echo "ok=${#ok[@]}" >> ${buildcompare}
40+
echo "ko=${#ko[@]}" >> ${buildcompare}
41+
echo "okFiles=\"${okFiles[@]}\"" >> ${buildcompare}
42+
echo "koFiles=\"${koFiles[@]}\"" >> ${buildcompare}
43+
echo "" >> ${buildcompare}
44+
echo "# see what caused the mismatch in the checksum by executing the following diffscope commands" >> ${buildcompare}
45+
for f in ${ko[@]}
46+
do
47+
echo "# diffoscope $outputDir1$f $outputDir2$f" >> ${buildcompare}
48+
done
49+
50+
if [ ${#ko[@]} -eq 0 ]; then
51+
exit 0
52+
else
53+
exit 1
54+
fi

0 commit comments

Comments
 (0)