netbox-docker/build.sh

124 lines
4.2 KiB
Bash
Raw Normal View History

#!/bin/bash
2018-03-05 07:29:24 -06:00
# Builds the Dockerfile[.variant] and injects tgz'ed Netbox code from Github
set -e
if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
echo "Usage: ${0} <branch> [--push]"
echo " branch The branch or tag to build. Required."
echo " --push Pushes built Docker image to docker hub."
echo ""
echo "You can use the following ENV variables to customize the build:"
echo " DOCKER_OPTS Add parameters to Docker."
echo " Default:"
echo " When <TAG> starts with 'v': \"\""
echo " Else: \"--no-cache\""
echo " BRANCH The branch to build."
echo " Also used for tagging the image."
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: ninech"
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>)."
echo " Default: digitalocean"
echo " SRC_REPO The name of the netbox for to use (i.e. github.com/<SRC_ORG>/<SRC_REPO>)."
echo " Default: netbox"
echo " URL Where to fetch the package from."
echo " Must be a tar.gz file of the source code."
echo " Default: https://github.com/<SRC_ORG>/<SRC_REPO>/archive/\$BRANCH.tar.gz"
echo " VARIANT The variant to build."
echo " When set the value will be used as a TAG suffix and for Dockerfile selection."
echo " The TAG being build must exist for the base variant and the variant Dockerfile"
echo " must start with the following two lines:"
echo " ARG FROM_TAG=latest"
echo " FROM ninech/netbox:$FROM_TAG"
echo " Example: VARIANT=ldap will result in the tag 'latest-ldap' and the Dockerfile"
echo " 'Dockerfile.ldap' being used."
echo " Default: empty"
if [ "${1}x" == "x" ]; then
exit 1
else
exit 0
fi
fi
# variables for fetching the source
SRC_ORG="${SRC_ORG-digitalocean}"
SRC_REPO="${SRC_REPO-netbox}"
BRANCH="${1}"
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 07:29:24 -06:00
# caching is only ok for version tags
case "${TAG}" in
v*)
CACHE="${CACHE-}";;
*)
CACHE="${CACHE---no-cache}";;
esac
# Checking which VARIANT to build
if [ -z "$VARIANT" ]; then
DOCKERFILE="Dockerfile"
else
DOCKERFILE="Dockerfile.${VARIANT}"
DOCKER_TAG="${DOCKER_TAG}-${VARIANT}"
if [ ! -f ${DOCKERFILE} ]; then
echo "The Dockerfile ${DOCKERFILE} for variant '${VARIANT}' doesn't exist. Exiting"
exit 1
fi
fi
# Docker options
2018-03-05 07:29:24 -06:00
DOCKER_OPTS=(
"$CACHE"
--pull
)
# Build args
DOCKER_BUILD_ARGS=(
--build-arg "FROM_TAG=${TAG}"
--build-arg "BRANCH=${BRANCH}"
--build-arg "URL=${URL}"
)
if [ -z "$DRY_RUN" ]; then
DOCKER_CMD="docker"
else
echo "⚠️ DRY_RUN MODE ON ⚠️"
DOCKER_CMD="echo docker"
fi
echo "🐳 Building the Docker image '${DOCKER_TAG}' from the url '${URL}'."
2018-03-05 07:29:24 -06:00
$DOCKER_CMD build -t "${DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" .
echo "✅ Finished building the Docker images '${DOCKER_TAG}'"
if [ "${2}" == "--push" ] ; then
echo "⏫ Pushing '${DOCKER_TAG}"
2018-03-05 07:29:24 -06:00
$DOCKER_CMD push "${DOCKER_TAG}"
echo "✅ Finished pushing the Docker image '${DOCKER_TAG}'."
fi