forked from reactioncommerce/docker-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerfiles.sh
executable file
·66 lines (56 loc) · 1.87 KB
/
dockerfiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml
# ---- Start unofficial bash strict mode boilerplate
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix # more strict failures in subshells
# set -x # enable debugging
IFS=$'\n\t'
# ---- End unofficial bash strict mode boilerplate
dockerfiles_to_process() {
declare -a args=($@)
if [[ ${#args[@]} -gt 0 ]]; then
printf '%s\n' "${args[@]}"
return
fi
git ls-files | grep -e '/Dockerfile$'
}
main() {
cd "$(dirname "${BASH_SOURCE[0]}")"
root_dir="$(pwd -P)"
root_build_dir="${root_dir}/build"
root_build_scripts_dir="${root_build_dir}/scripts"
root_scripts_dir="${root_dir}/scripts"
subcommand="$1"
shift
dockerfiles_to_process "$@" | {
while IFS= read -r file_path; do
name=$(echo "${file_path}" | awk -F / '{print $(NF-2)}')
context_dir=$(dirname "${file_path}")
tag=$(echo "${file_path}" | awk -F / '{print $(NF-1)}')
case "${subcommand}" in
build)
# Clear build dir
rm -rf "${root_build_dir}"
# Copy root /scripts and image context to build dir. Copy image-specific
# scripts second so that they could overwrite root scripts if necessary.
mkdir -p "${root_build_scripts_dir}"
cp -a "${root_scripts_dir}/." "${root_build_scripts_dir}"
cp -a "${context_dir}/." "${root_build_dir}"
# Do build
docker build -t "reactioncommerce/${name}:${tag}" "${root_build_dir}"
;;
push)
docker push "reactioncommerce/${name}:${tag}"
;;
*)
echo "Usage $0 <build|push> [dockerfile_path...]" 1>&2
exit 1
;;
esac
done
}
}
main "$@"