diff --git a/.github/workflows/build_main.yaml b/.github/workflows/build_main.yaml index 90f9698ce2a..c75f3e5b802 100644 --- a/.github/workflows/build_main.yaml +++ b/.github/workflows/build_main.yaml @@ -10,6 +10,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + version: latest - name: Build and Push to registry env: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} @@ -19,5 +23,5 @@ jobs: TAG_LATEST: "false" run: | echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin - make push + make multiarch-build-push docker logout diff --git a/.github/workflows/build_tag.yaml b/.github/workflows/build_tag.yaml index 8d2fc871104..f139ff92489 100644 --- a/.github/workflows/build_tag.yaml +++ b/.github/workflows/build_tag.yaml @@ -17,6 +17,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + version: latest - name: Build and Push to registry env: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} diff --git a/Dockerfile b/Dockerfile index 7eaf55e3070..8247a410ba5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM golang:1.15.2 AS build +ARG BUILDPLATFORM=linux/amd64 + +FROM --platform=$BUILDPLATFORM golang:1.15.2 AS build WORKDIR /contour ENV GOPROXY=https://proxy.golang.org @@ -13,13 +15,16 @@ COPY Makefile Makefile ARG BUILD_BRANCH ARG BUILD_SHA ARG BUILD_VERSION +ARG TARGETOS +ARG TARGETARCH -RUN make install \ +RUN make build \ CGO_ENABLED=0 \ - GOOS=linux \ + GOOS=${TARGETOS} \ + GOARCH=${TARGETARCH} \ BUILD_VERSION=${BUILD_VERSION} \ BUILD_SHA=${BUILD_SHA} \ BUILD_BRANCH=${BUILD_BRANCH} FROM scratch AS final -COPY --from=build /go/bin/contour /bin/contour +COPY --from=build /contour/contour /bin/contour diff --git a/Makefile b/Makefile index 394918943b1..48232907f69 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,19 @@ JEKYLL_PORT := 4000 JEKYLL_LIVERELOAD_PORT := 35729 TAG_LATEST ?= false + +ifeq ($(TAG_LATEST), true) + IMAGE_TAGS = \ + --tag $(IMAGE):$(VERSION) \ + --tag $(IMAGE):latest +else + IMAGE_TAGS = \ + --tag $(IMAGE):$(VERSION) +endif + +# Platforms to build the multi-arch image for. +IMAGE_PLATFORMS ?= linux/amd64,linux/arm64 + # Used to supply a local Envoy docker container an IP to connect to that is running # 'contour serve'. On MacOS this will work, but may not on other OSes. Defining # LOCALIP as an env var before running 'make local' will solve that. @@ -71,6 +84,9 @@ check: install check-test check-test-race ## Install and run tests .PHONY: checkall checkall: vendor check lint check-generate +build: ## Build the contour binary + go build -mod=readonly -v -ldflags="$(GO_LDFLAGS)" $(GO_TAGS) $(MODULE)/cmd/contour + install: ## Build and install the contour binary go install -mod=readonly -v -ldflags="$(GO_LDFLAGS)" $(GO_TAGS) $(MODULE)/cmd/contour @@ -84,6 +100,17 @@ download: ## Download Go modules vendor: go mod vendor +multiarch-build-push: ## Build and push a multi-arch Contour container image to the Docker registry + docker buildx build \ + --platform $(IMAGE_PLATFORMS) \ + --build-arg "BUILD_VERSION=$(BUILD_VERSION)" \ + --build-arg "BUILD_BRANCH=$(BUILD_BRANCH)" \ + --build-arg "BUILD_SHA=$(BUILD_SHA)" \ + $(DOCKER_BUILD_LABELS) \ + $(IMAGE_TAGS) \ + --push \ + . + container: ## Build the Contour container image docker build \ --build-arg "BUILD_VERSION=$(BUILD_VERSION)" \ diff --git a/hack/actions/build-and-push-release-images.sh b/hack/actions/build-and-push-release-images.sh index 01d5664e2ee..7ca3aa2b225 100755 --- a/hack/actions/build-and-push-release-images.sh +++ b/hack/actions/build-and-push-release-images.sh @@ -6,7 +6,7 @@ set -o pipefail # This scripts sets TAG_LATEST based on whether the tag currently being # built is the highest semantic version tag that's not a pre-release -# (alpha, beta, rc), and calls 'make push'. +# (alpha, beta, rc), and calls 'make multiarch-build-push'. # # This script is intended to be run only for tag builds and will no-op # if GITHUB_REF is not in the format "refs/tags/". @@ -54,4 +54,4 @@ echo "CURRENT_TAG: $CURRENT_TAG" echo "HIGHEST_SEMVER_TAG: $HIGHEST_SEMVER_TAG" echo "TAG_LATEST: $TAG_LATEST" -make push TAG_LATEST="$TAG_LATEST" +make multiarch-build-push TAG_LATEST="$TAG_LATEST"