1- FROM golang:1.16-buster as builder
2-
3- # Up-to-date libgit2 dependencies are only available in sid (unstable).
4- # The libgit2 dependencies must be listed here to be able to build on ARM64.
5- RUN echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list \
6- && echo "deb-src http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list
7- RUN set -eux; \
8- apt-get update \
9- && apt-get install -y libgit2-dev/unstable zlib1g-dev/unstable libssh2-1-dev/unstable libpcre3-dev/unstable \
10- && apt-get clean \
11- && apt-get autoremove --purge -y \
12- && rm -rf /var/lib/apt/lists/*
1+ ARG BASE_VARIANT=bullseye
2+ ARG GO_VERSION=1.16.8
3+ ARG XX_VERSION=1.0.0-rc.2
134
14- WORKDIR /workspace
5+ ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2
6+ ARG LIBGIT2_TAG=libgit2-1.1.1-1
157
16- # Copy the Go Modules manifests
17- COPY go.mod go.mod
18- COPY go.sum go.sum
8+ FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
9+ FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} as libgit2
10+
11+ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
12+ FROM --platform=$BUILDPLATFORM golang:1.17rc1-${BASE_VARIANT} AS golatest
13+
14+ FROM gostable AS go-linux
15+
16+ FROM go-${TARGETOS} AS build-base-bullseye
17+
18+ # Copy the build utiltiies
19+ COPY --from=xx / /
20+ COPY --from=libgit2 /Makefile /libgit2/
21+
22+ # Install the libgit2 build dependencies
23+ RUN make -C /libgit2 cmake
24+
25+ ARG TARGETPLATFORM
26+ RUN make -C /libgit2 dependencies
27+
28+ FROM build-base-${BASE_VARIANT} as libgit2-bullseye
29+
30+ # Compile and install libgit2
31+ ARG TARGETPLATFORM
32+ RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
33+
34+ FROM libgit2-${BASE_VARIANT} as build-bullseye
35+
36+ # Configure workspace
37+ WORKDIR /workspace
1938
2039# This has its own go.mod, which needs to be present so go mod
2140# download works.
2241COPY api/ api/
2342
43+ # Copy modules manifests
44+ COPY go.mod go.mod
45+ COPY go.sum go.sum
46+
2447# cache deps before building and copying source so that we don't need to re-download as much
2548# and so that source changes don't invalidate our downloaded layer
2649RUN go mod download
@@ -30,30 +53,43 @@ COPY main.go main.go
3053COPY pkg/ pkg/
3154COPY controllers/ controllers/
3255
33- # Build
34- RUN CGO_ENABLED=1 go build -o image-automation-controller main.go
56+ # Build the binary
57+ ENV CGO_ENABLED=1
58+ ARG TARGETPLATFORM
59+ RUN xx-go build -o image-automation-controller -trimpath \
60+ main.go
3561
36- FROM debian:buster-slim as controller
62+ FROM build-${BASE_VARIANT} as prepare-bullseye
3763
38- LABEL org.opencontainers.image.source="https://github.com/fluxcd/image-automation-controller"
64+ # Move libgit2 lib to generic and predictable location
65+ ARG TARGETPLATFORM
66+ RUN mkdir -p /libgit2/lib/ \
67+ && cp -d /usr/lib/$(xx-info triple)/libgit2.so* /libgit2/lib/
3968
40- # Up-to-date libgit2 dependencies are only available in
41- # unstable, as libssh2 in testing/bullseye has been linked
42- # against gcrypt which causes issues with PKCS* formats.
43- RUN echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list \
44- && echo "deb-src http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list
45- RUN set -eux; \
46- apt-get update \
47- && apt-get install -y ca-certificates libgit2-1.1 \
48- && apt-get clean \
49- && apt-get autoremove --purge -y \
50- && rm -rf /var/lib/apt/lists/*
69+ FROM prepare-${BASE_VARIANT} as build
5170
52- COPY --from=builder /workspace/image-automation-controller /usr/local/bin/
71+ FROM debian:${BASE_VARIANT}-slim as controller
5372
73+ # Configure user
5474RUN groupadd controller && \
5575 useradd --gid controller --shell /bin/sh --create-home controller
5676
57- USER controller
77+ # Copy libgit2
78+ COPY --from=build /libgit2/lib/ /usr/local/lib/
79+ RUN ldconfig
80+
81+ # Upgrade packages and install runtime dependencies
82+ RUN echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list \
83+ && echo "deb-src http://deb.debian.org/debian sid main" >> /etc/apt/sources.list \
84+ && apt update \
85+ && apt install --no-install-recommends -y zlib1g/sid libssl1.1/sid libssh2-1/sid \
86+ && apt install --no-install-recommends -y ca-certificates \
87+ && apt clean \
88+ && apt autoremove --purge -y \
89+ && rm -rf /var/lib/apt/lists/*
90+
91+ # Copy over binary from build
92+ COPY --from=build /workspace/image-automation-controller /usr/local/bin/
5893
94+ USER controller
5995ENTRYPOINT [ "image-automation-controller" ]
0 commit comments