Skip to content
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

Use tonistiigi/xx for cross-platform builds #178

Merged
merged 3 commits into from
Oct 27, 2021
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
28 changes: 27 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ jobs:
steps:
- name: checkout
uses: actions/checkout@v2
- name: Setup QEMU
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually need qemu considering we're only building for amd64 in this workflow?

uses: docker/setup-qemu-action@v1
with:
platforms: all
Copy link
Member

Choose a reason for hiding this comment

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

If we do need it, I suppose this should be set to the subset of platforms we actually do support:

Suggested change
platforms: all
platforms: linux/amd64,linux/arm/v7,linux/arm64

- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
- name: Restore go cache
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Cache Docker layers
uses: actions/cache@v2
id: cache
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-ghcache-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-ghcache-
- name: Set up Go
uses: actions/setup-go@v2
with:
Expand All @@ -28,4 +43,15 @@ jobs:
- name: Run tests
run: make test
- name: Build container image
run: make docker-build IMG=ghcr.io/fluxcd/${{ github.event.repository.name }}:latest
run: |
make docker-build IMG=ghcr.io/fluxcd/${{ github.event.repository.name }}:latest \
BUILD_PLATFORMS=linux/amd64 \
BUILD_ARGS="--cache-from=type=local,src=/tmp/.buildx-cache \
--cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max"
- # Temp fix
Copy link
Member

Choose a reason for hiding this comment

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

We can now use -cache-from=type=gha --cache-to=type=gha,mode=max as docker/buildx#681 was resolved.

# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
15 changes: 13 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
ARG XX_VERSION=1.0.0-rc.2

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

# Build the manager binary
FROM golang:1.16-alpine as builder
FROM --platform=$BUILDPLATFORM golang:1.16-alpine AS builder

# Copy the build utilities.
COPY --from=xx / /

ARG TARGETPLATFORM

# Configure workspace.
WORKDIR /workspace

# copy modules manifests
Expand All @@ -20,7 +30,8 @@ COPY controllers/ controllers/
COPY internal/ internal/

# build without giving the arch, so that it gets it from the machine
RUN CGO_ENABLED=0 go build -a -o image-reflector-controller main.go
ENV CGO_ENABLED=0
RUN xx-go build -a -o image-reflector-controller main.go

FROM alpine:3.13

Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ CRD_OPTIONS ?= crd:crdVersions=v1
ENVTEST_BIN_VERSION?=1.22.0
KUBEBUILDER_ASSETS?=$(shell $(SETUP_ENVTEST) use -i $(ENVTEST_BIN_VERSION) -p path)

# Allows for defining additional Docker buildx arguments, e.g. '--push'.
BUILD_ARGS ?=
# Architectures to build images for.
BUILD_PLATFORMS ?= linux/amd64

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -76,8 +81,12 @@ generate: controller-gen
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."

# Build the docker image
docker-build: test
docker build . -t ${IMG}
docker-build:
docker buildx build \
--platform=$(BUILD_PLATFORMS) \
-t ${IMG} \
--load \
${BUILD_ARGS} .

# Push the docker image
docker-push:
Expand Down