This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·110 lines (90 loc) · 3.79 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
107
108
109
110
#!/usr/bin/env bash
set -o errexit # abort on nonzero exit status
set -o pipefail # don't hide errors within pipes
version="$1"
if [ "${version}" == "" ]; then
echo >&2 "The first parameter to the build script must be the nginx version"
exit 1
fi
dockerfile="$2"
if [ "${dockerfile}" == "" ]; then
echo >&2 "The second parameter to the build script must be the relative dockerfile path"
exit 1
fi
set -o nounset # abort on unbound variable
if ! command -v docker > /dev/null; then
echo >&2 "Docker must be installed to run build"
exit 1
fi
if ! command -v curl > /dev/null; then
echo >&2 "curl must be installed to run build"
exit 1
fi
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
if [ ! -d "${script_dir}/.gnupg" ]; then
echo >&2 "local gnupg directory not found at path: ${script_dir}/.gnupg"
exit 1
fi
arch=""
case $(uname -m) in
i386) arch="386" ;;
i686) arch="386" ;;
x86_64) arch="amd64" ;;
aarch64) arch="arm64" ;;
arm) dpkg --print-architecture | grep -q "arm64" && arch="arm64" || arch="arm" ;;
*)
echo >&2 "Unable to determine system architecture."
exit 1
;;
esac
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
function lib_name() {
distro_name="${1##*.}"
if [ "${distro_name}" == "alpine" ]; then
echo 'musl'
else
echo 'libc'
fi
}
echo "Building images for NGINX ${version}"
# Download NGINX source code
download_dir="${script_dir}/downloads"
if [ ! -d "${download_dir}" ]; then
mkdir --parents "${download_dir}"
fi
container_images="${script_dir}/container_images-$(basename ${dockerfile})-${version}.txt"
# Build container images
if [ -f "${container_images}" ]; then
rm "${container_images}"
fi
dirname="$(dirname "${dockerfile}")"
tag_name="$(basename "${dirname}")"
lib_name="$(lib_name "$dockerfile")"
# Enable docker build kit
export DOCKER_BUILDKIT=1
# Ensure permissions are set correctly for gpg data directory
chmod 700 "${script_dir}/.gnupg"
# Base images need to be processed differently because they are squashed and
# they do not require downloading NGINX source code.
if echo "$dockerfile" | grep --quiet "base"; then
docker build --file "${dockerfile}" --build-arg ARCH="${arch}" --tag "${arch}/${tag_name}-base:${os}-${lib_name}" "${script_dir}"
docker tag "${arch}/${tag_name}-base:${os}-${lib_name}" "ghcr.io/nginxinc/${arch}/${tag_name}-base:${os}-${lib_name}"
echo "${arch}/${tag_name}-base:${os}-${lib_name}" >> "${container_images}"
else
if [ ! -f "${script_dir}/downloads/nginx-${version}.tar.gz.asc" ]; then
echo "Downloading http://nginx.org/download/nginx-${version}.tar.gz.asc -> ${script_dir}/downloads/nginx-${version}.tar.gz.asc"
curl --retry 6 --fail --show-error --silent --location --output "${script_dir}/downloads/nginx-${version}.tar.gz.asc" "http://nginx.org/download/nginx-${version}.tar.gz.asc"
fi
if [ ! -f "${script_dir}/downloads/nginx-${version}.tar.gz" ]; then
echo "Downloading http://nginx.org/download/nginx-${version}.tar.gz -> ${script_dir}/downloads/nginx-${version}.tar.gz"
curl --retry 6 --fail --show-error --silent --location --output "${script_dir}/downloads/nginx-${version}.tar.gz" "http://nginx.org/download/nginx-${version}.tar.gz"
if ! gpg --homedir "${script_dir}/.gnupg" --verify "${script_dir}/downloads/nginx-${version}.tar.gz.asc" "${script_dir}/downloads/nginx-${version}.tar.gz"; then
echo >&2 "Could not verify integrity of NGINX archive: ${script_dir}/downloads/nginx-${version}.tar.gz"
exit 2
fi
fi
docker build --file "${dockerfile}" --build-arg ARCH="${arch}" --build-arg NGX_VERSION="${version}" --tag "${arch}/${tag_name}:${os}-${lib_name}-nginx-${version}" "${script_dir}"
echo "${arch}/${tag_name}:${os}-${lib_name}-nginx-${version}" >> "${container_images}"
fi
echo "Created the following container images:"
cat "${container_images}"