-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-build.sh
executable file
·98 lines (80 loc) · 2.45 KB
/
docker-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
#!/usr/bin/env bash
# SPDX-FileCopyrightText: © 2020 Sebastian Davids <[email protected]>
# SPDX-License-Identifier: Apache-2.0
set -Eeu -o pipefail -o posix
while getopts ':d:np:t:' opt; do
case "${opt}" in
d) dockerfile="${OPTARG}"
;;
n) no_cache='--pull --no-cache'
;;
p) port="${OPTARG}"
;;
t) tag="${OPTARG}"
;;
?)
echo "Usage: $0 [-d Dockerfile] [-n] [-t tag]" >&2
exit 1
;;
esac
done
readonly dockerfile="${dockerfile:-$PWD/Dockerfile}"
readonly no_cache="${no_cache:-}"
readonly port="${port:-3000}"
readonly tag="${tag:-local}"
if [ ! -f "${dockerfile}" ]; then
echo "Dockerfile '${dockerfile}' does not exist" >&2
exit 2
fi
# https://docs.docker.com/reference/cli/docker/image/tag/#description
readonly namespace='de.sdavids'
readonly repository='sdavids-node-docker-image-slimming'
readonly label_group='de.sdavids.docker.group'
readonly label="${label_group}=${repository}"
readonly image_name="${namespace}/${repository}"
# https://reproducible-builds.org/docs/source-date-epoch/
if [ -z "${SOURCE_DATE_EPOCH:-}" ]; then
if [ -z "$(git status --porcelain=v1 2>/dev/null)" ]; then
SOURCE_DATE_EPOCH="$(git log --max-count=1 --pretty=format:%ct)"
else
SOURCE_DATE_EPOCH="$(date +%s)"
fi
export SOURCE_DATE_EPOCH
fi
if [ "$(uname)" = 'Darwin' ]; then
created_at="$(date -r "${SOURCE_DATE_EPOCH}" -Iseconds -u | sed -e 's/+00:00$/Z/')"
else
created_at="$(date -d "@${SOURCE_DATE_EPOCH}" -Iseconds -u | sed -e 's/+00:00$/Z/')"
fi
readonly created_at
if [ -n "${GITHUB_SHA:-}" ]; then
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
commit="${GITHUB_SHA}"
elif [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != 'true' ]; then
commit='N/A'
else
if [ -z "$(git status --porcelain=v1 2>/dev/null)" ]; then
ext=''
else
ext='-next'
fi
commit="$(git rev-parse --verify HEAD)${ext}"
unset ext
fi
readonly commit
# to ensure ${label} is set, we use --label "${label}"
# which might overwrite the LABEL ${label_group} of the Dockerfile
# shellcheck disable=SC2086
docker image build \
${no_cache} \
--file "${dockerfile}" \
--compress \
--tag "${image_name}:latest" \
--tag "${image_name}:${tag}" \
--build-arg "git_commit=${commit}" \
--build-arg "created_at=${created_at}" \
--build-arg "port=${port}" \
--label "${label}" \
.
echo
docker image inspect -f '{{json .Config.Labels}}' "${image_name}:${tag}"