|
| 1 | +# We use the Go 1.22 version unless asked to use something else. |
| 2 | +# The GitHub Actions CI job sets this argument for a consistent Go version. |
1 | 3 | ARG GO_VERSION=1.22 |
| 4 | +ARG BASE_IMAGE=kcllang/kcl |
2 | 5 |
|
| 6 | +# Setup the base environment. The BUILDPLATFORM is set automatically by Docker. |
| 7 | +# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using |
| 8 | +# the OS and architecture of the host running the build, not the OS and |
| 9 | +# architecture that we're building the function for. |
3 | 10 | FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build |
4 | 11 |
|
5 | 12 | COPY / /src |
6 | 13 | WORKDIR /src |
7 | 14 |
|
| 15 | +ENV CGO_ENABLED=0 |
| 16 | + |
| 17 | +# We run go mod download in a separate step so that we can cache its results. |
| 18 | +# This lets us avoid re-downloading modules if we don't need to. The type=target |
| 19 | +# mount tells Docker to mount the current directory read-only in the WORKDIR. |
| 20 | +# The type=cache mount tells Docker to cache the Go modules cache across builds. |
| 21 | +RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download |
| 22 | + |
8 | 23 | # The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to |
9 | 24 | # these values to ask Go to compile a binary for these architectures. If |
10 | 25 | # TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile |
11 | 26 | # for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine). |
12 | 27 | ARG TARGETOS |
13 | 28 | ARG TARGETARCH |
14 | 29 |
|
15 | | -# build |
16 | | -ENV CGO_ENABLED=0 |
17 | | -RUN go build -a -o kcl-controller cmd/main.go |
18 | | - |
19 | | -FROM kcllang/kcl |
| 30 | +# Build the function binary. The type=target mount tells Docker to mount the |
| 31 | +# current directory read-only in the WORKDIR. The type=cache mount tells Docker |
| 32 | +# to cache the Go modules cache across builds. |
| 33 | +RUN --mount=target=. \ |
| 34 | + --mount=type=cache,target=/go/pkg/mod \ |
| 35 | + --mount=type=cache,target=/root/.cache/go-build \ |
| 36 | + GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o kcl-controller cmd/main.go |
20 | 37 |
|
| 38 | +FROM ${BASE_IMAGE} as image |
21 | 39 | RUN apt-get update && apt-get install -y ca-certificates tini |
22 | | - |
23 | 40 | COPY --from=build /src/kcl-controller /usr/local/bin/ |
24 | | - |
25 | 41 | # RUN addgroup -S controller && adduser -S controller -G controller |
26 | 42 | RUN groupadd controller && useradd -g controller controller |
27 | 43 |
|
|
0 commit comments