Skip to content

Commit

Permalink
refactor: simpler dockerfile builder
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 20, 2024
1 parent 953e32c commit 49b103f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ jobs:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
platforms: linux/amd64, linux/arm64
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
30 changes: 23 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
# We use the Go 1.22 version unless asked to use something else.
# The GitHub Actions CI job sets this argument for a consistent Go version.
ARG GO_VERSION=1.22
ARG BASE_IMAGE=kcllang/kcl

# Setup the base environment. The BUILDPLATFORM is set automatically by Docker.
# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using
# the OS and architecture of the host running the build, not the OS and
# architecture that we're building the function for.
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build

COPY / /src
WORKDIR /src

ENV CGO_ENABLED=0

# We run go mod download in a separate step so that we can cache its results.
# This lets us avoid re-downloading modules if we don't need to. The type=target
# mount tells Docker to mount the current directory read-only in the WORKDIR.
# The type=cache mount tells Docker to cache the Go modules cache across builds.
RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download

# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to
# these values to ask Go to compile a binary for these architectures. If
# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile
# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine).
ARG TARGETOS
ARG TARGETARCH

# build
ENV CGO_ENABLED=0
RUN go build -a -o kcl-controller cmd/main.go

FROM kcllang/kcl
# Build the function binary. The type=target mount tells Docker to mount the
# current directory read-only in the WORKDIR. The type=cache mount tells Docker
# to cache the Go modules cache across builds.
RUN --mount=target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o kcl-controller cmd/main.go

FROM ${BASE_IMAGE} as image
RUN apt-get update && apt-get install -y ca-certificates tini

COPY --from=build /src/kcl-controller /usr/local/bin/

# RUN addgroup -S controller && adduser -S controller -G controller
RUN groupadd controller && useradd -g controller controller

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ nginx-deployment-1 1/1 1 0 4s
```

kcl-controller creates a `nginx-deployment-1` according to the KCL program in the repository.

git tag v0.3.1
git push origin v0.3.1
gh release create v0.3.1 --draft --generate-notes --title "v0.3.1 Release"

0 comments on commit 49b103f

Please sign in to comment.