Skip to content

chore: update Makefile for build multi architecture container image #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ jobs:
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out

build_image:
name: build image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- run: |
make image-local-build
2 changes: 1 addition & 1 deletion .github/workflows/example-e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
ubuntu:
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-22.04, ubuntu-latest ]
os: [ ubuntu-22.04, ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
15 changes: 10 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
FROM --platform=${BUILDPLATFORM} golang:1.23 AS build
ARG BUILDER_IMAGE=golang:1.23
ARG BASE_IMAGE=debian:11-slim

# Build the manager binary
FROM --platform=${BUILDPLATFORM} ${BUILDER_IMAGE} AS builder

COPY / /src
WORKDIR /src

Expand All @@ -9,13 +14,12 @@ WORKDIR /src
ARG TARGETOS
ARG TARGETARCH

ENV CGO_ENABLED=0
RUN --mount=type=cache,target=/go/pkg --mount=type=cache,target=/root/.cache/go-build GOARCH=${TARGETARCH} GOOS=linux CGO_ENABLED=0 make build

RUN --mount=type=cache,target=/go/pkg --mount=type=cache,target=/root/.cache/go-build GOOS=${TARGETOS} GOARCH=${TARGETARCH} make build
FROM ${BASE_IMAGE}

FROM debian:11-slim AS image
COPY --from=builder /src/bin/kcl /usr/local/bin/kcl

COPY --from=build /src/bin/kcl /usr/local/bin/kcl
# Verify KCL installation and basic functionality
RUN kcl version && \
echo 'a=1' | kcl run -
Expand All @@ -25,6 +29,7 @@ RUN kcl version && \
RUN apt-get update && \
apt-get install -y --no-install-recommends git ca-certificates && \
rm -rf /var/lib/apt/lists/*

# Create the temporary directory
RUN mkdir -p /tmp

Expand Down
52 changes: 51 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lint:
.PHONY: build
build: lint
mkdir -p bin/
go build -o bin/kcl -ldflags="$(LDFLAGS)" $(MAIN_FILE)
$(GO_BUILD_ENV) go build -o bin/kcl -ldflags="$(LDFLAGS)" $(MAIN_FILE)

.PHONY: test
test:
Expand Down Expand Up @@ -92,3 +92,53 @@ e2e: ## Run e2e test
.PHONY: e2e-init
e2e-init:
scripts/e2e/e2e-init.sh $(TS)

GO_VERSION := $(shell awk '/^go /{print $$2}' go.mod|head -n1)

GIT_TAG ?= $(shell git describe --tags --dirty --always)
# Image URL to use all building/pushing image targets
PLATFORMS ?= linux/amd64,linux/arm64
DOCKER_BUILDX_CMD ?= docker buildx
IMAGE_BUILD_CMD ?= $(DOCKER_BUILDX_CMD) build
BASE_IMAGE ?= debian:11-slim
BUILDER_IMAGE ?= golang:$(GO_VERSION)
CGO_ENABLED ?= 0

IMAGE_BUILD_EXTRA_OPTS ?=

IMAGE_REGISTRY ?= ghcr.io/kcl-lang
IMAGE_NAME := kcl
IMAGE_REPO ?= $(IMAGE_REGISTRY)/$(IMAGE_NAME)
IMAGE_TAG ?= $(IMAGE_REPO):$(GIT_TAG)

ifdef EXTRA_TAG
IMAGE_EXTRA_TAG ?= $(IMAGE_REPO):$(EXTRA_TAG)
endif
ifdef IMAGE_EXTRA_TAG
IMAGE_BUILD_EXTRA_OPTS += -t $(IMAGE_EXTRA_TAG)
endif

# Build the multiplatform container image locally and push to repo.
.PHONY: image-local-push
image-local-push: PUSH=--push
image-local-push: image-local-build

# Build the multiplatform container image locally.
.PHONY: image-local-build
image-local-build:
BUILDER=$(shell $(DOCKER_BUILDX_CMD) create --use)
$(MAKE) image-build PUSH=$(PUSH)
$(DOCKER_BUILDX_CMD) rm $$BUILDER

.PHONY: image-push
image-push: PUSH=--push
image-push: image-build

image-build:
$(IMAGE_BUILD_CMD) -t $(IMAGE_TAG) \
--platform=$(PLATFORMS) \
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
--build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) \
--build-arg CGO_ENABLED=$(CGO_ENABLED) \
$(PUSH) \
$(IMAGE_BUILD_EXTRA_OPTS) ./
Comment on lines +96 to +144
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.