2017-06-09 10:14:43 +02:00
|
|
|
#!/bin/bash
|
2019-10-15 01:00:21 +02:00
|
|
|
# Clones the Netbox repository with git from Github and builds the Dockerfile
|
2017-06-09 10:14:43 +02:00
|
|
|
|
2018-04-10 10:58:31 +02:00
|
|
|
echo "▶️ $0 $*"
|
2018-04-10 11:42:06 +02:00
|
|
|
|
2017-06-09 10:14:43 +02:00
|
|
|
set -e
|
|
|
|
|
|
|
|
if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
|
2019-02-01 09:58:07 +01:00
|
|
|
echo "Usage: ${0} <branch> [--push|--push-only]"
|
|
|
|
echo " branch The branch or tag to build. Required."
|
2019-10-14 21:54:49 +02:00
|
|
|
echo " --push Pushes the built Docker image to the registry."
|
|
|
|
echo " --push-only Only pushes the Docker image to the registry, but does not build it."
|
2017-06-09 10:14:43 +02:00
|
|
|
echo ""
|
|
|
|
echo "You can use the following ENV variables to customize the build:"
|
2019-10-14 21:54:49 +02:00
|
|
|
echo " SRC_ORG Which fork of netbox to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO})."
|
|
|
|
echo " Default: netbox-community"
|
|
|
|
echo " SRC_REPO The name of the repository to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO})."
|
|
|
|
echo " Default: netbox"
|
|
|
|
echo " URL Where to fetch the code from."
|
|
|
|
echo " Must be a git repository. Can be private."
|
|
|
|
echo " Default: https://github.com/\${SRC_ORG}/\${SRC_REPO}.git"
|
|
|
|
echo " NETBOX_PATH The path where netbox will be checkout out."
|
|
|
|
echo " Must not be outside of the netbox-docker repository (because of Docker)!"
|
|
|
|
echo " Default: .netbox"
|
|
|
|
echo " SKIP_GIT If defined, git is not invoked and \${NETBOX_PATH} will not be altered."
|
|
|
|
echo " This may be useful, if you are manually managing the NETBOX_PATH."
|
|
|
|
echo " Default: undefined"
|
|
|
|
echo " TAG The version part of the docker tag."
|
|
|
|
echo " Default:"
|
|
|
|
echo " When \${BRANCH}=master: latest"
|
|
|
|
echo " When \${BRANCH}=develop: snapshot"
|
|
|
|
echo " Else: same as \${BRANCH}"
|
|
|
|
echo " DOCKER_ORG The Docker registry (i.e. hub.docker.com/r/\${DOCKER_ORG}/\${DOCKER_REPO})"
|
|
|
|
echo " Also used for tagging the image."
|
|
|
|
echo " Default: netboxcommunity"
|
|
|
|
echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r/\${DOCKER_ORG}/\${DOCKER_REPO})"
|
|
|
|
echo " Also used for tagging the image."
|
|
|
|
echo " Default: netbox"
|
2019-10-10 12:42:10 +02:00
|
|
|
echo " DOCKER_FROM The base image to use."
|
2019-10-14 21:54:49 +02:00
|
|
|
echo " Default: Whatever is defined as default in the Dockerfile."
|
|
|
|
echo " DOCKER_TAG The name of the tag which is applied to the image."
|
|
|
|
echo " Useful for pushing into another registry than hub.docker.com."
|
|
|
|
echo " Default: \${DOCKER_ORG}/\${DOCKER_REPO}:\${TAG}"
|
2019-10-10 12:42:10 +02:00
|
|
|
echo " DOCKER_SHORT_TAG The name of the short tag which is applied to the"
|
2019-10-14 21:54:49 +02:00
|
|
|
echo " image. This is used to tag all patch releases to their"
|
|
|
|
echo " containing version e.g. v2.5.1 -> v2.5"
|
|
|
|
echo " Default: \${DOCKER_ORG}/\${DOCKER_REPO}:<MAJOR>.<MINOR>"
|
|
|
|
echo " DOCKERFILE The name of Dockerfile to use."
|
|
|
|
echo " Default: Dockerfile"
|
2019-10-10 14:48:45 +02:00
|
|
|
echo " DOCKER_TARGET A specific target to build."
|
2019-10-14 21:54:49 +02:00
|
|
|
echo " It's currently not possible to pass multiple targets."
|
|
|
|
echo " Default: main ldap"
|
|
|
|
echo " HTTP_PROXY The proxy to use for http requests."
|
|
|
|
echo " Example: http://proxy.domain.tld:3128"
|
|
|
|
echo " Default: undefined"
|
|
|
|
echo " NO_PROXY Comma-separated list of domain extensions proxy should not be used for."
|
|
|
|
echo " Example: .domain1.tld,.domain2.tld"
|
|
|
|
echo " Default: undefined"
|
|
|
|
echo " DEBUG If defined, the script does not stop when certain checks are unsatisfied."
|
|
|
|
echo " Default: undefined"
|
|
|
|
echo " DRY_RUN Prints all build statements instead of running them."
|
|
|
|
echo " Default: undefined"
|
|
|
|
echo ""
|
|
|
|
echo "Examples:"
|
|
|
|
echo " ${0} master"
|
|
|
|
echo " This will fetch the latest 'master' branch, build a Docker Image and tag it"
|
|
|
|
echo " 'netboxcommunity/netbox:latest'."
|
|
|
|
echo " ${0} develop"
|
|
|
|
echo " This will fetch the latest 'develop' branch, build a Docker Image and tag it"
|
|
|
|
echo " 'netboxcommunity/netbox:snapshot'."
|
|
|
|
echo " ${0} v2.6.6"
|
|
|
|
echo " This will fetch the 'v2.6.6' tag, build a Docker Image and tag it"
|
|
|
|
echo " 'netboxcommunity/netbox:v2.6.6' and 'netboxcommunity/netbox:v2.6'."
|
|
|
|
echo " ${0} develop-2.7"
|
|
|
|
echo " This will fetch the 'develop-2.7' branch, build a Docker Image and tag it"
|
|
|
|
echo " 'netboxcommunity/netbox:develop-2.7'."
|
|
|
|
echo " SRC_ORG=cimnine ${0} feature-x"
|
|
|
|
echo " This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git,"
|
|
|
|
echo " build a Docker Image and tag it 'netboxcommunity/netbox:feature-x'."
|
|
|
|
echo " SRC_ORG=cimnine DOCKER_ORG=cimnine ${0} feature-x"
|
|
|
|
echo " This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git,"
|
|
|
|
echo " build a Docker Image and tag it 'cimnine/netbox:feature-x'."
|
2017-06-09 10:14:43 +02:00
|
|
|
|
|
|
|
if [ "${1}x" == "x" ]; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-10-13 16:00:42 +02:00
|
|
|
###
|
|
|
|
# Determining the build command to use
|
|
|
|
###
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ -z "${DRY_RUN}" ]; then
|
2019-10-13 16:00:42 +02:00
|
|
|
DRY=""
|
|
|
|
else
|
|
|
|
echo "⚠️ DRY_RUN MODE ON ⚠️"
|
|
|
|
DRY="echo"
|
|
|
|
fi
|
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
|
|
|
# read the project version from the `VERSION` file and trim it
|
2018-04-04 09:44:58 +02:00
|
|
|
# see https://stackoverflow.com/a/3232433/172132
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2018-04-04 09:44:58 +02:00
|
|
|
NETBOX_DOCKER_PROJECT_VERSION="${NETBOX_DOCKER_PROJECT_VERSION-$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' VERSION)}"
|
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2018-02-02 12:48:38 +01:00
|
|
|
# variables for fetching the source
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-07-02 21:32:58 +02:00
|
|
|
SRC_ORG="${SRC_ORG-netbox-community}"
|
2018-02-02 12:48:38 +01:00
|
|
|
SRC_REPO="${SRC_REPO-netbox}"
|
2017-06-09 10:14:43 +02:00
|
|
|
BRANCH="${1}"
|
2019-10-14 21:54:49 +02:00
|
|
|
URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}.git}"
|
2019-11-20 22:25:31 +01:00
|
|
|
NETBOX_PATH="${NETBOX_PATH-.netbox}"
|
2018-02-02 12:48:38 +01:00
|
|
|
|
2019-10-13 16:00:42 +02:00
|
|
|
###
|
|
|
|
# fetching the source
|
|
|
|
###
|
2019-11-20 22:25:31 +01:00
|
|
|
if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ] ; then
|
2019-10-14 21:54:49 +02:00
|
|
|
echo "🌐 Checking out '${BRANCH}' of netbox from the url '${URL}' into '${NETBOX_PATH}'"
|
|
|
|
if [ ! -d "${NETBOX_PATH}" ]; then
|
|
|
|
$DRY git clone -q --depth 10 -b "${BRANCH}" "${URL}" "${NETBOX_PATH}"
|
|
|
|
fi
|
2019-10-13 16:00:42 +02:00
|
|
|
|
|
|
|
(
|
2019-10-14 21:54:49 +02:00
|
|
|
$DRY cd "${NETBOX_PATH}"
|
|
|
|
|
|
|
|
if [ -n "${HTTP_PROXY}" ]; then
|
|
|
|
git config http.proxy "${HTTP_PROXY}"
|
|
|
|
fi
|
2019-10-13 16:00:42 +02:00
|
|
|
|
2019-10-14 21:54:49 +02:00
|
|
|
$DRY git remote set-url origin "${URL}"
|
2019-10-14 22:20:09 +02:00
|
|
|
$DRY git fetch -qp --depth 10 origin "${BRANCH}"
|
2019-10-14 21:54:49 +02:00
|
|
|
$DRY git checkout -qf FETCH_HEAD
|
|
|
|
$DRY git prune
|
2019-10-13 16:00:42 +02:00
|
|
|
)
|
2019-10-14 21:54:49 +02:00
|
|
|
echo "✅ Checked out netbox"
|
2019-10-13 16:00:42 +02:00
|
|
|
fi
|
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
|
|
|
# Determining the value for DOCKERFILE
|
|
|
|
# and checking whether it exists
|
|
|
|
###
|
|
|
|
DOCKERFILE="${DOCKERFILE-Dockerfile}"
|
2019-03-27 12:00:32 +01:00
|
|
|
if [ ! -f "${DOCKERFILE}" ]; then
|
2019-10-10 12:42:10 +02:00
|
|
|
echo "🚨 The Dockerfile ${DOCKERFILE} doesn't exist."
|
2019-03-27 12:00:32 +01:00
|
|
|
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ -z "${DEBUG}" ]; then
|
2019-03-27 12:00:32 +01:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "⚠️ Would exit here with code '1', but DEBUG is enabled."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2018-02-02 12:48:38 +01:00
|
|
|
# variables for tagging the docker image
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-02-06 11:46:20 +01:00
|
|
|
DOCKER_ORG="${DOCKER_ORG-netboxcommunity}"
|
2018-02-02 12:48:38 +01:00
|
|
|
DOCKER_REPO="${DOCKER_REPO-netbox}"
|
|
|
|
case "${BRANCH}" in
|
|
|
|
master)
|
|
|
|
TAG="${TAG-latest}";;
|
|
|
|
develop)
|
|
|
|
TAG="${TAG-snapshot}";;
|
|
|
|
*)
|
|
|
|
TAG="${TAG-$BRANCH}";;
|
|
|
|
esac
|
|
|
|
|
2019-10-10 14:48:45 +02:00
|
|
|
###
|
|
|
|
# Determine targets to build
|
|
|
|
###
|
|
|
|
DEFAULT_DOCKER_TARGETS=("main" "ldap")
|
|
|
|
DOCKER_TARGETS=( "${DOCKER_TARGET:-"${DEFAULT_DOCKER_TARGETS[@]}"}")
|
|
|
|
echo "🏭 Building the following targets:" "${DOCKER_TARGETS[@]}"
|
|
|
|
|
|
|
|
###
|
|
|
|
# Build each target
|
|
|
|
###
|
2019-10-10 12:42:10 +02:00
|
|
|
for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
|
2019-10-14 21:54:49 +02:00
|
|
|
echo "🏗 Building the target '${DOCKER_TARGET}'"
|
2019-10-10 14:48:45 +02:00
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-10-10 15:24:53 +02:00
|
|
|
# composing the final TARGET_DOCKER_TAG
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-10-10 15:24:53 +02:00
|
|
|
TARGET_DOCKER_TAG="${DOCKER_TAG-${DOCKER_ORG}/${DOCKER_REPO}:${TAG}}"
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ "${DOCKER_TARGET}" != "main" ]; then
|
2019-10-10 15:24:53 +02:00
|
|
|
TARGET_DOCKER_TAG="${TARGET_DOCKER_TAG}-${DOCKER_TARGET}"
|
2018-02-22 14:49:38 +01:00
|
|
|
fi
|
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
|
|
|
# composing the additional DOCKER_SHORT_TAG,
|
|
|
|
# i.e. "v2.6.1" becomes "v2.6",
|
|
|
|
# which is only relevant for version tags
|
|
|
|
###
|
|
|
|
if [[ "${TAG}" =~ ^v([0-9]+)\.([0-9]+)\.[0-9]+$ ]]; then
|
|
|
|
MAJOR=${BASH_REMATCH[1]}
|
|
|
|
MINOR=${BASH_REMATCH[2]}
|
2018-03-05 16:29:02 +01:00
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
DOCKER_SHORT_TAG="${DOCKER_SHORT_TAG-${DOCKER_ORG}/${DOCKER_REPO}:v${MAJOR}.${MINOR}}"
|
2018-03-05 16:29:02 +01:00
|
|
|
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ "${DOCKER_TARGET}" != "main" ]; then
|
2019-10-10 12:42:10 +02:00
|
|
|
DOCKER_SHORT_TAG="${DOCKER_SHORT_TAG}-${DOCKER_TARGET}"
|
|
|
|
fi
|
|
|
|
fi
|
2017-06-09 10:14:43 +02:00
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-10-14 21:54:49 +02:00
|
|
|
# Proceeding to buils stage, except if `--push-only` is passed
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ "${2}" != "--push-only" ] ; then
|
|
|
|
###
|
|
|
|
# Composing all arguments for `docker build`
|
|
|
|
###
|
|
|
|
DOCKER_BUILD_ARGS=(
|
|
|
|
--pull
|
|
|
|
--target "${DOCKER_TARGET}"
|
|
|
|
-f "${DOCKERFILE}"
|
|
|
|
-t "${TARGET_DOCKER_TAG}"
|
|
|
|
)
|
|
|
|
if [ -n "${DOCKER_SHORT_TAG}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=( -t "${DOCKER_SHORT_TAG}" )
|
|
|
|
fi
|
2019-10-10 12:42:10 +02:00
|
|
|
|
2019-10-14 21:54:49 +02:00
|
|
|
# --label
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
2019-10-15 10:03:39 +02:00
|
|
|
--label "NETBOX_DOCKER_PROJECT_VERSION=${NETBOX_DOCKER_PROJECT_VERSION}"
|
|
|
|
--label "NETBOX_BRANCH=${BRANCH}"
|
|
|
|
--label "ORIGINAL_DOCKER_TAG=${TARGET_DOCKER_TAG}"
|
2019-10-14 21:54:49 +02:00
|
|
|
)
|
|
|
|
if [ -d "${NETBOX_PATH}/.git" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
2019-10-15 10:03:39 +02:00
|
|
|
--label "NETBOX_GIT_COMMIT=$($DRY cd "${NETBOX_PATH}"; $DRY git rev-parse HEAD)"
|
|
|
|
--label "NETBOX_GIT_URL=$($DRY cd "${NETBOX_PATH}"; $DRY git remote get-url origin)"
|
2019-10-14 21:54:49 +02:00
|
|
|
)
|
|
|
|
fi
|
2019-03-27 12:00:32 +01:00
|
|
|
|
2019-10-14 21:54:49 +02:00
|
|
|
# --build-arg
|
2019-10-15 10:03:39 +02:00
|
|
|
DOCKER_BUILD_ARGS+=( --build-arg "NETBOX_PATH=${NETBOX_PATH}" )
|
2019-10-14 21:54:49 +02:00
|
|
|
if [ -n "${DOCKER_FROM}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=( --build-arg "FROM=${DOCKER_FROM}" )
|
|
|
|
fi
|
|
|
|
if [ -n "${HTTP_PROXY}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=( --build-arg "http_proxy=${HTTP_PROXY}" )
|
|
|
|
DOCKER_BUILD_ARGS+=( --build-arg "https_proxy=${HTTPS_PROXY}" )
|
|
|
|
fi
|
|
|
|
if [ -n "${NO_PROXY}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=( --build-arg "no_proxy=${NO_PROXY}" )
|
|
|
|
fi
|
|
|
|
|
|
|
|
###
|
|
|
|
# Building the docker image
|
|
|
|
###
|
2019-10-13 16:00:42 +02:00
|
|
|
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}'."
|
2019-10-14 21:54:49 +02:00
|
|
|
$DRY docker build "${DOCKER_BUILD_ARGS[@]}" .
|
2019-10-10 15:24:53 +02:00
|
|
|
echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'"
|
2019-10-10 12:42:10 +02:00
|
|
|
fi
|
2019-03-27 12:00:32 +01:00
|
|
|
|
2019-10-10 12:42:10 +02:00
|
|
|
###
|
|
|
|
# Pushing the docker images if either `--push` or `--push-only` are passed
|
|
|
|
###
|
|
|
|
if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then
|
2019-10-10 15:24:53 +02:00
|
|
|
echo "⏫ Pushing '${TARGET_DOCKER_TAG}"
|
2019-10-13 16:00:42 +02:00
|
|
|
$DRY docker push "${TARGET_DOCKER_TAG}"
|
2019-10-10 15:24:53 +02:00
|
|
|
echo "✅ Finished pushing the Docker image '${TARGET_DOCKER_TAG}'."
|
2019-10-10 12:42:10 +02:00
|
|
|
|
|
|
|
if [ -n "$DOCKER_SHORT_TAG" ]; then
|
|
|
|
echo "⏫ Pushing '${DOCKER_SHORT_TAG}'"
|
2019-10-13 16:00:42 +02:00
|
|
|
$DRY docker push "${DOCKER_SHORT_TAG}"
|
2019-10-10 12:42:10 +02:00
|
|
|
echo "✅ Finished pushing the Docker image '${DOCKER_SHORT_TAG}'."
|
|
|
|
fi
|
2019-03-20 14:03:18 +01:00
|
|
|
fi
|
2019-10-10 12:42:10 +02:00
|
|
|
done
|