-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbuild.sh
executable file
·106 lines (91 loc) · 2.51 KB
/
build.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
set -e
ID_PREFIX="io.buildpacks.samples.stacks"
DEFAULT_PREFIX=cnbs/sample-base
DEFAULT_PLATFORM=amd64
REPO_PREFIX=${DEFAULT_PREFIX}
PLATFORM=${DEFAULT_PLATFORM}
usage() {
echo "Usage: "
echo " $0 [-f <prefix>] [-p <platform>] <dir>"
echo " -f prefix to use for images (default: ${DEFAULT_PREFIX})"
echo " -p prefix to use for images (default: ${DEFAULT_PLATFORM})"
echo " <dir> directory to build"
exit 1;
}
while getopts "v:p:" o; do
case "${o}" in
f)
REPO_PREFIX=${OPTARG}
;;
p)
PLATFORM=${OPTARG}
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
:)
usage
;;
esac
done
BASE_DIR=${@:$OPTIND:1}
if [[ -z ${REPO_PREFIX} ]]; then
echo "Prefix cannot be empty"
echo
usage
exit 1
fi
if [[ -z ${BASE_DIR} ]]; then
echo "Must specify directory"
echo
usage
exit 1
fi
cd $(dirname $0)
IMAGE_DIR=$(realpath "${BASE_DIR}")
TAG=$(basename "${IMAGE_DIR}")
STACK_ID="${ID_PREFIX}.$(basename "${IMAGE_DIR}")"
BASE_IMAGE=${REPO_PREFIX}:${TAG}
RUN_IMAGE=${REPO_PREFIX}-run:${TAG}
BUILD_IMAGE=${REPO_PREFIX}-build:${TAG}
FROM_IMAGE=$(head -n1 "${IMAGE_DIR}"/base/Dockerfile | cut -d' ' -f2)
# Get target distro information
if cmd /c ver; then
DISTRO_NAME=""
RAW_VERSION=$(docker run --rm "${FROM_IMAGE}" cmd /c ver)
DISTRO_VERSION=$(echo "$RAW_VERSION" | head -n1 | sed 's/Microsoft Windows //' | sed 's/[][]//g' | cut -d' ' -f2)
echo "DISTRO_VERSION: ${DISTRO_VERSION}"
else
DISTRO_NAME=$(docker run --rm "${FROM_IMAGE}" cat /etc/os-release | grep '^ID=' | cut -d'=' -f2)
echo "DISTRO_NAME: ${DISTRO_NAME}"
DISTRO_VERSION=$(docker run --rm "${FROM_IMAGE}" cat /etc/os-release | grep '^VERSION_ID=' | cut -d'=' -f2)
echo "DISTRO_VERSION: ${DISTRO_VERSION}"
fi
if [[ -d "${IMAGE_DIR}/base" ]]; then
docker build --platform=${PLATFORM} \
--build-arg "distro_name=${DISTRO_NAME}" \
--build-arg "distro_version=${DISTRO_VERSION}" \
--build-arg "stack_id=${STACK_ID}" \
-t "${BASE_IMAGE}" \
"${IMAGE_DIR}/base"
fi
echo "BUILDING ${BUILD_IMAGE}..."
docker build --platform=${PLATFORM} \
--build-arg "base_image=${BASE_IMAGE}" \
--build-arg "stack_id=${STACK_ID}" \
-t "${BUILD_IMAGE}" \
"${IMAGE_DIR}/build"
echo "BUILDING ${RUN_IMAGE}..."
docker build --platform=${PLATFORM} \
--build-arg "base_image=${BASE_IMAGE}" \
-t "${RUN_IMAGE}" \
"${IMAGE_DIR}/run"
echo
echo "BASE IMAGES BUILT!"
echo
echo "Images:"
for IMAGE in "${BASE_IMAGE}" "${BUILD_IMAGE}" "${RUN_IMAGE}"; do
echo " ${IMAGE}"
done