From 26658d920b6e10792e37a525813f9723d8d2e3b1 Mon Sep 17 00:00:00 2001 From: Matt Peters Date: Mon, 6 Jul 2020 08:22:55 -0500 Subject: [PATCH] Add configurable docker build context and file Add support for building docker images with an explicit configuration for specifying the docker context and Dockerfile. This is required for projects that build from 3rd party source that maintains a Dockerfile outside of the project repo and builds from source that is cloned by the repo tool into the project path. New Configuration Parameters: DOCKER_CONTEXT= DOCKER_FILE= NOTE: Paths are relative to the image build file. Story: 2006537 Task: 40286 Change-Id: I6520fd31218aaf849f4d1e803bb1e0bfb614b29f Signed-off-by: Matt Peters --- .../build-docker-images/build-stx-images.sh | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/build-tools/build-docker-images/build-stx-images.sh b/build-tools/build-docker-images/build-stx-images.sh index 133593a9..ca33b96e 100755 --- a/build-tools/build-docker-images/build-stx-images.sh +++ b/build-tools/build-docker-images/build-stx-images.sh @@ -420,6 +420,10 @@ function build_image_docker { # local LABEL LABEL=$(source ${image_build_file} && echo ${LABEL}) + local DOCKER_CONTEXT + DOCKER_CONTEXT=$(source ${image_build_file} && echo ${DOCKER_CONTEXT}) + local DOCKER_FILE + DOCKER_FILE=$(source ${image_build_file} && echo ${DOCKER_FILE}) local DOCKER_REPO DOCKER_REPO=$(source ${image_build_file} && echo ${DOCKER_REPO}) local DOCKER_REF @@ -441,10 +445,10 @@ function build_image_docker { echo "Building ${LABEL}" - local docker_src - if [ -z "${DOCKER_REPO}" ]; then - docker_src=$(dirname ${image_build_file})/docker - else + local real_docker_context + local real_docker_file + + if [ -n "${DOCKER_REPO}" ]; then local ORIGWD=${PWD} echo "get_git '${DOCKER_REPO}' '${DOCKER_REF}' '${DOCKER_PATCHES}'" @@ -455,27 +459,40 @@ function build_image_docker { return 1 fi - local DOCKER_FILE="${PWD}/Dockerfile" - if [ ! -f ${DOCKER_FILE} ]; then - DOCKER_FILE=$(find ${PWD} -type f -name Dockerfile | head -n 1) + real_docker_file="${PWD}/Dockerfile" + if [ ! -f ${real_docker_file} ]; then + real_docker_file=$(find ${PWD} -type f -name Dockerfile | head -n 1) fi - docker_src=$(dirname ${DOCKER_FILE}) + real_docker_context=$(dirname ${real_docker_file}) cd ${ORIGWD} + else + if [ -n "${DOCKER_CONTEXT}" ]; then + real_docker_context=$(dirname ${image_build_file})/${DOCKER_CONTEXT} + else + real_docker_context=$(dirname ${image_build_file})/docker + fi + + if [ -n "${DOCKER_FILE}" ]; then + real_docker_file=$(dirname ${image_build_file})/${DOCKER_FILE} + else + real_docker_file=${real_docker_context}/Dockerfile + fi fi # Check for a Dockerfile - if [ ! -f ${docker_src}/Dockerfile ]; then - echo "${docker_src}/Dockerfile not found" >&2 + if [ ! -f ${real_docker_file} ]; then + echo "${real_docker_file} not found" >&2 RESULTS_FAILED+=(${LABEL}) return 1 fi - # Possible design option: Make a copy of the docker_src dir in BUILDDIR + # Possible design option: Make a copy of the real_docker_context dir in BUILDDIR local build_image_name="${USER}/${LABEL}:${IMAGE_TAG_BUILD}" local -a BASE_BUILD_ARGS - BASE_BUILD_ARGS+=(${docker_src} --no-cache) + BASE_BUILD_ARGS+=(${real_docker_context} --no-cache) + BASE_BUILD_ARGS+=(--file ${real_docker_file}) BASE_BUILD_ARGS+=(--build-arg "BASE=${BASE}") if [ ! -z "$PROXY" ]; then BASE_BUILD_ARGS+=(--build-arg http_proxy=$PROXY)