1- ARG  BASE_VARIANT=alpine
21ARG  GO_VERSION=1.23
32ARG  XX_VERSION=1.6.1
43
54FROM  --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
65
7- FROM  --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} AS gostable
8- 
9- FROM  gostable AS go-linux
10- 
11- #  Build-base consists of build platform dependencies and xx.
12- #  These will be used at current arch to yield execute the cross compilations.
13- FROM  go-${TARGETOS} AS build-base
14- 
15- RUN  apk add --no-cache clang lld
6+ #  Docker buildkit multi-arch build requires golang alpine
7+ FROM  --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS builder
168
9+ #  Copy the build utilities.
1710COPY  --from=xx / /
1811
19- #  build-go-mod can still be cached at build platform architecture.
20- FROM  build-base AS build
21- 
2212ARG  TARGETPLATFORM
2313
24- #  Some dependencies have to installed 
25- #  for the target platform: https://github.com/tonistiigi/xx#go--cgo
26- RUN  xx-apk add musl-dev gcc clang lld
27- 
2814#  Configure workspace
2915WORKDIR  /workspace
3016
@@ -46,28 +32,17 @@ COPY internal/ internal/
4632ARG  TARGETPLATFORM
4733ARG  TARGETARCH
4834
49- #  Reasons why CGO is in use:
50- #  - The SHA1 implementation (sha1cd) used by go-git depends on CGO for
51- #    performance reasons. See: https://github.com/pjbgf/sha1cd/issues/15
52- ENV  CGO_ENABLED=1
53- 
54- RUN  export CGO_LDFLAGS="-static -fuse-ld=lld"  && \
55-   xx-go build \
56-   -ldflags "-s -w"  \
57-   -tags 'netgo,osusergo,static_build'  \
58-   -o /source-controller -trimpath main.go;
59- 
60- #  Ensure that the binary was cross-compiled correctly to the target platform.
61- RUN  xx-verify --static /source-controller
35+ #  build without specifing the arch
36+ ENV  CGO_ENABLED=0
37+ RUN  xx-go build -trimpath -a -o source-controller main.go
6238
6339FROM  alpine:3.21
6440
6541ARG  TARGETPLATFORM
6642RUN  apk --no-cache add ca-certificates \
6743  && update-ca-certificates
6844
69- #  Copy over binary from build
70- COPY  --from=build /source-controller /usr/local/bin/
45+ COPY  --from=builder /workspace/source-controller /usr/local/bin/
7146
7247USER  65534:65534
7348ENTRYPOINT  [ "source-controller"  ]
0 commit comments