Skip to content

Commit 847d55f

Browse files
authored
Merge pull request #1725 from fluxcd/build-no-cgo
Build controller without CGO
2 parents 912feb4 + 53fd7a2 commit 847d55f

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

Dockerfile

+7-32
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
ARG BASE_VARIANT=alpine
21
ARG GO_VERSION=1.23
32
ARG XX_VERSION=1.6.1
43

54
FROM --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.
1710
COPY --from=xx / /
1811

19-
# build-go-mod can still be cached at build platform architecture.
20-
FROM build-base AS build
21-
2212
ARG 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
2915
WORKDIR /workspace
3016

@@ -46,28 +32,17 @@ COPY internal/ internal/
4632
ARG TARGETPLATFORM
4733
ARG 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

6339
FROM alpine:3.21
6440

6541
ARG TARGETPLATFORM
6642
RUN 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

7247
USER 65534:65534
7348
ENTRYPOINT [ "source-controller" ]

config/testdata/git/large-repo.yaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
apiVersion: source.toolkit.fluxcd.io/v1beta1
1+
apiVersion: source.toolkit.fluxcd.io/v1
22
kind: GitRepository
33
metadata:
44
name: large-repo
55
spec:
66
interval: 10m
77
timeout: 2m
8-
url: https://github.com/hashgraph/hedera-mirror-node.git
8+
url: https://github.com/nodejs/node.git
99
ref:
1010
branch: main
11-
ignore: |
12-
/*
13-
!/charts

0 commit comments

Comments
 (0)