Skip to content

Commit e086fb4

Browse files
committed
Use QEMU and "arch-test" to avoid bad binaries in the future
1 parent 50e26df commit e086fb4

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- uses: actions/checkout@v3
19+
- run: sudo apt-get update && sudo apt-get install -y --no-install-recommends binfmt-support qemu-user-static
1920
- run: ./build.sh
2021
- run: ./test.sh gosu-amd64
2122
- run: ./test.sh gosu-i386

Dockerfile

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
FROM golang:1.18.2-alpine3.14
1+
FROM golang:1.18.2-bullseye
22

3-
RUN apk add --no-cache file
3+
RUN set -eux; \
4+
apt-get update; \
5+
apt-get install -y --no-install-recommends \
6+
arch-test \
7+
file \
8+
; \
9+
rm -rf /var/lib/apt/lists/*
10+
11+
# 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
12+
ENV BUILD_FLAGS="-v -ldflags '-d -w'"
13+
14+
RUN set -eux; \
15+
{ \
16+
echo '#!/usr/bin/env bash'; \
17+
echo 'set -Eeuo pipefail -x'; \
18+
echo 'eval "go build $BUILD_FLAGS -o /go/bin/gosu-$ARCH"'; \
19+
echo 'file "/go/bin/gosu-$ARCH"'; \
20+
echo 'if arch-test "$ARCH"; then'; \
21+
# 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
22+
echo ' try() { for (( i = 0; i < 30; i++ )); do if timeout 1s "$@"; then return 0; fi; done; return 1; }'; \
23+
echo ' try "/go/bin/gosu-$ARCH" --version'; \
24+
echo ' try "/go/bin/gosu-$ARCH" nobody id'; \
25+
echo ' try "/go/bin/gosu-$ARCH" nobody ls -l /proc/self/fd'; \
26+
echo 'fi'; \
27+
} > /usr/local/bin/gosu-build-and-test.sh; \
28+
chmod +x /usr/local/bin/gosu-build-and-test.sh
429

530
# disable CGO for ALL THE THINGS (to help ensure no libc)
631
ENV CGO_ENABLED 0
@@ -12,57 +37,18 @@ RUN set -eux; \
1237
go mod download; \
1338
go mod verify
1439

15-
# 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
16-
ENV BUILD_FLAGS="-v -ldflags '-d -w'"
17-
1840
COPY *.go ./
1941

2042
# gosu-$(dpkg --print-architecture)
21-
RUN set -eux; \
22-
eval "GOARCH=amd64 go build $BUILD_FLAGS -o /go/bin/gosu-amd64"; \
23-
file /go/bin/gosu-amd64; \
24-
/go/bin/gosu-amd64 --version; \
25-
/go/bin/gosu-amd64 nobody id; \
26-
/go/bin/gosu-amd64 nobody ls -l /proc/self/fd
27-
28-
RUN set -eux; \
29-
eval "GOARCH=386 go build $BUILD_FLAGS -o /go/bin/gosu-i386"; \
30-
file /go/bin/gosu-i386; \
31-
/go/bin/gosu-i386 --version; \
32-
/go/bin/gosu-i386 nobody id; \
33-
/go/bin/gosu-i386 nobody ls -l /proc/self/fd
34-
35-
RUN set -eux; \
36-
eval "GOARCH=arm GOARM=5 go build $BUILD_FLAGS -o /go/bin/gosu-armel"; \
37-
file /go/bin/gosu-armel
38-
39-
RUN set -eux; \
40-
eval "GOARCH=arm GOARM=6 go build $BUILD_FLAGS -o /go/bin/gosu-armhf"; \
41-
file /go/bin/gosu-armhf
42-
43-
# boo Raspberry Pi, making life hard (armhf-is-v7 vs armhf-is-v6 ...)
44-
#RUN set -eux; \
45-
# eval "GOARCH=arm GOARM=7 go build $BUILD_FLAGS -o /go/bin/gosu-armhf"; \
46-
# file /go/bin/gosu-armhf
47-
48-
RUN set -eux; \
49-
eval "GOARCH=arm64 go build $BUILD_FLAGS -o /go/bin/gosu-arm64"; \
50-
file /go/bin/gosu-arm64
51-
52-
RUN set -eux; \
53-
eval "GOARCH=mips64le go build $BUILD_FLAGS -o /go/bin/gosu-mips64el"; \
54-
file /go/bin/gosu-mips64el
55-
56-
RUN set -eux; \
57-
eval "GOARCH=ppc64le go build $BUILD_FLAGS -o /go/bin/gosu-ppc64el"; \
58-
file /go/bin/gosu-ppc64el
59-
60-
RUN set -eux; \
61-
eval "GOARCH=riscv64 go build $BUILD_FLAGS -o /go/bin/gosu-riscv64"; \
62-
file /go/bin/gosu-riscv64
63-
64-
RUN set -eux; \
65-
eval "GOARCH=s390x go build $BUILD_FLAGS -o /go/bin/gosu-s390x"; \
66-
file /go/bin/gosu-s390x
43+
RUN ARCH=amd64 GOARCH=amd64 gosu-build-and-test.sh
44+
RUN ARCH=i386 GOARCH=386 gosu-build-and-test.sh
45+
RUN ARCH=armel GOARCH=arm GOARM=5 gosu-build-and-test.sh
46+
RUN ARCH=armhf GOARCH=arm GOARM=6 gosu-build-and-test.sh
47+
#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 ...)
48+
RUN ARCH=arm64 GOARCH=arm64 gosu-build-and-test.sh
49+
RUN ARCH=mips64el GOARCH=mips64le gosu-build-and-test.sh
50+
RUN ARCH=ppc64el GOARCH=ppc64le gosu-build-and-test.sh
51+
RUN ARCH=riscv64 GOARCH=riscv64 gosu-build-and-test.sh
52+
RUN ARCH=s390x GOARCH=s390x gosu-build-and-test.sh
6753

6854
RUN set -eux; ls -lAFh /go/bin/gosu-*; file /go/bin/gosu-*

0 commit comments

Comments
 (0)