From 319e1e343d447fbf8acd51d049970ed30a7ea3e0 Mon Sep 17 00:00:00 2001 From: nick-tan Date: Sun, 6 Mar 2022 13:38:10 -0800 Subject: [PATCH 1/2] ci: build multi arch images with buildx --- .github/workflows/build_image.yml | 37 +++++++++++++++++++++++++++++++ Dockerfile | 9 ++++++-- Makefile | 10 ++++----- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/build_image.yml diff --git a/.github/workflows/build_image.yml b/.github/workflows/build_image.yml new file mode 100644 index 0000000..c1643ef --- /dev/null +++ b/.github/workflows/build_image.yml @@ -0,0 +1,37 @@ +name: Build Image + +on: + push: + branches: + - 'master' + tags: + - "*" + +jobs: + bake: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + buildkitd-flags: --debug + config-inline: | + [registry."docker.io"] + mirrors = ["mirror.gcr.io"] + - name: Build image + run: | + make docker + - name: Login to DockerHub + uses: docker/login-action@v1 + if: ${{ startsWith(github.ref, 'refs/tags/') }} + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Pubish to Dockerhub + if: ${{ startsWith(github.ref, 'refs/tags/') }} + run: | + make release diff --git a/Dockerfile b/Dockerfile index 782b980..2f5f2d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,16 @@ -FROM golang:1.16.0 AS BUILDER +FROM --platform=$TARGETPLATFORM golang:1.16.0 AS BUILDER + +ARG TARGETPLATFORM +ARG TARGETARCH +ARG BUILDPLATFORM + WORKDIR /go/src/github.com/jtblin/kube2iam ENV ARCH=linux ENV CGO_ENABLED=0 COPY . ./ RUN make setup && make build -FROM alpine:3.14.3 +FROM --platform=$TARGETPLATFORM alpine:3.14.3 RUN apk --no-cache add \ ca-certificates \ iptables diff --git a/Makefile b/Makefile index a5a239a..5ef64c8 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,8 @@ GIT_HASH := $$(git rev-parse --short HEAD) GOBUILD_VERSION_ARGS := -ldflags "-s -X $(VERSION_VAR)=$(REPO_VERSION) -X $(GIT_VAR)=$(GIT_HASH) -X $(BUILD_DATE_VAR)=$(BUILD_DATE)" # useful for other docker repos DOCKER_REPO ?= jtblin -CPU_ARCH ?= amd64 -IMAGE_NAME := $(DOCKER_REPO)/$(BINARY_NAME)-$(CPU_ARCH) +CPU_ARCH ?= linux/arm64/v8,linux/amd64 +IMAGE_NAME := $(DOCKER_REPO)/$(BINARY_NAME) MANIFEST_NAME := $(DOCKER_REPO)/$(BINARY_NAME) ARCH ?= darwin GOLANGCI_LINT_VERSION ?= v1.23.8 @@ -74,16 +74,14 @@ check-all: travis-checks: build test-race check bench-race docker: - docker build -t $(IMAGE_NAME):$(GIT_HASH) . $(DOCKER_BUILD_FLAGS) + docker buildx build --progress=plain --platform $(CPU_ARCH) -t $(IMAGE_NAME):$(GIT_HASH) . $(DOCKER_BUILD_FLAGS) docker-dev: docker docker tag $(IMAGE_NAME):$(GIT_HASH) $(IMAGE_NAME):dev docker push $(IMAGE_NAME):dev release: check test docker - docker push $(IMAGE_NAME):$(GIT_HASH) - docker tag $(IMAGE_NAME):$(GIT_HASH) $(IMAGE_NAME):$(REPO_VERSION) - docker push $(IMAGE_NAME):$(REPO_VERSION) + docker buildx build --push --progress=plain --platform $(CPU_ARCH) -t $(IMAGE_NAME):$(GIT_HASH) . $(DOCKER_BUILD_FLAGS) ifeq (, $(findstring -rc, $(REPO_VERSION))) docker tag $(IMAGE_NAME):$(GIT_HASH) $(IMAGE_NAME):latest docker push $(IMAGE_NAME):latest From 847cb1ea9d04c0401dec00f58fe2eab8b37c5d4a Mon Sep 17 00:00:00 2001 From: Nick Tan Date: Tue, 17 May 2022 13:22:38 -0700 Subject: [PATCH 2/2] ci: build multi arch images with buildx --- Dockerfile | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2f5f2d4..782b980 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,11 @@ -FROM --platform=$TARGETPLATFORM golang:1.16.0 AS BUILDER - -ARG TARGETPLATFORM -ARG TARGETARCH -ARG BUILDPLATFORM - +FROM golang:1.16.0 AS BUILDER WORKDIR /go/src/github.com/jtblin/kube2iam ENV ARCH=linux ENV CGO_ENABLED=0 COPY . ./ RUN make setup && make build -FROM --platform=$TARGETPLATFORM alpine:3.14.3 +FROM alpine:3.14.3 RUN apk --no-cache add \ ca-certificates \ iptables