From b970b3b45133c8d7dd8b6cd1054d2d04af286f8d Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Mon, 1 Jun 2020 17:56:55 +0530 Subject: [PATCH 1/5] Added buildx support in cico scripts. Signed-off-by: Prabhav Thali --- build/dockerfiles/Dockerfile | 4 +-- cico_build_ci.sh | 1 + cico_build_nightly.sh | 1 + cico_build_release.sh | 1 + cico_functions.sh | 49 ++++++++++++++++++++++++++++-------- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/build/dockerfiles/Dockerfile b/build/dockerfiles/Dockerfile index fcea90b12..88cc905f3 100644 --- a/build/dockerfiles/Dockerfile +++ b/build/dockerfiles/Dockerfile @@ -11,7 +11,7 @@ # # Dockerfile defines che-machine-exec production image eclipse/che-machine-exec-dev # -FROM golang:1.12.8-alpine as go_builder +FROM --platform=$TARGETPLATFORM golang:1.12.8-alpine as go_builder ENV USER=machine-exec ENV UID=12345 @@ -37,7 +37,7 @@ WORKDIR /che-machine-exec/ COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags '-w -s' -a -installsuffix cgo -o /go/bin/che-machine-exec . -FROM node:10.16-alpine as cloud_shell_builder +FROM --platform=$TARGETPLATFORM node:10.16-alpine as cloud_shell_builder COPY --from=go_builder /che-machine-exec/cloud-shell cloud-shell-src WORKDIR cloud-shell-src RUN yarn && \ diff --git a/cico_build_ci.sh b/cico_build_ci.sh index 6c19efb87..4e0c9a2aa 100644 --- a/cico_build_ci.sh +++ b/cico_build_ci.sh @@ -22,4 +22,5 @@ export SCRIPT_DIR load_jenkins_vars install_deps +check_buildx_support build_and_push diff --git a/cico_build_nightly.sh b/cico_build_nightly.sh index 57ea13a20..24bd5be42 100644 --- a/cico_build_nightly.sh +++ b/cico_build_nightly.sh @@ -22,5 +22,6 @@ export SCRIPT_DIR load_jenkins_vars install_deps +check_buildx_support set_nightly_tag build_and_push diff --git a/cico_build_release.sh b/cico_build_release.sh index 422a41ca1..7013343dd 100644 --- a/cico_build_release.sh +++ b/cico_build_release.sh @@ -22,5 +22,6 @@ export SCRIPT_DIR load_jenkins_vars install_deps +check_buildx_support set_release_tag build_and_push diff --git a/cico_functions.sh b/cico_functions.sh index a7a76df57..b5d15a679 100644 --- a/cico_functions.sh +++ b/cico_functions.sh @@ -33,6 +33,27 @@ function load_jenkins_vars() { fi } +function check_buildx_support() { + export DOCKER_BUILD_KIT=1 + export DOCKER_CLI_EXPERIMENTAL=enabled + + docker_version="$(docker --version | cut -d' ' -f3 | tr -cd '0-9.')" + if [[ $docker_version < 19.03 ]]; then + echo "CICO: Docker $docker_version greater than or equal to 19.03 is required." + exit 1 + fi + + # Kernel + kernel_version="$(uname -r)" + if [[ "$(version "$kernel_version")" < "$(version '4.8')" ]]; then + echo "Kernel $kernel_version too old - need >= 4.8." \ + " Install a newer kernel." + else + echo "kernel $kernel_version has binfmt_misc fix-binary (F) support." + fi + +} + function install_deps() { # We need to disable selinux for now, XXX /usr/sbin/setenforce 0 || true @@ -44,9 +65,18 @@ function install_deps() { git service docker start + + #Enable qemu and binfmt support + docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + echo 'CICO: Dependencies installed' } +function version() { + printf '%02d' $(echo "$1" | tr . ' ' | sed -e 's/ 0*/ /g') 2>/dev/null +} + function set_release_tag() { # Let's obtain the tag based on the # version defined in the 'VERSION' file @@ -66,12 +96,6 @@ function set_git_commit_tag() { export GIT_COMMIT_TAG } -function tag_push() { - local TARGET=$1 - docker tag "${IMAGE}" "$TARGET" - docker push "$TARGET" | cat -} - function build_and_push() { REGISTRY="quay.io" DOCKERFILE="Dockerfile" @@ -86,13 +110,16 @@ function build_and_push() { # Let's build and push image to 'quay.io' using git commit hash as tag first set_git_commit_tag - docker build -t ${IMAGE} -f ./build/dockerfiles/${DOCKERFILE} . | cat - tag_push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${GIT_COMMIT_TAG}" - echo "CICO: '${GIT_COMMIT_TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization" - # If additional tag is set (e.g. "nightly"), let's tag the image accordingly and also push to 'quay.io' + # Create a new builder instance using buildx + docker buildx create --use --name builder + docker buildx inspect --bootstrap + docker buildx build --platform linux/amd64,linux/s390x -t ${REGISTRY}/${ORGANIZATION}/${IMAGE}:${GIT_COMMIT_TAG} -f ./build/dockerfiles/${DOCKERFILE} --push --progress plain --no-cache . + echo "CICO: '${GIT_COMMIT_TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization" + + # If additional tag is set (e.g. "nightly"), let's build the image accordingly and also push to 'quay.io' if [ -n "${TAG}" ]; then - tag_push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TAG}" + docker buildx build --platform linux/amd64,linux/s390x -t ${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TAG} -f ./build/dockerfiles/${DOCKERFILE} --push --progress plain --no-cache . echo "CICO: '${TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization" fi } From cf3297a74924ad0a057888943bea8a8d0a2480fb Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Fri, 5 Jun 2020 18:25:41 +0530 Subject: [PATCH 2/5] Used version() to validate docker version Signed-off-by: Prabhav Thali --- build/dockerfiles/Dockerfile | 4 ++-- cico_functions.sh | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/build/dockerfiles/Dockerfile b/build/dockerfiles/Dockerfile index 88cc905f3..fcea90b12 100644 --- a/build/dockerfiles/Dockerfile +++ b/build/dockerfiles/Dockerfile @@ -11,7 +11,7 @@ # # Dockerfile defines che-machine-exec production image eclipse/che-machine-exec-dev # -FROM --platform=$TARGETPLATFORM golang:1.12.8-alpine as go_builder +FROM golang:1.12.8-alpine as go_builder ENV USER=machine-exec ENV UID=12345 @@ -37,7 +37,7 @@ WORKDIR /che-machine-exec/ COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags '-w -s' -a -installsuffix cgo -o /go/bin/che-machine-exec . -FROM --platform=$TARGETPLATFORM node:10.16-alpine as cloud_shell_builder +FROM node:10.16-alpine as cloud_shell_builder COPY --from=go_builder /che-machine-exec/cloud-shell cloud-shell-src WORKDIR cloud-shell-src RUN yarn && \ diff --git a/cico_functions.sh b/cico_functions.sh index b5d15a679..995d1ce2f 100644 --- a/cico_functions.sh +++ b/cico_functions.sh @@ -34,12 +34,9 @@ function load_jenkins_vars() { } function check_buildx_support() { - export DOCKER_BUILD_KIT=1 - export DOCKER_CLI_EXPERIMENTAL=enabled - docker_version="$(docker --version | cut -d' ' -f3 | tr -cd '0-9.')" - if [[ $docker_version < 19.03 ]]; then - echo "CICO: Docker $docker_version greater than or equal to 19.03 is required." + if [[ "$(version "$docker_version")" < "$(version '19.03')" ]]; then + echo "CICO: Docker $docker_version is too old. Greater than or equal to 19.03 is required." exit 1 fi @@ -66,6 +63,10 @@ function install_deps() { service docker start + #set buildx env + export DOCKER_BUILD_KIT=1 + export DOCKER_CLI_EXPERIMENTAL=enabled + #Enable qemu and binfmt support docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d docker run --rm --privileged multiarch/qemu-user-static --reset -p yes From f868cfce27f0b83af72acdc2fefde8f380c32639 Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Thu, 25 Jun 2020 15:58:50 +0530 Subject: [PATCH 3/5] sort is used for version check Signed-off-by: Prabhav Thali --- cico_functions.sh | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/cico_functions.sh b/cico_functions.sh index 995d1ce2f..a5b840588 100644 --- a/cico_functions.sh +++ b/cico_functions.sh @@ -33,22 +33,28 @@ function load_jenkins_vars() { fi } +function check_version() { + local query=$1 + local target=$2 + echo "$target" "$query" | tr ' ' '\n' | sort -V | head -n1 2> /dev/null +} + function check_buildx_support() { docker_version="$(docker --version | cut -d' ' -f3 | tr -cd '0-9.')" - if [[ "$(version "$docker_version")" < "$(version '19.03')" ]]; then - echo "CICO: Docker $docker_version is too old. Greater than or equal to 19.03 is required." + if [[ $(check_version "$docker_version" "19.03") != 19.03 ]]; then + echo "CICO: Docker $docker_version greater than or equal to 19.03 is required." exit 1 - fi - - # Kernel - kernel_version="$(uname -r)" - if [[ "$(version "$kernel_version")" < "$(version '4.8')" ]]; then - echo "Kernel $kernel_version too old - need >= 4.8." \ - " Install a newer kernel." else - echo "kernel $kernel_version has binfmt_misc fix-binary (F) support." + # Kernel + kernel_version="$(uname -r)" + if [[ $(check_version "$kernel_version" "4.8") != "4.8" ]]; then + echo "Kernel $kernel_version too old - need >= 4.8." \ + " Install a newer kernel." + exit 1 + else + echo "kernel $kernel_version has binfmt_misc fix-binary (F) support." + fi fi - } function install_deps() { @@ -74,10 +80,6 @@ function install_deps() { echo 'CICO: Dependencies installed' } -function version() { - printf '%02d' $(echo "$1" | tr . ' ' | sed -e 's/ 0*/ /g') 2>/dev/null -} - function set_release_tag() { # Let's obtain the tag based on the # version defined in the 'VERSION' file From da4897c6f41a2618ce4b66a81d6904761f62b3fd Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Thu, 25 Jun 2020 16:03:30 +0530 Subject: [PATCH 4/5] Using tagged image for qemu Signed-off-by: Prabhav Thali --- cico_functions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cico_functions.sh b/cico_functions.sh index a5b840588..28da23750 100644 --- a/cico_functions.sh +++ b/cico_functions.sh @@ -75,7 +75,7 @@ function install_deps() { #Enable qemu and binfmt support docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + docker run --rm --privileged multiarch/qemu-user-static:4.2.0-7 --reset -p yes echo 'CICO: Dependencies installed' } From 4c0295689ed67270056f5326c93f61f7ba53a37a Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Wed, 15 Jul 2020 17:57:18 +0530 Subject: [PATCH 5/5] Added docs for function Renamed method check_version Extracted docker buildx in separate method Signed-off-by: Prabhav Thali --- cico_functions.sh | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cico_functions.sh b/cico_functions.sh index 28da23750..5ae45ff32 100644 --- a/cico_functions.sh +++ b/cico_functions.sh @@ -33,21 +33,23 @@ function load_jenkins_vars() { fi } -function check_version() { +# Sorts two versions and returns the lower version to check if its supported for using docker buildx. +function check_supported_version() { local query=$1 local target=$2 echo "$target" "$query" | tr ' ' '\n' | sort -V | head -n1 2> /dev/null } +# Checks whether docker and kernel versions are greater than or equal to the specified version, such that docker buildx requirements are met. function check_buildx_support() { docker_version="$(docker --version | cut -d' ' -f3 | tr -cd '0-9.')" - if [[ $(check_version "$docker_version" "19.03") != 19.03 ]]; then + if [[ $(check_supported_version "$docker_version" "19.03") != 19.03 ]]; then echo "CICO: Docker $docker_version greater than or equal to 19.03 is required." exit 1 else # Kernel kernel_version="$(uname -r)" - if [[ $(check_version "$kernel_version" "4.8") != "4.8" ]]; then + if [[ $(check_supported_version "$kernel_version" "4.8") != "4.8" ]]; then echo "Kernel $kernel_version too old - need >= 4.8." \ " Install a newer kernel." exit 1 @@ -74,7 +76,7 @@ function install_deps() { export DOCKER_CLI_EXPERIMENTAL=enabled #Enable qemu and binfmt support - docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d + docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64 docker run --rm --privileged multiarch/qemu-user-static:4.2.0-7 --reset -p yes echo 'CICO: Dependencies installed' @@ -99,6 +101,11 @@ function set_git_commit_tag() { export GIT_COMMIT_TAG } +function build_and_push_using_buildx() { + local TARGET=$1 + docker buildx build --platform linux/amd64,linux/s390x -t "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TARGET}" -f ./build/dockerfiles/${DOCKERFILE} --push --progress plain --no-cache . +} + function build_and_push() { REGISTRY="quay.io" DOCKERFILE="Dockerfile" @@ -117,12 +124,12 @@ function build_and_push() { # Create a new builder instance using buildx docker buildx create --use --name builder docker buildx inspect --bootstrap - docker buildx build --platform linux/amd64,linux/s390x -t ${REGISTRY}/${ORGANIZATION}/${IMAGE}:${GIT_COMMIT_TAG} -f ./build/dockerfiles/${DOCKERFILE} --push --progress plain --no-cache . + build_and_push_using_buildx "${GIT_COMMIT_TAG}" echo "CICO: '${GIT_COMMIT_TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization" # If additional tag is set (e.g. "nightly"), let's build the image accordingly and also push to 'quay.io' if [ -n "${TAG}" ]; then - docker buildx build --platform linux/amd64,linux/s390x -t ${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TAG} -f ./build/dockerfiles/${DOCKERFILE} --push --progress plain --no-cache . + build_and_push_using_buildx "${TAG}" echo "CICO: '${TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization" fi }