Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker buildx support in cico #483

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cico_build_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export SCRIPT_DIR

load_jenkins_vars
install_deps
check_buildx_support
build_and_push
1 change: 1 addition & 0 deletions cico_build_nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export SCRIPT_DIR
load_jenkins_vars
install_deps
set_nightly_tag
check_buildx_support
build_and_push
1 change: 1 addition & 0 deletions cico_build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export SCRIPT_DIR
load_jenkins_vars
install_deps
set_release_tag
check_buildx_support
build_and_push
53 changes: 45 additions & 8 deletions cico_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ function load_jenkins_vars() {
fi
}

# 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_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_supported_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() {
# We need to disable selinux for now, XXX
/usr/sbin/setenforce 0 || true
Expand All @@ -46,6 +72,14 @@ function install_deps() {
git

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:a7996909642ee92942dcd6cff44b9b95f08dad64
docker run --rm --privileged multiarch/qemu-user-static:4.2.0-7 --reset -p yes
echo 'CICO: Dependencies installed'
}

Expand All @@ -68,10 +102,9 @@ function set_git_commit_tag() {
export GIT_COMMIT_TAG
}

function tag_push() {
function build_and_push_using_buildx() {
local TARGET=$1
docker tag "${IMAGE}" "$TARGET"
docker push "$TARGET" | cat
docker buildx build --platform=linux/amd64,linux/s390x -t "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TARGET}" -f ./build/dockerfiles/${DOCKERFILE} --target registry --push --progress plain --no-cache .
}

function build_and_push() {
Expand All @@ -97,15 +130,19 @@ function build_and_push() {
echo "Could not login, missing credentials for pushing to the '${ORGANIZATION}' organization"
fi

# Let's build and push image to 'quay.io' using git commit hash as tag first
# Create a new builder instance using buildx
docker buildx create --use --name plugin_builder
docker buildx inspect --bootstrap

set_git_commit_tag
docker build -t ${IMAGE} -f ./build/dockerfiles/${DOCKERFILE} --target registry . | cat
tag_push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${GIT_COMMIT_TAG}"

# Let's build and push image to 'quay.io' using git commit hash as tag first
docker buildx build --platform=linux/amd64,linux/s390x -t "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${GIT_COMMIT_TAG}" -f ./build/dockerfiles/${DOCKERFILE} --target registry --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 tag the image accordingly and also push to 'quay.io'
# 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}"
build_and_push_using_buildx "${TAG}"
echo "CICO: '${TAG}' version of images pushed to '${REGISTRY}/${ORGANIZATION}' organization"
fi
}