#!/bin/bash # Check that a release builds to the same bytes twice. Both builds are this checkout's # scripts/release.sh, which runs the built commit's code only in its container, never here directly. # That code still runs, in a container only as isolated as the runtime keeps it, so an outside # commit, such as a pull request's, is checked only with the maintainer's go-ahead for that run. # A commit (HEAD by default) builds as release.sh --commit builds it, unsigned; # a tag, v, builds as its release does (release.sh --tag: the tag's checks, and no # signature), so its SHA256SUMS can be compared with the published one. # The second build differs from the first in what release.sh and .goreleaser.yaml keep out of the # bytes, so that losing either shows as a difference: # - it fetches the commit from a repository with a stray tag at it, v0.0.0-stray, which Go would # stamp into the binary as its version if the build saw it (a v0 version, as Go ignores a tag # whose major version the module's path doesn't name); # - it runs under Docker where the first runs under podman, when both are installed, so as you # where the first runs as root, which the archives would record. # And every file in each platform's archive has to be root's, with mode 0755 or 0644, whatever # either build ran as. It prints each file's SHA-256, says DIFFERS beside any file whose two builds # differ, and exits 1 when one does or an archive's headers aren't those. Both builds run on this # machine, in the same image and on its processor: to compare two machines, run it on each and # compare the printouts. Nothing is signed or published. CI's repro job runs it. # scripts/repro-check.sh [COMMIT | TAG] set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" die() { echo "repro-check: $*" >&2; exit 1; } case ${1:-} in -h | --help) awk 'NR > 1 && !/^#/ { exit } NR > 1 { sub(/^# ?/, ""); print }' "$0"; exit 0 ;; esac (( $# <= 1 )) || die "too many arguments: scripts/repro-check.sh [COMMIT | TAG]" arg=${1:-HEAD} commit=$(git -C "$root" rev-parse -q --verify --end-of-options "$arg^{commit}") || die "no commit $arg" how=(--commit "$commit") if [[ $arg == v* ]] && git -C "$root" rev-parse -q --verify "refs/tags/$arg" >/dev/null; then how=(--tag "$arg"); fi # The runtimes: podman, then Docker, when both are installed, else the one there is, or # DEVVM_RELEASE_RUNTIME's for both. if [[ -n ${DEVVM_RELEASE_RUNTIME:-} ]]; then rt1=$DEVVM_RELEASE_RUNTIME rt2=$rt1 elif command -v podman >/dev/null && command -v docker >/dev/null; then rt1=podman rt2=docker elif command -v podman >/dev/null; then rt1=podman rt2=podman elif command -v docker >/dev/null; then rt1=docker rt2=docker else die "needs podman or Docker to build in the release's container (DEVVM_RELEASE_RUNTIME names one)" fi sha256() { { sha256sum "$1" 2>/dev/null || shasum -a 256 "$1"; } | cut -c1-64; } tmp=$(mktemp -d "${TMPDIR:-/tmp}/devvm-repro.XXXXXX") trap 'rm -rf "$tmp"' EXIT # Build 2's repository, never checked out: the commit, the tag when it's one, and the stray tag. src=$tmp/src { git -c init.defaultBranch=build init -q --template= "$src" && git -C "$src" -c protocol.version=2 fetch -q --no-tags --update-shallow "$root" "$commit" && git -C "$src" tag v0.0.0-stray "$commit"; } || die "couldn't make build 2's repository" if [[ ${how[0]} == --tag ]]; then git -C "$src" fetch -q --no-tags "$root" "refs/tags/$arg:refs/tags/$arg" || die "couldn't fetch $arg" fi # build N RUNTIME REPO: build N, into $tmp/N, under RUNTIME, fetching from REPO; then what it ran # under, as release.sh says it: RUNTIME as UID:GID. A run that says nothing of it, or leaves no # files, failed, whatever its exit status. build() { local under echo "==> build $1 of $arg ($commit), under $2" >&2 env -u DEVVM_MINISIGN_KEY -u DEVVM_MINISIGN_PUB -u DEVVM_RELEASE_URL DEVVM_RELEASE_RUNTIME="$2" \ DEVVM_RELEASE_SRC="$3" DEVVM_RELEASE_DIST="$tmp/$1" "$root/scripts/release.sh" "${how[@]}" > "$tmp/$1.log" 2>&1 || { tail -40 "$tmp/$1.log" >&2; die "build $1 failed: its log is above"; } under=$(sed -n 's/^==> building .*, under //p' "$tmp/$1.log") [[ -n $under && -d $tmp/$1 ]] || { tail -40 "$tmp/$1.log" >&2; die "build $1 made no release: its log is above"; } echo "$under" } u1=$(build 1 "$rt1" "$root") u2=$(build 2 "$rt2" "$src") echo "==> build 1: under $u1, from this repository" echo "==> build 2: under $u2, from one with a stray tag, v0.0.0-stray, at the commit" if [[ ${u1##* as } == "${u2##* as }" ]]; then echo "(both ran as the same user: with podman and Docker both installed, they don't; the archives' headers are checked either way)" fi # headers ARCHIVE: each entry's mode and its owner/group, by name, then by number, from tar -tv: # GNU tar's listing has owner/group in one field, and bsdtar's a link count, then each. headers() { { tar -tvzf "$1" && tar --numeric-owner -tvzf "$1"; } | awk '{ if ($2 ~ /\//) print $1, $2; else print $1, $3 "/" $4 }' } # The release's files: what scripts/publish.sh uploads, and the formulas, in C's order on every # builder. a=$tmp/1 b=$tmp/2 files=$(cd "$a" && LC_ALL=C ls devvm-*.tar.gz SHA256SUMS install.sh devvm*.rb) [[ $(cd "$b" && LC_ALL=C ls devvm-*.tar.gz SHA256SUMS install.sh devvm*.rb) == "$files" ]] || die "the builds made different files" same=true root_owned=true while IFS= read -r f; do x=$(sha256 "$a/$f") y=$(sha256 "$b/$f") if [[ $x == "$y" ]]; then echo "$x $f" else echo "$x $f DIFFERS: build 2 made $y" same=false fi [[ $f =~ -(darwin|linux)-(arm64|amd64)\.tar\.gz$ ]] || continue for n in 1 2; do bad=$(headers "$tmp/$n/$f" | awk '!(($1 == "-rwxr-xr-x" || $1 == "-rw-r--r--" || $1 == "drwxr-xr-x") && ($2 == "root/root" || $2 == "0/0"))') || die "couldn't list $f" [[ -z $bad ]] || { echo " $f, build $n: not root's with mode 0755 or 0644: $(head -n 1 <<<"$bad")"; root_owned=false; } done done <<<"$files" $same || die "the two builds differ" $root_owned || die "an archive holds a file that isn't root's with mode 0755 or 0644" echo "==> both builds of $arg ($commit) made the same bytes"