Enable to build ansible image out of git directory 81/75981/4
authorPetr Ospalý <p.ospaly@partner.samsung.com>
Fri, 18 Jan 2019 09:34:27 +0000 (10:34 +0100)
committerPetr Ospalý <p.ospaly@partner.samsung.com>
Thu, 24 Jan 2019 14:22:46 +0000 (15:22 +0100)
Inside the build_ansible_image.sh is a variable (label for docker image)
which is set to git commit hash. This is fine but you can just copy
and move ansible directory elsewhere out of git and this script breaks
because of this.

This change preserve previous functionality but only if the script
is run inside a git repository otherwise it will skip setting the
label for the built docker image.

Change-Id: I23f828542efddde21d1c96116de7b20272ef23ef
Issue-ID: OOM-1575
Signed-off-by: Petr Ospalý <p.ospaly@partner.samsung.com>
ansible/docker/build_ansible_image.sh

index d54ddc4..49833be 100755 (executable)
@@ -27,14 +27,12 @@ image_name="${2:-ansible:latest}"
 script_path=$(readlink -f "$0")
 script_dir=$(dirname "$script_path")
 
-git_commit=$(git rev-parse --revs-only HEAD)
+git_commit=$(git rev-parse --revs-only HEAD || true)
 build_date=$(date -I)
 
-if [ -z "$ansible_version" ]; then
-    docker build "$script_dir" -t "${image_name}" --label "git-commit=$git_commit" --label "build-date=$build_date"
-else
-    docker build "$script_dir" -t "${image_name}" --label "git-commit=$git_commit" --label "build-date=$build_date" --build-arg ansible_version="$ansible_version"
-fi
+docker build "$script_dir" -t "${image_name}" --label "build-date=$build_date" \
+    ${git_commit:+--label git-commit=${git_commit}} \
+    ${ansible_version:+--build-arg ansible_version=${ansible_version}}
 
 # Export docker image into chroot and tararchive it. It takes ~40M of space and is packaged together with sw.
 if "${script_dir}"/create_docker_chroot.sh convert "${image_name}" "${script_dir}"/ansible_chroot ; then