From 5bbb2483347a33265901b67fb00e9b7d463ed871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pavl=C3=ADn?= Date: Wed, 26 Apr 2023 14:44:43 +0200 Subject: [PATCH 1/3] feat: add multi-arch Dockerfiles and scripts --- Makefile | 58 ++++++++++++++++++++++++ docker/base/Dockerfile.amd64 | 15 +++++++ docker/base/Dockerfile.arm64 | 19 ++++++++ docker/base/build.sh | 80 +++++++++++++++++++++++++++++++++ docker/base/entrypoint.sh | 11 +++++ docker/builder/Dockerfile.arm64 | 0 6 files changed, 183 insertions(+) create mode 100644 docker/base/Dockerfile.amd64 create mode 100644 docker/base/Dockerfile.arm64 create mode 100755 docker/base/build.sh create mode 100755 docker/base/entrypoint.sh create mode 100644 docker/builder/Dockerfile.arm64 diff --git a/Makefile b/Makefile index ef82e1ed85..36ae964189 100644 --- a/Makefile +++ b/Makefile @@ -311,3 +311,61 @@ wrappers: | build deps librln libwaku.so go build -ldflags "-linkmode external -extldflags '$(EXTRA_LIBS_DYNAMIC)'" -o build/go_wrapper_example wrappers/wrapper_example.go #wrappers/cfuncs.go endif # "variables.mk" was not included + +############################################## +# Static Binaries for Multiple Architectures # +############################################## + +MAKE_PID := $(shell echo $$PPID) +JOB_FLAG := $(filter -j%, $(subst --jobs ,-j,$(subst --jobs=,-j,$(subst -j ,-j,$(shell ps T | grep "^\s*$(MAKE_PID).*$(MAKE)"))))) +JOBS := $(subst -j,,$(JOB_FLAG)) +ifneq ($(JOBS), ) + JOBS := -j $(JOBS) +endif + +ARCH :=amd64 +NWAKU_BASE_TAG := latest_ +#nwaku-base-20230502082624_arm64 + +SHELL := /bin/bash + +static-arm64: ARCH :=arm64 +static-arm64: + set -x &&\ + mkdir -p $${PWD}/vendor-$(ARCH) $${PWD}/build/$(ARCH) &&\ + docker run -it --rm\ + -v $${PWD}:/home/user/nwaku:z\ + -v $${PWD}/vendor-arm64:/home/user/nwaku/vendor:z\ + -v $${PWD}/build/arm64:/home/user/nwaku/build:z\ + --workdir /home/user/nwaku\ + --user $$(id -u):$$(id -g)\ + nwaku:$(NWAKU_BASE_TAG)$(ARCH)\ + $(JOBS)\ + V=1\ + LOG_LEVEL="TRACE"\ + QUICK_AND_DIRTY_COMPILER=1\ + NIMFLAGS="--cpu:arm64 --os:linux --gcc.exe:aarch64-alpine-linux-musl-gcc --gcc.linkerexe:aarch64-alpine-linux-musl-gcc --passL:-static"\ + wakunode2 + +static-amd64: ARCH :=amd64 +static-amd64: + set -x &&\ + mkdir -p $${PWD}/vendor-$(ARCH) $${PWD}/build/$(ARCH) &&\ + docker run -it --rm\ + -v $${PWD}:/home/user/nwaku:z\ + -v $${PWD}/vendor-amd64:/home/user/nwaku/vendor:z\ + -v $${PWD}/build/amd64:/home/user/nwaku/build:z\ + --workdir /home/user/nwaku\ + --user $$(id -u):$$(id -g)\ + nwaku:$(NWAKU_BASE_TAG)$(ARCH)\ + $(JOBS)\ + V=1\ + LOG_LEVEL="TRACE"\ + QUICK_AND_DIRTY_COMPILER=1\ + NIMFLAGS="--cpu:amd64 --os:linux --gcc.exe:x86_64-alpine-linux-musl-gcc --gcc.linkerexe:x86_64-alpine-linux-musl-gcc --passL:-static"\ + wakunode2 + +static-all: static-arm64 static-amd64 + +static-clean: + rm -rf build/arm64 build/amd64 vendor-arm64 vendor-amd64 \ No newline at end of file diff --git a/docker/base/Dockerfile.amd64 b/docker/base/Dockerfile.amd64 new file mode 100644 index 0000000000..5db4a02b46 --- /dev/null +++ b/docker/base/Dockerfile.amd64 @@ -0,0 +1,15 @@ +FROM --platform=amd64 alpine:edge + +ARG USER_ID +ARG GROUP_ID + +RUN addgroup -g ${GROUP_ID} user; \ + adduser --disabled-password --gecos '' -u ${USER_ID} -G user user; + +WORKDIR /home/user/nwaku +ENTRYPOINT [ "entrypoint.sh" ] + +# Get build tools and required header files +RUN apk add --no-cache bash git cargo build-base pcre-dev linux-headers + +ADD entrypoint.sh /bin/entrypoint.sh diff --git a/docker/base/Dockerfile.arm64 b/docker/base/Dockerfile.arm64 new file mode 100644 index 0000000000..bb710f973a --- /dev/null +++ b/docker/base/Dockerfile.arm64 @@ -0,0 +1,19 @@ +FROM --platform=arm64 alpine:edge + +ARG USER_ID +ARG GROUP_ID + +RUN addgroup -g ${GROUP_ID} user; \ + adduser --disabled-password --gecos '' -u ${USER_ID} -G user user; + +WORKDIR /home/user/nwaku +ENTRYPOINT [ "entrypoint.sh" ] + +# Get build tools and required header files +RUN apk add --no-cache bash git cargo build-base pcre-dev linux-headers + +ADD entrypoint.sh /bin/entrypoint.sh + +#RUN git clone https://github.com/waku-org/nwaku.git --depth 1 &&\ +# cd nwaku &&\ +# make deps diff --git a/docker/base/build.sh b/docker/base/build.sh new file mode 100755 index 0000000000..a994ddfa38 --- /dev/null +++ b/docker/base/build.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +set -ex + +cd "$(dirname "${BASH_SOURCE[0]}")" + +BUILDER= +PUSH= +LOAD= +LATEST= +IMAGE_NAME="nwaku" + +for i in `seq 1 $#`;do + case $1 in + "--builder") + shift + BUILDER="--builder ${1}" + shift + ;; + "--push") + shift + PUSH="--push" + ;; + "--load") + shift + LOAD="--load" + ;; + "--registry") + shift + REGISTRY="${1}/" + shift + ;; + "--repository") + shift + REPOSITORY="${1}/" + shift + ;; + "--latest") + shift + LATEST="1" + ;; + *) + break + ;; + esac +done + +IMAGE_NAME=${REGISTRY}${REPOSITORY}${IMAGE_NAME} + +ARCHS=$(ls Dockerfile.* | sed 's/.*\.//') + +ARCH="${@:-amd64}" + + + +if [[ "${ARCH}" == "all" ]]; then + echo "Building all images: $( echo ${ARCHS} | tr '\n' ' ')" + ARCH=${ARCHS} +fi + +TAG="nwaku-base-$(date --utc +"%Y%m%d%H%M%S")" + +for arch in $(echo ${ARCH}); do + DOCKER_BUILDKIT=1 \ + docker build\ + ${BUILDER}\ + ${PUSH}\ + ${LOAD}\ + -t ${IMAGE_NAME}:${TAG}_${arch}\ + --build-arg USER_ID=$(id -u)\ + --build-arg GROUP_ID=$(id -g)\ + -f Dockerfile.${arch}\ + . + + if [[ -n "${LATEST}" ]]; then + docker tag\ + ${IMAGE_NAME}:${TAG}_${arch}\ + ${IMAGE_NAME}:latest_${arch} + fi +done diff --git a/docker/base/entrypoint.sh b/docker/base/entrypoint.sh new file mode 100755 index 0000000000..334270c8cc --- /dev/null +++ b/docker/base/entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +echo $@ + +die() { + exit 143; # 128 + 15 -- SIGTERM +} + +trap 'die' SIGINT + +make "$@" \ No newline at end of file diff --git a/docker/builder/Dockerfile.arm64 b/docker/builder/Dockerfile.arm64 new file mode 100644 index 0000000000..e69de29bb2 From 18a57eb11d182fdecd7603209d78c15b4ad2f872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pavl=C3=ADn?= Date: Fri, 12 May 2023 10:23:55 +0200 Subject: [PATCH 2/3] Add containers build based on static binaries --- Makefile | 101 +++++++++++++++++++++++++---------- docker/base/build.sh | 4 +- docker/base/entrypoint.sh | 2 +- docker/dist/Dockerfile.amd64 | 5 ++ docker/dist/Dockerfile.arm64 | 5 ++ 5 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 docker/dist/Dockerfile.amd64 create mode 100644 docker/dist/Dockerfile.arm64 diff --git a/Makefile b/Makefile index 36ae964189..a25b6704e5 100644 --- a/Makefile +++ b/Makefile @@ -324,48 +324,93 @@ ifneq ($(JOBS), ) endif ARCH :=amd64 -NWAKU_BASE_TAG := latest_ +#FIXME! +NWAKU_BUILDER_IMAGE := quay.io/vpavlin0/nwaku-builder +NWAKU_BASE_TAG := 20230512110759 +NWAKU_DIST_TAG := $(GIT_VERSION)_ #nwaku-base-20230502082624_arm64 SHELL := /bin/bash -static-arm64: ARCH :=arm64 -static-arm64: +BINARIES := wakunode2 wakunode1 chat2 + +static-base: set -x &&\ - mkdir -p $${PWD}/vendor-$(ARCH) $${PWD}/build/$(ARCH) &&\ + mkdir -p $${PWD}/vendor-$(ARCH) $${PWD}/build/$(ARCH) $${PWD}/nimcache-$(ARCH) &&\ docker run -it --rm\ -v $${PWD}:/home/user/nwaku:z\ - -v $${PWD}/vendor-arm64:/home/user/nwaku/vendor:z\ - -v $${PWD}/build/arm64:/home/user/nwaku/build:z\ + -v $${PWD}/vendor-$(ARCH):/home/user/nwaku/vendor:z\ + -v $${PWD}/nimcache-$(ARCH):/home/user/nwaku/nimcache:z\ + -v $${PWD}/build/$(ARCH):/home/user/nwaku/build:z\ --workdir /home/user/nwaku\ --user $$(id -u):$$(id -g)\ - nwaku:$(NWAKU_BASE_TAG)$(ARCH)\ + $(NWAKU_BUILDER_IMAGE):$(NWAKU_BASE_TAG)_$(ARCH)\ $(JOBS)\ - V=1\ + V=1\ LOG_LEVEL="TRACE"\ QUICK_AND_DIRTY_COMPILER=1\ - NIMFLAGS="--cpu:arm64 --os:linux --gcc.exe:aarch64-alpine-linux-musl-gcc --gcc.linkerexe:aarch64-alpine-linux-musl-gcc --passL:-static"\ - wakunode2 + NIMFLAGS="$(NIMFLAGS)"\ + $(BINARIES) + +static-arm64: ARCH :=arm64 +static-arm64: NIMFLAGS := $(NIMFLAGS) --cpu:arm64 --os:linux --gcc.exe:aarch64-alpine-linux-musl-gcc --gcc.linkerexe:aarch64-alpine-linux-musl-gcc --passL:-static +static-arm64: + $(MAKE) $(JOBS) static-base NIMFLAGS="$(NIMFLAGS)" ARCH=$(ARCH) + + static-amd64: ARCH :=amd64 +static-amd64: NIMFLAGS := $(NIMFLAGS) --cpu:amd64 --os:linux --gcc.exe:x86_64-alpine-linux-musl-gcc --gcc.linkerexe:x86_64-alpine-linux-musl-gcc --passL:-static static-amd64: - set -x &&\ - mkdir -p $${PWD}/vendor-$(ARCH) $${PWD}/build/$(ARCH) &&\ - docker run -it --rm\ - -v $${PWD}:/home/user/nwaku:z\ - -v $${PWD}/vendor-amd64:/home/user/nwaku/vendor:z\ - -v $${PWD}/build/amd64:/home/user/nwaku/build:z\ - --workdir /home/user/nwaku\ - --user $$(id -u):$$(id -g)\ - nwaku:$(NWAKU_BASE_TAG)$(ARCH)\ - $(JOBS)\ - V=1\ - LOG_LEVEL="TRACE"\ - QUICK_AND_DIRTY_COMPILER=1\ - NIMFLAGS="--cpu:amd64 --os:linux --gcc.exe:x86_64-alpine-linux-musl-gcc --gcc.linkerexe:x86_64-alpine-linux-musl-gcc --passL:-static"\ - wakunode2 - -static-all: static-arm64 static-amd64 + $(MAKE) $(JOBS) static-base NIMFLAGS="$(NIMFLAGS)" ARCH=$(ARCH) static-clean: - rm -rf build/arm64 build/amd64 vendor-arm64 vendor-amd64 \ No newline at end of file + rm -rf build/arm64 build/amd64 vendor-arm64 vendor-amd64 + + +################################################ +# Distributable Artifacts (tars and containers) # +################################################ + +dist-base: + mkdir -p dist &&\ + pushd build/$(ARCH) &&\ + tar czf ../../dist/wakunode-$(GIT_VERSION)-$(ARCH).tgz $(BINARIES) + +dist-amd64: static-amd64 + $(MAKE) $(JOBS) dist-base ARCH=amd64 + +dist-arm64: static-arm64 + $(MAKE) $(JOBS) dist-base ARCH=arm64 + +dist-container-base: + echo $(ARCH) &&\ + ls &&\ + cd build/$(ARCH) &&\ + docker build -t nwaku:$(NWAKU_DIST_TAG)$(ARCH) -f ../../docker/dist/Dockerfile.$(ARCH) . + +dist-container-amd64: | dist-amd64 + $(MAKE) $(JOBS) dist-container-base ARCH=amd64 + +dist-container-arm64: | dist-arm64 + $(MAKE) $(JOBS) dist-container-base ARCH=arm64 + +dist-container: + $(MAKE) $(JOBS) dist-container-amd64 + $(MAKE) $(JOBS) dist-container-arm64 + docker manifest create quay.io/vpavlin0/nwaku:multiarch \ + --amend nwaku:$(NWAKU_DIST_TAG)amd64 \ + --amend nwaku:$(NWAKU_DIST_TAG)arm64 + +################### +# Release Targets # +################### + +release-notes: + docker run \ + -it \ + --rm \ + -v $${PWD}:/opt/sv4git/repo:z \ + -u $(shell id -u) \ + quay.io/vpavlin0/sv4git:latest \ + release-notes diff --git a/docker/base/build.sh b/docker/base/build.sh index a994ddfa38..574e11c421 100755 --- a/docker/base/build.sh +++ b/docker/base/build.sh @@ -8,7 +8,7 @@ BUILDER= PUSH= LOAD= LATEST= -IMAGE_NAME="nwaku" +IMAGE_NAME="nwaku-builder" for i in `seq 1 $#`;do case $1 in @@ -58,7 +58,7 @@ if [[ "${ARCH}" == "all" ]]; then ARCH=${ARCHS} fi -TAG="nwaku-base-$(date --utc +"%Y%m%d%H%M%S")" +TAG="$(date --utc +"%Y%m%d%H%M%S")" for arch in $(echo ${ARCH}); do DOCKER_BUILDKIT=1 \ diff --git a/docker/base/entrypoint.sh b/docker/base/entrypoint.sh index 334270c8cc..86965d79c2 100755 --- a/docker/base/entrypoint.sh +++ b/docker/base/entrypoint.sh @@ -8,4 +8,4 @@ die() { trap 'die' SIGINT -make "$@" \ No newline at end of file +make "$@" diff --git a/docker/dist/Dockerfile.amd64 b/docker/dist/Dockerfile.amd64 new file mode 100644 index 0000000000..0f2ef1e48f --- /dev/null +++ b/docker/dist/Dockerfile.amd64 @@ -0,0 +1,5 @@ +FROM --platform=amd64 alpine:edge + +ADD . /usr/local/bin + +ENTRYPOINT [ "wakunode2" ] diff --git a/docker/dist/Dockerfile.arm64 b/docker/dist/Dockerfile.arm64 new file mode 100644 index 0000000000..129f504f77 --- /dev/null +++ b/docker/dist/Dockerfile.arm64 @@ -0,0 +1,5 @@ +FROM --platform=arm64 alpine:edge + +ADD . /usr/local/bin + +ENTRYPOINT [ "wakunode2" ] From c84ae7486663ed2f325b68e3839f581a4e00b835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pavl=C3=ADn?= Date: Fri, 12 May 2023 16:36:16 +0200 Subject: [PATCH 3/3] Add Jenkinsfile to produce the builder images --- ci/Jenkinsfile.builders | 34 ++++++++++++++++++++++++++++++++++ docker/base/build.sh | 6 ++++++ 2 files changed, 40 insertions(+) create mode 100644 ci/Jenkinsfile.builders diff --git a/ci/Jenkinsfile.builders b/ci/Jenkinsfile.builders new file mode 100644 index 0000000000..7260e301c6 --- /dev/null +++ b/ci/Jenkinsfile.builders @@ -0,0 +1,34 @@ +pipeline { + agent { label 'linux' } + + options { + timestamps() + timeout(time: 20, unit: 'MINUTES') + buildDiscarder(logRotator( + numToKeepStr: '10', + daysToKeepStr: '30', + )) + } + + parameters { + string( + name: 'REPOSITORY', + description: 'Name of container registry repository', + defaultValue: params.REPOSITORY ?: 'wakuorg', + ) + } + + stages { + stage('Build & Push') { + steps { + withDockerRegistry([credentialsId: "dockerhub-vacorgbot-api-token", url: ""]) { + dir("docker/base") { + sh "./build.sh --registry docker.io --repository ${params.REPOSITORY} --latest --push all" + } + } + } + } + } // stages + +} // pipeline + diff --git a/docker/base/build.sh b/docker/base/build.sh index 574e11c421..e138890ada 100755 --- a/docker/base/build.sh +++ b/docker/base/build.sh @@ -8,6 +8,7 @@ BUILDER= PUSH= LOAD= LATEST= +NO_CACHE="--no-cache" IMAGE_NAME="nwaku-builder" for i in `seq 1 $#`;do @@ -39,6 +40,10 @@ for i in `seq 1 $#`;do shift LATEST="1" ;; + "--use-cache") + shift + NO_CACHE="" + ;; *) break ;; @@ -66,6 +71,7 @@ for arch in $(echo ${ARCH}); do ${BUILDER}\ ${PUSH}\ ${LOAD}\ + ${NO_CACHE}\ -t ${IMAGE_NAME}:${TAG}_${arch}\ --build-arg USER_ID=$(id -u)\ --build-arg GROUP_ID=$(id -g)\