Skip to content

Commit

Permalink
Merge pull request #52 from thomasferrandiz/optimize-build
Browse files Browse the repository at this point in the history
optimize build time by using cross-compilation instead of emulation
  • Loading branch information
thomasferrandiz authored Apr 10, 2024
2 parents 40e6164 + b30b03b commit 8cf0620
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 193 deletions.
143 changes: 0 additions & 143 deletions .drone.yml

This file was deleted.

70 changes: 70 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
on:
push:
branches:
- master
pull_request:

permissions:
contents: read
security-events: write # upload Sarif results

name: Build
jobs:
build-amd64:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set the TAG value
id: get-TAG
run: |
echo "$(make -s log | grep TAG)" >> "$GITHUB_ENV"
- name: Build container image
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: rancher/hardened-coredns:${{ env.TAG }}-amd64
file: Dockerfile

- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@0.18.0
with:
image-ref: rancher/hardened-coredns:${{ env.TAG }}-amd64
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'

build-arm64:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Set the TAG value
id: get-TAG
run: |
echo "$(make -s log | grep TAG)" >> "$GITHUB_ENV"
- name: Build container image
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: rancher/hardened-coredns:${{ env.TAG }}-arm64
file: Dockerfile
outputs: type=docker
platforms: linux/arm64
44 changes: 44 additions & 0 deletions .github/workflows/image-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
on:
release:
types: [published]

permissions:
contents: read

jobs:
push-multiarch:
permissions:
contents: read
id-token: write
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: "Read secrets"
uses: rancher-eio/read-vault-secrets@main
with:
secrets: |
secret/data/github/repo/${{ github.repository }}/dockerhub/${{ github.repository_owner }}/credentials username | DOCKER_USERNAME ;
secret/data/github/repo/${{ github.repository }}/dockerhub/${{ github.repository_owner }}/credentials password | DOCKER_PASSWORD
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Container Registry
uses: docker/login-action@v3
with:
username: ${{ env.DOCKER_USERNAME }}
password: ${{ env.DOCKER_PASSWORD }}

- name: Build container image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: rancher/hardened-coredns:${{ github.event.release.tag_name }}
file: Dockerfile
platforms: linux/amd64, linux/arm64
34 changes: 24 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
ARG BCI_IMAGE=registry.suse.com/bci/bci-busybox
ARG GO_IMAGE=rancher/hardened-build-base:v1.21.9b1
ARG ARCH="amd64"

# Image that provides cross compilation tooling.
FROM --platform=$BUILDPLATFORM rancher/mirrored-tonistiigi-xx:1.3.0 as xx

FROM ${BCI_IMAGE} as bci
FROM ${GO_IMAGE} as base-builder
FROM --platform=$BUILDPLATFORM ${GO_IMAGE} as base-builder
# copy xx scripts to your build stage
COPY --from=xx / /
RUN apk add file make git clang lld
ARG TARGETPLATFORM
# setup required packages
RUN set -x && \
apk --no-cache add \
file \
gcc \
git \
make
xx-apk --no-cache add musl-dev gcc lld

# setup the coredns build
FROM base-builder as coredns-builder
FROM --platform=$BUILDPLATFORM base-builder as coredns-builder
ARG SRC=github.com/coredns/coredns
ARG PKG=github.com/coredns/coredns
ARG ARCH
Expand All @@ -21,16 +25,26 @@ RUN git clone --depth=1 https://${SRC}.git $GOPATH/src/${PKG}
WORKDIR $GOPATH/src/${PKG}
RUN git fetch --all --tags --prune
RUN git checkout tags/${TAG} -b ${TAG}
RUN GO_LDFLAGS="-linkmode=external -X ${PKG}/coremain.GitCommit=$(git rev-parse --short HEAD)" \
RUN go mod download
# cross-compilation setup
ARG TARGETPLATFORM
RUN xx-go --wrap && \
GO_LDFLAGS="-linkmode=external -X ${PKG}/coremain.GitCommit=$(git rev-parse --short HEAD)" \
go-build-static.sh -gcflags=-trimpath=${GOPATH}/src -o bin/coredns .
RUN go-assert-static.sh bin/*
RUN xx-verify --static bin/*
RUN if [ "${ARCH}" != "s390x" || "${ARCH}" != "arm64" ]; then \
go-assert-boring.sh bin/*; \
fi

RUN install -s bin/* /usr/local/bin
RUN install bin/* /usr/local/bin
RUN coredns --version

FROM bci as coredns
FROM ${GO_IMAGE} as strip_binary
#strip needs to run on TARGETPLATFORM, not BUILDPLATFORM
COPY --from=coredns-builder /usr/local/bin/coredns /coredns
RUN strip /coredns

FROM bci as coredns
COPY --from=strip_binary /coredns /coredns
ENTRYPOINT ["/coredns"]
31 changes: 13 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,33 @@ SRC ?= github.com/coredns/coredns
TAG ?= v1.11.1$(BUILD_META)
export DOCKER_BUILDKIT?=1

ifneq ($(DRONE_TAG),)
TAG := $(DRONE_TAG)
endif

ifeq (,$(filter %$(BUILD_META),$(TAG)))
$(error TAG needs to end with build metadata: $(BUILD_META))
$(error TAG ${TAG} needs to end with build metadata: $(BUILD_META))
endif

.PHONY: image-build
image-build:
docker build \
--pull \
docker buildx build \
--platform=$(ARCH) \
--build-arg PKG=$(PKG) \
--build-arg SRC=$(SRC) \
--build-arg TAG=$(TAG:$(BUILD_META)=) \
--build-arg ARCH=$(ARCH) \
--target coredns \
--tag $(ORG)/hardened-coredns:$(TAG) \
--tag $(ORG)/hardened-coredns:$(TAG)-$(ARCH) \
--load \
.

.PHONY: image-push
image-push:
docker push $(ORG)/hardened-coredns:$(TAG)-$(ARCH)

.PHONY: image-manifest
image-manifest:
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create --amend \
$(ORG)/hardened-coredns:$(TAG) \
$(ORG)/hardened-coredns:$(TAG)-$(ARCH)
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push \
$(ORG)/hardened-coredns:$(TAG)
PHONY: log
log:
@echo "ARCH=$(ARCH)"
@echo "TAG=$(TAG)"
@echo "ORG=$(ORG)"
@echo "PKG=$(PKG)"
@echo "SRC=$(SRC)"
@echo "BUILD_META=$(BUILD_META)"
@echo "UNAME_M=$(UNAME_M)"

.PHONY: image-scan
image-scan:
Expand Down
12 changes: 0 additions & 12 deletions manifest-coredns.tmpl

This file was deleted.

Loading

0 comments on commit 8cf0620

Please sign in to comment.