| #!/bin/bash | |
| # Copyright (C) 2018 The Android Open Source Project | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http:#www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| usage() { | |
| me=`basename "$0"` | |
| echo >&2 "Usage: $me [--help] [--tag TAG] [--no-cache] [--gerrit-url URL] [--base-image IMAGE] [--platform PLATFORM] [--branch GERRIT_BRANCH] [--buildah] [IMAGE]" | |
| exit 1 | |
| } | |
| BUILD_TOOL="docker" | |
| while test $# -gt 0 ; do | |
| case "$1" in | |
| --help) | |
| usage | |
| ;; | |
| --no-cache) | |
| NO_CACHE=true | |
| shift | |
| ;; | |
| --tag) | |
| shift | |
| TAG=$1 | |
| shift | |
| ;; | |
| --gerrit-url) | |
| shift | |
| GERRIT_WAR_URL=$1 | |
| shift | |
| ;; | |
| --healthcheck-jar-url) | |
| shift | |
| HEALTHCHECK_JAR_URL=$1 | |
| shift | |
| ;; | |
| --base-image) | |
| shift | |
| BASE_IMAGE=$1 | |
| shift | |
| ;; | |
| --platform) | |
| shift | |
| PLATFORM=$1 | |
| shift | |
| ;; | |
| --branch) | |
| shift | |
| GERRIT_BRANCH=$1 | |
| shift | |
| ;; | |
| --buildah) | |
| BUILD_TOOL="buildah" | |
| shift | |
| ;; | |
| *) | |
| break | |
| esac | |
| done | |
| #Get list of images | |
| source container-images/publish_list | |
| IMAGES=$(get_image_list) | |
| PLATFORM=${PLATFORM:-linux/amd64} # Default value if PLATFORM is not set | |
| GERRIT_BRANCH=${GERRIT_BRANCH:-master} # Default value if GERRIT_BRANCH is not set | |
| DOCKER_BUILD_OPTS="--platform=$PLATFORM" | |
| if [[ $BUILD_TOOL = buildah ]]; then | |
| DOCKER_BUILD_OPTS="$DOCKER_BUILD_OPTS --isolation=chroot" | |
| GERRIT_VER_OPTS="$GERRIT_VER_OPTS --no-docker" | |
| fi | |
| test -n "$NO_CACHE" && DOCKER_BUILD_OPTS="$DOCKER_BUILD_OPTS --no-cache" | |
| if test -n "$GERRIT_WAR_URL"; then | |
| BUILD_ARGS="--build-arg GERRIT_WAR_URL=$GERRIT_WAR_URL" | |
| GERRIT_VER_OPTS="$GERRIT_VER_OPTS --gerrit-url $GERRIT_WAR_URL" | |
| fi | |
| if test -n "$HEALTHCHECK_JAR_URL"; then | |
| BUILD_ARGS="$BUILD_ARGS --build-arg HEALTHCHECK_JAR_URL=$HEALTHCHECK_JAR_URL" | |
| fi | |
| export K8SGERRIT_VER="$(./get_version.sh --output K8SGERRIT)" | |
| docker_build(){ | |
| IMAGE=$1 | |
| $BUILD_TOOL build \ | |
| $DOCKER_BUILD_OPTS \ | |
| --build-arg TAG=$K8SGERRIT_VER \ | |
| --build-arg GERRIT_VER="$GERRIT_VER" \ | |
| -t k8sgerrit/$IMAGE:$TAG \ | |
| ./container-images/$IMAGE | |
| if test $? -ne 0; then | |
| REPORT="$REPORT Failed: k8sgerrit/$IMAGE.\n" | |
| RETURN_CODE=1 | |
| else | |
| REPORT="$REPORT Success: k8sgerrit/$IMAGE:$TAG\n" | |
| fi | |
| } | |
| docker_build_gerrit_base(){ | |
| BUILD_ARGS="$BUILD_ARGS --build-arg TAG=$K8SGERRIT_VER --build-arg GERRIT_BRANCH=$GERRIT_BRANCH" | |
| $BUILD_TOOL build \ | |
| $DOCKER_BUILD_OPTS \ | |
| $BUILD_ARGS \ | |
| -t gerrit-base:$K8SGERRIT_VER \ | |
| ./container-images/gerrit-base | |
| if test $? -ne 0; then | |
| echo -e "\n\nFailed to build gerrit-base image." | |
| exit 1 | |
| fi | |
| export GERRIT_VER="$(./get_version.sh --output GERRIT --platform $PLATFORM $GERRIT_VER_OPTS)" | |
| if test -z "$TAG"; then | |
| export TAG="$(./get_version.sh --platform $PLATFORM $GERRIT_VER_OPTS)" | |
| fi | |
| } | |
| REPORT="Build results: \n" | |
| RETURN_CODE=0 | |
| if test -n "$BASE_IMAGE"; then | |
| BASE_BUILD_ARGS="--build-arg BASE_IMAGE=$BASE_IMAGE" | |
| fi | |
| $BUILD_TOOL build $DOCKER_BUILD_OPTS $BASE_BUILD_ARGS -t base:$K8SGERRIT_VER ./container-images/base | |
| if test $? -ne 0; then | |
| echo -e "\n\nFailed to build base image." | |
| exit 1 | |
| fi | |
| if test $# -eq 0 ; then | |
| docker_build_gerrit_base | |
| for IMAGE in $IMAGES; do | |
| docker_build $IMAGE | |
| done | |
| else | |
| while test $# -gt 0 ; do | |
| if [[ $1 = gerrit-* ]]; then | |
| docker_build_gerrit_base | |
| else | |
| if test -z "$TAG"; then | |
| TAG="$K8SGERRIT_VER-unknown" | |
| fi | |
| echo -e "\nNo Image containing Gerrit will be built." \ | |
| "The Gerrit-version can thus not be determinded." \ | |
| "Using tag $TAG\n" | |
| fi | |
| docker_build $1 | |
| shift | |
| done | |
| fi | |
| echo -e "\n\n$REPORT" | |
| exit $RETURN_CODE |