From e6f70a62e574994b0b1d5e32a70e1f22a841e659 Mon Sep 17 00:00:00 2001 From: Shachar Sharon Date: Thu, 2 Feb 2023 12:24:29 +0200 Subject: [PATCH] workflows: build within emulated container Use QEMU to build samba-operator within containerized environment; either AMD64 or ARM64 architectures (regardless of local host architecture). Using this method we can ensures that all build tools are properly configured on architectures other than default x86_64, without relaying on specific hardware. Signed-off-by: Shachar Sharon --- .github/workflows/build-within-container.yml | 26 ++++++++++++++ hack/Dockerfile.build | 8 +++++ hack/build-within-container.sh | 37 ++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .github/workflows/build-within-container.yml create mode 100644 hack/Dockerfile.build create mode 100755 hack/build-within-container.sh diff --git a/.github/workflows/build-within-container.yml b/.github/workflows/build-within-container.yml new file mode 100644 index 00000000..5735b0d3 --- /dev/null +++ b/.github/workflows/build-within-container.yml @@ -0,0 +1,26 @@ +name: Build within container +on: + push: + branches: [master] + pull_request: + branches: [master] +jobs: + build-within-container: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Enable emulation + run: | + sudo apt-get update + sudo apt-get install -y qemu qemu-user-static + sudo update-binfmts --display + - name: Require build utilities + run: | + command -v podman + command -v qemu-x86_64-static + command -v qemu-aarch64-static + - name: Build within AMD64 container + run: hack/build-within-container.sh amd64 + - name: Build within ARM64 container + run: hack/build-within-container.sh arm64 diff --git a/hack/Dockerfile.build b/hack/Dockerfile.build new file mode 100644 index 00000000..a8820d57 --- /dev/null +++ b/hack/Dockerfile.build @@ -0,0 +1,8 @@ +FROM centos:stream9 +WORKDIR /workspace +RUN dnf install -y epel-release epel-next-release +RUN dnf install -y gcc tar golang wget make yamllint +COPY samba-operator.tar.gz . +RUN tar xvfz samba-operator.tar.gz +RUN go version +RUN make diff --git a/hack/build-within-container.sh b/hack/build-within-container.sh new file mode 100755 index 00000000..e8a4ca1c --- /dev/null +++ b/hack/build-within-container.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +export LC_ALL=C +unset CDPATH +ARCH=${1:-amd64} + +# Currently, aupport amd64 (x86_64) and arm64 (aarch64) architectures +case "${ARCH}" in "amd64") ;; "arm64") ;; \ + *) echo "illegal ${ARCH}" && exit 1 ;; esac + +# Prerequisites checks +_require_command() { + command -v "$1" > /dev/null || (echo "missing: $1" && exit 1) +} + +_require_command realpath +_require_command git +_require_command podman +_require_command qemu-x86_64-static +_require_command qemu-aarch64-static + +# Fail on error +set -o errexit +set -o nounset +set -o pipefail + +# Create tar-ball using git +BASEDIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")/../") +cd "${BASEDIR}" +git archive --format tar.gz HEAD > hack/samba-operator.tar.gz +function cleanup_on_exit() { rm -f hack/samba-operator.tar.gz; } +trap cleanup_on_exit EXIT + +# Build within container using podman +podman build \ + --arch=${ARCH} \ + --tag localhost/samba-operator-build:${ARCH} \ + --file hack/Dockerfile.build