2017-06-09 10:14:43 +02:00
|
|
|
#!/bin/bash
|
2018-03-05 14:29:24 +01:00
|
|
|
# Builds the Dockerfile[.variant] and injects tgz'ed Netbox code from Github
|
2017-06-09 10:14:43 +02:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
|
|
|
|
echo "Usage: ${0} <branch> [--push]"
|
2017-09-11 15:30:52 +02:00
|
|
|
echo " branch The branch or tag to build. Required."
|
|
|
|
echo " --push Pushes built Docker image to docker hub."
|
2017-06-09 10:14:43 +02:00
|
|
|
echo ""
|
|
|
|
echo "You can use the following ENV variables to customize the build:"
|
2018-03-05 14:30:30 +01:00
|
|
|
echo " DEBUG If defined, the script does not stop when certain checks are unsatisfied."
|
|
|
|
echo " DRY_RUN Prints all build statements instead of running them."
|
2018-02-02 12:48:38 +01:00
|
|
|
echo " DOCKER_OPTS Add parameters to Docker."
|
|
|
|
echo " Default:"
|
|
|
|
echo " When <TAG> starts with 'v': \"\""
|
|
|
|
echo " Else: \"--no-cache\""
|
2017-06-09 10:14:43 +02:00
|
|
|
echo " BRANCH The branch to build."
|
|
|
|
echo " Also used for tagging the image."
|
2018-02-02 12:48:38 +01:00
|
|
|
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>) "
|
2017-09-11 15:30:52 +02:00
|
|
|
echo " Also used for tagging the image."
|
|
|
|
echo " Default: ninech"
|
2018-02-02 12:48:38 +01:00
|
|
|
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"
|
|
|
|
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>:<BRANCH>"
|
|
|
|
echo " SRC_ORG Which fork of netbox to use (i.e. github.com/<SRC_ORG>/<SRC_REPO>)."
|
2017-09-11 15:30:52 +02:00
|
|
|
echo " Default: digitalocean"
|
2018-02-02 12:48:38 +01:00
|
|
|
echo " SRC_REPO The name of the netbox for to use (i.e. github.com/<SRC_ORG>/<SRC_REPO>)."
|
|
|
|
echo " Default: netbox"
|
2017-06-09 10:14:43 +02:00
|
|
|
echo " URL Where to fetch the package from."
|
|
|
|
echo " Must be a tar.gz file of the source code."
|
2018-02-02 12:48:38 +01:00
|
|
|
echo " Default: https://github.com/<SRC_ORG>/<SRC_REPO>/archive/\$BRANCH.tar.gz"
|
2018-02-22 14:49:38 +01:00
|
|
|
echo " VARIANT The variant to build."
|
2018-03-05 14:35:11 +01:00
|
|
|
echo " The value will be used as a suffix to the \$TAG and for the Dockerfile"
|
|
|
|
echo " selection. The TAG being build must exist for the base variant and"
|
|
|
|
echo " corresponding Dockerfile must start with the following lines:"
|
|
|
|
echo " ARG DOCKER_ORG=ninech"
|
|
|
|
echo " ARG DOCKER_REPOT=netbox"
|
2018-02-22 14:49:38 +01:00
|
|
|
echo " ARG FROM_TAG=latest"
|
2018-03-05 14:35:11 +01:00
|
|
|
echo " FROM \$DOCKER_ORG/\$DOCKER_REPO:\$FROM_TAG"
|
|
|
|
echo " Example: VARIANT=ldap will result in the tag 'latest-ldap' and the"
|
|
|
|
echo " Dockerfile 'Dockerfile.ldap' being used."
|
2018-02-22 14:49:38 +01:00
|
|
|
echo " Default: empty"
|
2017-06-09 10:14:43 +02:00
|
|
|
|
|
|
|
if [ "${1}x" == "x" ]; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-02-02 12:48:38 +01:00
|
|
|
# variables for fetching the source
|
|
|
|
SRC_ORG="${SRC_ORG-digitalocean}"
|
|
|
|
SRC_REPO="${SRC_REPO-netbox}"
|
2017-06-09 10:14:43 +02:00
|
|
|
BRANCH="${1}"
|
2018-02-02 12:48:38 +01:00
|
|
|
URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}"
|
|
|
|
|
|
|
|
# variables for tagging the docker image
|
|
|
|
DOCKER_ORG="${DOCKER_ORG-ninech}"
|
|
|
|
DOCKER_REPO="${DOCKER_REPO-netbox}"
|
|
|
|
case "${BRANCH}" in
|
|
|
|
master)
|
|
|
|
TAG="${TAG-latest}";;
|
|
|
|
develop)
|
|
|
|
TAG="${TAG-snapshot}";;
|
|
|
|
*)
|
|
|
|
TAG="${TAG-$BRANCH}";;
|
|
|
|
esac
|
|
|
|
DOCKER_TAG="${DOCKER_TAG-${DOCKER_ORG}/${DOCKER_REPO}:${TAG}}"
|
|
|
|
|
2018-03-05 14:29:24 +01:00
|
|
|
# caching is only ok for version tags
|
|
|
|
case "${TAG}" in
|
|
|
|
v*)
|
|
|
|
CACHE="${CACHE-}";;
|
|
|
|
*)
|
|
|
|
CACHE="${CACHE---no-cache}";;
|
|
|
|
esac
|
|
|
|
|
2018-02-22 14:49:38 +01:00
|
|
|
# Checking which VARIANT to build
|
|
|
|
if [ -z "$VARIANT" ]; then
|
|
|
|
DOCKERFILE="Dockerfile"
|
|
|
|
else
|
|
|
|
DOCKERFILE="Dockerfile.${VARIANT}"
|
|
|
|
DOCKER_TAG="${DOCKER_TAG}-${VARIANT}"
|
2018-03-05 14:30:30 +01:00
|
|
|
|
|
|
|
# Fail fast
|
|
|
|
if [ ! -f "${DOCKERFILE}" ]; then
|
|
|
|
echo "🚨 The Dockerfile ${DOCKERFILE} for variant '${VARIANT}' doesn't exist."
|
|
|
|
|
|
|
|
if [ -z "$DEBUG" ]; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "⚠️ Would exit here with code '1', but DEBUG is enabled."
|
|
|
|
fi
|
2018-02-22 14:49:38 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-02-02 12:48:38 +01:00
|
|
|
# Docker options
|
2018-03-05 14:29:24 +01:00
|
|
|
DOCKER_OPTS=(
|
|
|
|
"$CACHE"
|
|
|
|
--pull
|
|
|
|
)
|
|
|
|
|
|
|
|
# Build args
|
|
|
|
DOCKER_BUILD_ARGS=(
|
|
|
|
--build-arg "FROM_TAG=${TAG}"
|
|
|
|
--build-arg "BRANCH=${BRANCH}"
|
|
|
|
--build-arg "URL=${URL}"
|
2018-03-05 14:35:11 +01:00
|
|
|
--build-arg "DOCKER_ORG=${DOCKER_ORG}"
|
|
|
|
--build-arg "DOCKER_REPO=${DOCKER_REPO}"
|
2018-03-05 14:29:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if [ -z "$DRY_RUN" ]; then
|
|
|
|
DOCKER_CMD="docker"
|
|
|
|
else
|
|
|
|
echo "⚠️ DRY_RUN MODE ON ⚠️"
|
|
|
|
DOCKER_CMD="echo docker"
|
|
|
|
fi
|
2017-06-09 10:14:43 +02:00
|
|
|
|
2018-02-02 12:48:38 +01:00
|
|
|
echo "🐳 Building the Docker image '${DOCKER_TAG}' from the url '${URL}'."
|
2018-03-05 14:29:24 +01:00
|
|
|
$DOCKER_CMD build -t "${DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" .
|
2018-02-02 12:48:38 +01:00
|
|
|
echo "✅ Finished building the Docker images '${DOCKER_TAG}'"
|
2017-06-09 10:14:43 +02:00
|
|
|
|
|
|
|
if [ "${2}" == "--push" ] ; then
|
2018-02-02 12:48:38 +01:00
|
|
|
echo "⏫ Pushing '${DOCKER_TAG}"
|
2018-03-05 14:29:24 +01:00
|
|
|
$DOCKER_CMD push "${DOCKER_TAG}"
|
2018-02-02 12:48:38 +01:00
|
|
|
echo "✅ Finished pushing the Docker image '${DOCKER_TAG}'."
|
2017-06-09 10:14:43 +02:00
|
|
|
fi
|