From 619bee4965ffb463f3ad4b95858147c3832fa56b Mon Sep 17 00:00:00 2001 From: Yoann Dubreuil Date: Fri, 24 Jun 2016 11:30:13 +0200 Subject: [PATCH] local build script to build the docker image (#268) * local build script to build inside a docker image Simulate the Jenkins Pipeline docker step locally so BlueOcean is built like in Jenkins * document usage and add a command to build a BlueOcean docker image * Rename to build-in-docker.sh --- .gitignore | 1 + build-in-docker.sh | 159 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 build-in-docker.sh diff --git a/.gitignore b/.gitignore index 5db2d3ac..1f425ae6 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ npm-debug.log **/.project **/.classpath blueocean-web/src/main/webapp/ +.build_container diff --git a/build-in-docker.sh b/build-in-docker.sh new file mode 100755 index 00000000..5adbb95c --- /dev/null +++ b/build-in-docker.sh @@ -0,0 +1,159 @@ +#!/usr/bin/env bash +set -eu -o pipefail + +HERE="$(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" + +setup_nice_output() { + # check if stdout is a terminal... + if [ -t 1 ]; then + + # see if it supports colors... + ncolors=$(tput colors) + + if test -n "$ncolors" && test $ncolors -ge 8; then + bold="$(tput bold)" + underline="$(tput smul)" + standout="$(tput smso)" + normal="$(tput sgr0)" + black="$(tput setaf 0)" + red="$(tput setaf 1)" + green="$(tput setaf 2)" + yellow="$(tput setaf 3)" + blue="$(tput setaf 4)" + magenta="$(tput setaf 5)" + cyan="$(tput setaf 6)" + white="$(tput setaf 7)" + fi + fi +} + +new_build_container() { + local build_image=$1; shift + + build_container=$(docker create -i -v "$HERE":/build -w /build "$build_image" /bin/cat) + echo "$build_container" > "$HERE/.build_container" +} + +delete_build_container() { + docker rm "$build_container" + rm "$HERE/.build_container" +} + +stop_build_container() { + echo "${yellow}=> ${normal}Stopping build container $build_container" + local error; error=$(docker kill "$build_container") + if [[ $? -ne 0 ]]; then echo "$error"; exit 1; fi + trap EXIT +} + +stop_trap() { + stop_build_container + exit 1 +} + +prepare_build_container() { + local build_image=$1; shift + if [[ -f $HERE/.build_container ]]; then + read -r build_container < "$HERE/.build_container" + else + new_build_container "$build_image" + return + fi + + if [[ "$clean" = true ]]; then + echo "${yellow}=> ${normal}Removing old build container ${build_container}" + docker kill "$build_container" || true + docker rm "$build_container" || true + rm "$HERE/.build_container" + else + local state; state=$(docker inspect --format="{{ .State.Status }}" "$build_container") + if [[ $? -ne 0 || "$state" != "exited" ]]; then + echo "${red}ERROR: ${normal}Build container $build_container is not in a re-usable state, is there another build running?" + exit 1 + fi + + # was the build image updated manually with a docker pull? + local image_id; image_id=$(docker inspect --format="{{ .Id }}" "$build_image") + local container_image_id; container_image_id=$(docker inspect --format="{{ .Image }}" "$build_container") + if [[ "$image_id" != "$container_image_id" ]]; then + echo "${yellow}WARNING ${normal}Build container is not using the latest available image. Consider using '-c' to get a fresh build container using latest image" + fi + fi + + new_build_container "$build_image" +} + +# simulate Jenkins Pipeline docker.image().inside {} +build_inside() { + local build_image=$1; shift + prepare_build_container "$build_image" + + trap stop_trap EXIT + echo "${yellow}=> ${normal}Starting build container ${build_container}" + local error; error=$(docker start "$build_container") + if [[ $? -ne 0 ]]; then echo "$error"; exit 1; fi + + echo "${yellow}=> ${normal}Launching ${cyan}'${build_commands}'${normal}" + echo "$build_commands" | docker exec -i -u "$(id -u)" "$build_container" /bin/bash + stop_build_container +} + +make_image() { + echo "${yellow}=> ${normal}Building BlueOcean docker image ${tag_name}" + (cd "$HERE" && docker build -t "$tag_name" . ) +} + +build_commands="mvn clean install -B -DcleanNode -Dmaven.test.failure.ignore" +tag_name=blueocean-local + +usage() { +cat <