From fe05ca5c7ce999eca9f8baa5e4a3431826a2dbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Fri, 2 Feb 2018 12:48:38 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Adds=20new=20options=20to?= =?UTF-8?q?=20the=20build=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ build.sh | 75 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index df564cd..02cdc08 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,3 +40,5 @@ ENTRYPOINT [ "/docker-entrypoint.sh" ] VOLUME ["/etc/netbox-nginx/"] CMD ["gunicorn", "--log-level debug", "-c /opt/netbox/gunicorn_config.py", "netbox.wsgi"] + +LABEL SRC_URL="$URL" diff --git a/build.sh b/build.sh index 9141d0a..e8443bc 100755 --- a/build.sh +++ b/build.sh @@ -8,16 +8,33 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then 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 starts with 'v': \"\"" + echo " Else: \"--no-cache\"" echo " BRANCH The branch to build." echo " Also used for tagging the image." - echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r/DOCKER_REPO/netbox) " + echo " TAG The version part of the docker tag." + echo " Default:" + echo " When =master: latest" + echo " When =develop: snapshot" + echo " Else: same as " + echo " DOCKER_ORG The Docker registry (i.e. hub.docker.com/r//) " echo " Also used for tagging the image." echo " Default: ninech" - echo " SRC_REPO Which fork of netbox to use (i.e. github.com//netbox)." + echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r//) " + 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: /:" + echo " SRC_ORG Which fork of netbox to use (i.e. github.com//)." echo " Default: digitalocean" + echo " SRC_REPO The name of the netbox for to use (i.e. github.com//)." + 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_REPO}/netbox/archive/\$BRANCH.tar.gz" + echo " Default: https://github.com///archive/\$BRANCH.tar.gz" if [ "${1}x" == "x" ]; then exit 1 @@ -26,28 +43,42 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then fi fi -SRC_REPO="${SRC_REPO-digitalocean}" -DOCKER_REPO="${DOCKER_REPO-ninech}" +# variables for fetching the source +SRC_ORG="${SRC_ORG-digitalocean}" +SRC_REPO="${SRC_REPO-netbox}" BRANCH="${1}" -URL="${URL-https://github.com/${SRC_REPO}/netbox/archive/$BRANCH.tar.gz}" +URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}" -if [ "${BRANCH}" == "master" ]; then - TAG="${TAG-latest}" - CACHE="--no-cache" -elif [ "${BRANCH}" == "develop" ]; then - TAG="${TAG-snapshot}" - CACHE="--no-cache" -else - TAG="${TAG-$BRANCH}" - CACHE="" -fi +# 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}}" -echo "🐳 Building the Docker image '${DOCKER_REPO}/netbox:${TAG}' from the branch '${BRANCH}'." -docker build -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . -echo "✅ Finished building the Docker images '${DOCKER_REPO}/netbox:${TAG}'" +# caching is only ok for version tags +case "${TAG}" in + v*) + CACHE="${CACHE-}";; + *) + CACHE="${CACHE---no-cache}";; +esac + +# Docker options +DOCKER_OPTS="${DOCKER_OPTS-$CACHE}" + +echo "🐳 Building the Docker image '${DOCKER_TAG}' from the url '${URL}'." +docker build -t "${DOCKER_TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${DOCKER_OPTS} . +echo "✅ Finished building the Docker images '${DOCKER_TAG}'" if [ "${2}" == "--push" ] ; then - echo "⏫ Pushing '${DOCKER_REPO}/netbox:${BRANCH}" - docker push "${DOCKER_REPO}/netbox:${TAG}" - echo "✅ Finished pushing the Docker image '${DOCKER_REPO}/netbox:${TAG}'." + echo "⏫ Pushing '${DOCKER_TAG}" + docker push "${DOCKER_TAG}" + echo "✅ Finished pushing the Docker image '${DOCKER_TAG}'." fi