| 1 |
FROM golang:1.24.6-trixie |
| 2 |
|
| 3 |
RUN set -eux; \ |
| 4 |
apt-get install --update -y --no-install-recommends \ |
| 5 |
arch-test \ |
| 6 |
file \ |
| 7 |
; \ |
| 8 |
apt-get dist-clean |
| 9 |
|
| 10 |
# https://github.com/tianon/fake-git |
| 11 |
# https://github.com/tianon/fake-git/commits/HEAD |
| 12 |
ENV FAKEGIT_COMMIT dc6774bbecc1f72de44d02bfd4385a4e6f45f807 |
| 13 |
RUN set -eux; \ |
| 14 |
git init /opt/fake-git; \ |
| 15 |
git -C /opt/fake-git fetch --depth 1 https://github.com/tianon/fake-git.git "$FAKEGIT_COMMIT:"; \ |
| 16 |
git -C /opt/fake-git checkout FETCH_HEAD; \ |
| 17 |
ln -svfT /opt/fake-git/fake-git.sh /usr/local/bin/git; \ |
| 18 |
hash -r; \ |
| 19 |
FAKEGIT_GO_SEMVER='v1.2.3' git --fake |
| 20 |
|
| 21 |
# note: we cannot add "-s" here because then "govulncheck" does not work (see SECURITY.md); the ~0.2MiB increase (as of 2022-12-16, Go 1.18) is worth it |
| 22 |
ENV BUILD_FLAGS="-v -trimpath -ldflags '-d -w' -buildvcs=true" |
| 23 |
|
| 24 |
# disable CGO for ALL THE THINGS (to help ensure no libc) |
| 25 |
ENV CGO_ENABLED 0 |
| 26 |
|
| 27 |
RUN set -eux; \ |
| 28 |
{ \ |
| 29 |
echo '#!/usr/bin/env bash'; \ |
| 30 |
echo 'set -Eeuo pipefail -x'; \ |
| 31 |
# this scrapes our raw version number out of "version.go" (which we then use as our "commit ref" so it's "vcs.revision" in our metadata, and "cross-grade" to semver below for our fake tag so Go thinks we have a version number worth including) |
| 32 |
echo 'FAKEGIT_GO_REVISION="$(grep -oEm1 "[0-9][0-9.+a-z-]+" version.go)"'; \ |
| 33 |
# validate our assumptions about the above version number |
| 34 |
echo 'grep <<<"$FAKEGIT_GO_REVISION" -E "^[0-9]+[.][0-9]+\$"'; \ |
| 35 |
# Go *requires* semver, which is silly, but outside our control, so this takes our version numbers like "1.2" and "cross-grades" them to be like "v1.2.0", per (Go's implementation of) semver (and the VCS implementation is even stricter and requires the full triplet) |
| 36 |
echo 'FAKEGIT_GO_SEMVER="v${FAKEGIT_GO_REVISION}.0"'; \ |
| 37 |
echo 'export FAKEGIT_GO_REVISION FAKEGIT_GO_SEMVER'; \ |
| 38 |
echo 'eval "go build $BUILD_FLAGS -o /go/bin/gosu-$ARCH" github.com/tianon/gosu'; \ |
| 39 |
echo 'if go version -m "/go/bin/gosu-$ARCH" |& tee "/proc/$$/fd/1" | grep "(devel)" >&2; then exit 1; fi'; \ |
| 40 |
echo 'file "/go/bin/gosu-$ARCH"'; \ |
| 41 |
echo 'if arch-test "$ARCH"; then'; \ |
| 42 |
# there's a fun QEMU + Go 1.18+ bug that causes our binaries (especially on ARM arches) to hang indefinitely *sometimes*, hence the "timeout" and looping here |
| 43 |
echo ' try() { for (( i = 0; i < 30; i++ )); do if timeout 1s "$@"; then return 0; fi; done; return 1; }'; \ |
| 44 |
echo ' try "/go/bin/gosu-$ARCH" --version'; \ |
| 45 |
echo ' try "/go/bin/gosu-$ARCH" nobody id'; \ |
| 46 |
echo ' try "/go/bin/gosu-$ARCH" nobody ls -l /proc/self/fd'; \ |
| 47 |
echo 'fi'; \ |
| 48 |
} > /usr/local/bin/gosu-build-and-test.sh; \ |
| 49 |
chmod +x /usr/local/bin/gosu-build-and-test.sh |
| 50 |
|
| 51 |
WORKDIR /go/src/github.com/tianon/gosu |
| 52 |
|
| 53 |
# satisfy Go's need for ".git" to invoke "git" (or in our case, "fake-git.sh") |
| 54 |
RUN mkdir .git # ("touch .git" should be enough here, but Go insists it be a directory even though Git worktrees are a thing and have ".git" as a file) |
| 55 |
|
| 56 |
COPY go.mod go.sum ./ |
| 57 |
RUN go mod download |
| 58 |
|
| 59 |
COPY *.go ./ |
| 60 |
|
| 61 |
# gosu-$(dpkg --print-architecture) |
| 62 |
RUN ARCH=amd64 GOARCH=amd64 gosu-build-and-test.sh |
| 63 |
RUN ARCH=i386 GOARCH=386 gosu-build-and-test.sh |
| 64 |
RUN ARCH=armel GOARCH=arm GOARM=5 gosu-build-and-test.sh |
| 65 |
RUN ARCH=armhf GOARCH=arm GOARM=6 gosu-build-and-test.sh |
| 66 |
#RUN ARCH=armhf GOARCH=arm GOARM=7 gosu-build-and-test.sh # boo Raspberry Pi, making life hard (armhf-is-v7 vs armhf-is-v6 ...) |
| 67 |
RUN ARCH=arm64 GOARCH=arm64 gosu-build-and-test.sh |
| 68 |
RUN ARCH=mips64el GOARCH=mips64le gosu-build-and-test.sh |
| 69 |
RUN ARCH=ppc64el GOARCH=ppc64le gosu-build-and-test.sh |
| 70 |
RUN ARCH=riscv64 GOARCH=riscv64 gosu-build-and-test.sh |
| 71 |
RUN ARCH=s390x GOARCH=s390x gosu-build-and-test.sh |
| 72 |
|
| 73 |
RUN set -eux; go version -m /go/bin/gosu-*; ls -lAFh /go/bin/gosu-*; file /go/bin/gosu-* |