This is an automated email from the ASF dual-hosted git repository. vatamane pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/couchdb-ci.git
commit d793cc1a8c78c1b560b267bf6eefee57555b2327 Author: Adam Kocoloski <[email protected]> AuthorDate: Sat Feb 26 09:29:52 2022 -0500 Restore old xplat support, add new buildx targets This preserves the method of generating single-arch images while adding targets that use the buildx plugin to generate multi-arch images. In the future it would be possible to combine the single-arch images into a multi-arch one using the manifest approach if someone were so inclined. The couchdb-docker repo has the code to do this for the image we provide to end users. This commit is meant to revert many of the changes from 5c78da4 and can be reviewed together with it. --- README.md | 63 ++++++++++++++++++++++---------- build.sh | 87 ++++++++++++++++++++++++++++++++++++++++----- dockerfiles/debian-bullseye | 4 ++- dockerfiles/debian-buster | 5 +-- dockerfiles/debian-stretch | 4 ++- 5 files changed, 132 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 50a35a2..df1b0d3 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ For those OSes that support Docker, we run builds inside of Docker containers. T ## Authenticating to Docker Hub -1. You need a Docker Cloud account with access to the `apache` organization to upload new images. Ask the CouchDB PMC for assistance with this. +1. You need a Docker Cloud account with access to the `apache` organization to upload images. Ask the CouchDB PMC for assistance with this. 2. `export DOCKER_ID_USER="username"` 3. `docker login -u "username"` and enter your password. @@ -65,7 +65,9 @@ the [kerl](https://github.com/kerl/kerl) build system, and installs them to `/usr/local/kerl` for activation before builds. This version is intended for use in standard CI runs, such as for pull requests. -## Building a cross-architecture Docker image +## Building images for other architectures + +### Multi-arch images with Docker Buildx We can use Docker's [Buildx](https://docs.docker.com/buildx/working-with-buildx/) plugin to generate @@ -74,20 +76,37 @@ Desktop ships with buildx support, but you'll need to create a new builder to use it: ``` -docker buildx create --use +docker buildx create --name apache-couchdb --use +``` + +The `build.sh` script has `buildx-base` and `buildx-platform` targets that will +will build **and upload** a new multi-arch container image to the registry. For +example: + ``` +./build.sh buildx-platform debian-bullseye +``` + +The `$BUILDX_PLATFORMS` environment variable can be used to override the default +set of target platforms that will be supplied to the buildx builder. + +### Cross-building with $CONTAINERARCH -Then, add the `--platform` flag to spin up parallel builders for each desired -architecture. For example: +Alternatively, we can build individual images for each architecture. This only works from an `x86_64` build host. + +First, configure your machine with the correct dependencies to build multi-arch binaries: ``` -buildargs="--platform linux/amd64,linux/arm64,linux/ppc64le --push" ./build.sh platform debian-bullseye +docker run --privileged --rm tonistiigi/binfmt --install all ``` -will build **and upload** a new multi-arch container image to the registory. -Currently `docker images` can only accept single-platform images, so this is one -downside of the simplified build approach. Omitting the `--push` option will -just leave the build result in the build cache, which isn't terribly useful. +This is a one-time setup step. This docker container run will install the correct qemu static binaries necessary for running foreign architecture binaries on your host machine. It includes special magic to ensure `sudo` works correctly inside a container, too. + +Then, override the `CONTAINERARCH` environment variable when starting `build.sh`: + +``` +CONTAINERARCH=arm64v8 ./build.sh platform debian-bullseye +``` ## Publishing a container @@ -108,16 +127,24 @@ as a build arg to upload it automatically you can upload the image using ./build.sh <command> [OPTIONS] Recognized commands: - clean <plat> Removes all images for <plat>. - clean-all Removes all images for all platforms. + clean <plat> Removes all images for <plat>. + clean-all Removes all images for all platforms. + + *buildx-base <plat> Builds a multi-architecture base image. + *buildx-platform <plat> Builds a multi-architecture image with Erlang & JS support. + + base <plat> Builds the image for <plat> without Erlang or JS support. + base-all Builds all images without Erlang or JS support. + *base-upload <plat> Uploads the apache/couchdbci-{os} base images to Docker Hub. + *base-upload-all Uploads all the apache/couchdbci base images to Docker Hub. - platform <plat> Builds the image for <plat> with Erlang & JS support. - platform-all Builds all images with Erlang and JS support. - *platform-upload Uploads the couchdbdev/*-erlang-* images to Docker Hub. - *platform-upload-all Uploads all the couchdbdev/*-erlang-* images to Docker. + platform <plat> Builds the image for <plat> with Erlang & JS support. + platform-all Builds all images with Erlang and JS support. + *platform-upload <plat> Uploads the apache/couchdbci-{os} images to Docker Hub. + *platform-upload-all Uploads all the apache/couchdbci images to Docker Hub. - couch <plat> Builds and tests CouchDB for <plat>. - couch-all Builds and tests CouchDB on all platforms. + couch <plat> Builds and tests CouchDB for <plat>. + couch-all Builds and tests CouchDB on all platforms. Commands marked with * require appropriate Docker Hub credentials. ``` diff --git a/build.sh b/build.sh index 100582d..2089c77 100755 --- a/build.sh +++ b/build.sh @@ -46,7 +46,10 @@ DEBIANS="debian-stretch debian-buster debian-bullseye" UBUNTUS="ubuntu-bionic ubuntu-focal" CENTOSES="centos-7 centos-8" ERLANGALL_BASE="debian-bullseye" +XPLAT_BASE="debian-bullseye" +XPLAT_ARCHES="arm64v8 ppc64le" PASSED_BUILDARGS="$buildargs" +BUILDX_PLATFORMS="linux/amd64,linux/arm64,linux/ppc64le" check-envs() { @@ -63,6 +66,11 @@ check-envs() { then buildargs="$buildargs --build-arg elixirversion=${ELIXIRVERSION} " fi + if [ ! -z "${CONTAINERARCH}" ] + then + buildargs="$buildargs --build-arg repository=${CONTAINERARCH}/debian " + CONTAINERARCH="${CONTAINERARCH}-" + fi } split-os-ver() { @@ -74,6 +82,19 @@ split-os-ver() { } build-base-platform() { + check-envs + split-os-ver $1 + # invoke as build-base <plat> + # base images never get JavaScript, nor Erlang + docker build -f dockerfiles/${os}-${version} \ + --build-arg js=nojs \ + --build-arg erlang=noerlang \ + $buildargs \ + --tag apache/couchdbci-${os}:${CONTAINERARCH}${version}-base \ + ${SCRIPTPATH} +} + +buildx-base-platform() { check-envs split-os-ver $1 # invoke as build-base <plat> @@ -82,7 +103,9 @@ build-base-platform() { --build-arg js=nojs \ --build-arg erlang=noerlang \ $buildargs \ + --platform ${BUILDX_PLATFORMS} \ --tag apache/couchdbci-${os}:${version}-base \ + --push \ ${SCRIPTPATH} } @@ -109,6 +132,19 @@ pull-os-image() { } build-platform() { + check-envs + find-erlang-version $1 + pull-os-image $1 + split-os-ver $1 + docker build -f dockerfiles/${os}-${version} \ + $buildargs \ + --no-cache \ + --tag apache/couchdbci-${os}:${CONTAINERARCH}${version}-erlang-${ERLANGVERSION} \ + ${SCRIPTPATH} + unset ERLANGVERSION +} + +buildx-platform() { check-envs find-erlang-version $1 pull-os-image $1 @@ -116,7 +152,9 @@ build-platform() { docker buildx build -f dockerfiles/${os}-${version} \ $buildargs \ --no-cache \ + --platform ${BUILDX_PLATFORMS} \ --tag apache/couchdbci-${os}:${version}-erlang-${ERLANGVERSION} \ + --push \ ${SCRIPTPATH} unset ERLANGVERSION } @@ -137,7 +175,7 @@ upload-platform() { find-erlang-version $1 check-envs split-os-ver $1 - docker push apache/couchdbci-${os}:${version}-erlang-${ERLANGVERSION} + docker push apache/couchdbci-${os}:${CONTAINERARCH}${version}-erlang-${ERLANGVERSION} } build-test-couch() { @@ -164,6 +202,11 @@ case "$1" in clean $plat done ;; + buildx-base) + # Build and upload multi-arch base image using Docker Buildx + shift + buildx-base-platform $1 + ;; base) # Build base image for requested target platform shift @@ -186,17 +229,32 @@ case "$1" in upload-base $plat $* done ;; + buildx-platform) + # Build and upload multi-arch platform with JS and Erlang support + shift + buildx-platform $1 + ;; platform) # build platform with JS and Erlang support shift build-platform $1 ;; + platform-foreign) + # makes only foreign arch platforms + shift + for arch in $XPLAT_ARCHES; do + CONTAINERARCH=$arch build-platform $XPLAT_BASE + done + ;; platform-all) # build all platforms with JS and Erlang support shift for plat in $DEBIANS $UBUNTUS $CENTOSES; do build-platform $plat $* done + for arch in $XPLAT_ARCHES; do + CONTAINERARCH=$arch build-platform $XPLAT_BASE + done ERLANGVERSION=all build-platform $ERLANGALL_BASE ;; platform-upload) @@ -208,6 +266,9 @@ case "$1" in for plat in $DEBIANS $UBUNTUS $CENTOSES; do upload-platform $plat $* done + for arch in $XPLAT_ARCHES; do + CONTAINERARCH=$arch upload-platform $XPLAT_BASE $* + done ERLANGVERSION=all upload-platform $ERLANGALL_BASE ;; couch) @@ -232,16 +293,24 @@ case "$1" in $0 <command> [OPTIONS] Recognized commands: - clean <plat> Removes all images for <plat>. - clean-all Removes all images for all platforms. + clean <plat> Removes all images for <plat>. + clean-all Removes all images for all platforms. + + *buildx-base <plat> Builds a multi-architecture base image. + *buildx-platform <plat> Builds a multi-architecture image with Erlang & JS support. + + base <plat> Builds the image for <plat> without Erlang or JS support. + base-all Builds all images without Erlang or JS support. + *base-upload <plat> Uploads the apache/couchdbci-{os} base images to Docker Hub. + *base-upload-all Uploads all the apache/couchdbci base images to Docker Hub. - platform <plat> Builds the image for <plat> with Erlang & JS support. - platform-all Builds all images with Erlang and JS support. - *platform-upload Uploads the couchdbdev/*-erlang-* images to Docker Hub. - *platform-upload-all Uploads all the couchdbdev/*-erlang-* images to Docker. + platform <plat> Builds the image for <plat> with Erlang & JS support. + platform-all Builds all images with Erlang and JS support. + *platform-upload <plat> Uploads the apache/couchdbci-{os} images to Docker Hub. + *platform-upload-all Uploads all the apache/couchdbci images to Docker Hub. - couch <plat> Builds and tests CouchDB for <plat>. - couch-all Builds and tests CouchDB on all platforms. + couch <plat> Builds and tests CouchDB for <plat>. + couch-all Builds and tests CouchDB on all platforms. Commands marked with * require appropriate Docker Hub credentials. diff --git a/dockerfiles/debian-bullseye b/dockerfiles/debian-bullseye index ca91c08..0a79050 100644 --- a/dockerfiles/debian-bullseye +++ b/dockerfiles/debian-bullseye @@ -17,7 +17,9 @@ # NOTE: These are intended to be built using the arguments as # described in ../build.sh. See that script for more details. -FROM debian:bullseye +ARG repository=debian + +FROM $repository:bullseye # Choose whether to install SpiderMonkey 1.8.5, default yes ARG js=js diff --git a/dockerfiles/debian-buster b/dockerfiles/debian-buster index 8621fbd..8bf78eb 100644 --- a/dockerfiles/debian-buster +++ b/dockerfiles/debian-buster @@ -17,8 +17,9 @@ # NOTE: These are intended to be built using the arguments as # described in ../build.sh. See that script for more details. -# Support multi-platform builds with a single Dockerfile -FROM debian:buster +ARG repository=debian + +FROM $repository:buster # Choose whether to install SpiderMonkey 1.8.5, default yes ARG js=js diff --git a/dockerfiles/debian-stretch b/dockerfiles/debian-stretch index 10f3ca5..5ab121c 100644 --- a/dockerfiles/debian-stretch +++ b/dockerfiles/debian-stretch @@ -17,7 +17,9 @@ # NOTE: These are intended to be built using the arguments as # described in ../build.sh. See that script for more details. -FROM debian:stretch +ARG repository=debian + +FROM $repository:stretch # Choose whether to install SpiderMonkey 1.8.5, default yes ARG js=js
