Ability to keep buildx builder

This commit is contained in:
Christian Mäder 2021-03-14 16:04:02 +01:00 committed by cimnine
parent 0c9195ebc7
commit b048c5f559
1 changed files with 110 additions and 97 deletions

207
build.sh
View File

@ -6,94 +6,108 @@ echo "▶️ $0 $*"
set -e set -e
if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
echo "Usage: ${0} <branch> [--push|--push-only]" cat <<END_OF_DOCS
echo " branch The branch or tag to build. Required." Usage: ${0} <branch> [--push|--push-only]
echo " --push Pushes the built Docker image to the registry." branch The branch or tag to build. Required.
echo " --push-only Only pushes the Docker image to the registry, but does not build it." --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.
echo "You can use the following ENV variables to customize the build:"
echo " SRC_ORG Which fork of netbox to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO})." You can use the following ENV variables to customize the build:
echo " Default: netbox-community" SRC_ORG Which fork of netbox to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO}).
echo " SRC_REPO The name of the repository to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO})." Default: netbox-community
echo " Default: netbox" SRC_REPO The name of the repository to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO}).
echo " URL Where to fetch the code from." Default: netbox
echo " Must be a git repository. Can be private." URL Where to fetch the code from.
echo " Default: https://github.com/\${SRC_ORG}/\${SRC_REPO}.git" Must be a git repository. Can be private.
echo " NETBOX_PATH The path where netbox will be checkout out." Default: https://github.com/\${SRC_ORG}/\${SRC_REPO}.git
echo " Must not be outside of the netbox-docker repository (because of Docker)!" NETBOX_PATH The path where netbox will be checkout out.
echo " Default: .netbox" Must not be outside of the netbox-docker repository (because of Docker)!
echo " SKIP_GIT If defined, git is not invoked and \${NETBOX_PATH} will not be altered." Default: .netbox
echo " This may be useful, if you are manually managing the NETBOX_PATH." SKIP_GIT If defined, git is not invoked and \${NETBOX_PATH} will not be altered.
echo " Default: undefined" This may be useful, if you are manually managing the NETBOX_PATH.
echo " TAG The version part of the docker tag." Default: undefined
echo " Default:" TAG The version part of the docker tag.
echo " When <branch>=master: latest" Default:
echo " When <branch>=develop: snapshot" When <branch>=master: latest
echo " Else: same as <branch>" When <branch>=develop: snapshot
echo " DOCKER_REGISTRY The Docker repository's registry (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')" Else: same as <branch>
echo " Used for tagging the image." DOCKER_REGISTRY The Docker repository's registry (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')
echo " Default: docker.io" Used for tagging the image.
echo " DOCKER_ORG The Docker repository's organisation (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')" Default: docker.io
echo " Used for tagging the image." DOCKER_ORG The Docker repository's organisation (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')
echo " Default: netboxcommunity" Used for tagging the image.
echo " DOCKER_REPO The Docker repository's name (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')" Default: netboxcommunity
echo " Used for tagging the image." DOCKER_REPO The Docker repository's name (i.e. '\${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}'')
echo " Default: netbox" Used for tagging the image.
echo " DOCKER_TAG The name of the tag which is applied to the image." Default: netbox
echo " Useful for pushing into another registry than hub.docker.com." DOCKER_TAG The name of the tag which is applied to the image.
echo " Default: \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:\${TAG}" Useful for pushing into another registry than hub.docker.com.
echo " DOCKER_SHORT_TAG The name of the short tag which is applied to the" Default: \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:\${TAG}
echo " image. This is used to tag all patch releases to their" DOCKER_SHORT_TAG The name of the short tag which is applied to the
echo " containing version e.g. v2.5.1 -> v2.5" image. This is used to tag all patch releases to their
echo " Default: \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:<MAJOR>.<MINOR>" containing version e.g. v2.5.1 -> v2.5
echo " DOCKERFILE The name of Dockerfile to use." Default: \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:<MAJOR>.<MINOR>
echo " Default: Dockerfile" DOCKERFILE The name of Dockerfile to use.
echo " DOCKER_FROM The base image to use." Default: Dockerfile
echo " Default: 'alpine:3.14'" DOCKER_FROM The base image to use.
echo " DOCKER_TARGET A specific target to build." Default: 'alpine:3.14'
echo " It's currently not possible to pass multiple targets." DOCKER_TARGET A specific target to build.
echo " Default: main ldap" It's currently not possible to pass multiple targets.
echo " BUILDX_PLATFORMS If defined, the image will be build for the given platform(s)." Default: main ldap
echo " Example: linux/amd64,linux/arm64" BUILDX_PLATFORMS Specifies the platform(s) to build the image for.
echo " Default: linux/amd64" Example: linux/amd64,linux/arm64
echo " HTTP_PROXY The proxy to use for http requests." Default: linux/amd64
echo " Example: http://proxy.domain.tld:3128" BUILDX_BUILDER_NAME If defined, the image build will be assigned to the given builder.
echo " Default: undefined" If you specify this variable, make sure that the builder exists.
echo " NO_PROXY Comma-separated list of domain extensions proxy should not be used for." If this value is not defined, a new builx builder with the directory name of the
echo " Example: .domain1.tld,.domain2.tld" current directory (i.e. '$(basename "${PWD}")') is created.
echo " Default: undefined" Example: clever_lovelace
echo " DEBUG If defined, the script does not stop when certain checks are unsatisfied." Default: undefined
echo " Default: undefined" BUILDX_KEEP_BUILDER If defined and if BUILDX_BUILDER_NAME is undefined, then the
echo " DRY_RUN Prints all build statements instead of running them." buildx builder created by this script is not removed.
echo " Default: undefined" This is useful if you want to re-use the builder in a later build on the
echo " GH_ACTION If defined, special 'echo' statements are enabled that set the" same system.
echo " following environment variables in Github Actions:" By default, all buildx builders created by this script are removed at the end.
echo " - FINAL_DOCKER_TAG: The final value of the DOCKER_TAG env variable" Default: undefined
echo " Default: undefined" HTTP_PROXY The proxy to use for http requests.
echo "" Example: http://proxy.domain.tld:3128
echo "Examples:" Default: undefined
echo " ${0} master" NO_PROXY Comma-separated list of domain extensions proxy should not be used for.
echo " This will fetch the latest 'master' branch, build a Docker Image and tag it" Example: .domain1.tld,.domain2.tld
echo " 'netboxcommunity/netbox:latest'." Default: undefined
echo " ${0} develop" DEBUG If defined, the script does not stop when certain checks are unsatisfied.
echo " This will fetch the latest 'develop' branch, build a Docker Image and tag it" Default: undefined
echo " 'netboxcommunity/netbox:snapshot'." DRY_RUN Prints all build statements instead of running them.
echo " ${0} v2.6.6" Default: undefined
echo " This will fetch the 'v2.6.6' tag, build a Docker Image and tag it" GH_ACTION If defined, special 'echo' statements are enabled that set the
echo " 'netboxcommunity/netbox:v2.6.6' and 'netboxcommunity/netbox:v2.6'." following environment variables in Github Actions:
echo " ${0} develop-2.7" - FINAL_DOCKER_TAG: The final value of the DOCKER_TAG env variable
echo " This will fetch the 'develop-2.7' branch, build a Docker Image and tag it" Default: undefined
echo " 'netboxcommunity/netbox:develop-2.7'."
echo " SRC_ORG=cimnine ${0} feature-x" Examples:
echo " This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git," ${0} master
echo " build a Docker Image and tag it 'netboxcommunity/netbox:feature-x'." This will fetch the latest 'master' branch, build a Docker Image and tag it
echo " SRC_ORG=cimnine DOCKER_ORG=cimnine ${0} feature-x" 'netboxcommunity/netbox:latest'.
echo " This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git," ${0} develop
echo " build a Docker Image and tag it 'cimnine/netbox:feature-x'." This will fetch the latest 'develop' branch, build a Docker Image and tag it
echo " PLATFORMS=linux/amd64,linux/arm64 ${0} master" 'netboxcommunity/netbox:snapshot'.
echo " This will fetch the latest 'master' branch, build a Docker Image and tag it" ${0} v2.6.6
echo " 'netboxcommunity/netbox:latest'." This will fetch the 'v2.6.6' tag, build a Docker Image and tag it
echo " It will produce an ARM64 and an AMD64 version of the image." 'netboxcommunity/netbox:v2.6.6' and 'netboxcommunity/netbox:v2.6'.
${0} develop-2.7
This will fetch the 'develop-2.7' branch, build a Docker Image and tag it
'netboxcommunity/netbox:develop-2.7'.
SRC_ORG=cimnine ${0} feature-x
This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git,
build a Docker Image and tag it 'netboxcommunity/netbox:feature-x'.
SRC_ORG=cimnine DOCKER_ORG=cimnine ${0} feature-x
This will fetch the 'feature-x' branch from https://github.com/cimnine/netbox.git,
build a Docker Image and tag it 'cimnine/netbox:feature-x'.
PLATFORMS=linux/amd64,linux/arm64 ${0} master
This will fetch the latest 'master' branch, build a Docker Image and tag it
'netboxcommunity/netbox:latest'.
It will produce an ARM64 and an AMD64 version of the image.
END_OF_DOCS
if [ "${1}x" == "x" ]; then if [ "${1}x" == "x" ]; then
exit 1 exit 1
@ -392,8 +406,8 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
DOCKER_BUILD_ARGS+=(--platform "${BUILDX_PLATFORMS-linux/amd64}") DOCKER_BUILD_ARGS+=(--platform "${BUILDX_PLATFORMS-linux/amd64}")
# --cache-from / --cache-to # --cache-from / --cache-to
DOCKER_BUILD_ARGS+=("--cache-from=type=registry,ref=${TARGET_DOCKER_TAG}-cache,mode=max") # DOCKER_BUILD_ARGS+=("--cache-from=type=registry,ref=${TARGET_DOCKER_TAG}-cache,mode=max")
DOCKER_BUILD_ARGS+=("--cache-to=type=registry,ref=${TARGET_DOCKER_TAG}-cache,mode=max") # DOCKER_BUILD_ARGS+=("--cache-to=type=registry,ref=${TARGET_DOCKER_TAG}-cache,mode=max")
### ###
# Pushing the docker images if `--push` is passed # Pushing the docker images if `--push` is passed
@ -403,14 +417,13 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
fi fi
if [ -z "${BUILDX_BUILDER_NAME}" ]; then if [ -z "${BUILDX_BUILDER_NAME}" ]; then
echo "👷 Creating new Buildx Builder" BUILDX_BUILDER_NAME="$(basename "${PWD}")"
if [ -z "${DRY_RUN}" ]; then if ! docker buildx ls | grep --quiet --word-regexp "${BUILDX_BUILDER_NAME}"; then
BUILDX_BUILDER_NAME=$(docker buildx create) echo "👷 Creating new Buildx Builder '${BUILDX_BUILDER_NAME}'"
else $DRY docker buildx create --name "${BUILDX_BUILDER_NAME}"
BUILDX_BUILDER_NAME="DRY_RUN_NEW_BUIDLER"
fi
BUILDX_BUILDER_CREATED="yes" BUILDX_BUILDER_CREATED="yes"
fi fi
fi
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' on '${BUILDX_BUILDER_NAME}'." echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' on '${BUILDX_BUILDER_NAME}'."
echo " Build reason set to: ${BUILD_REASON}" echo " Build reason set to: ${BUILD_REASON}"
@ -425,7 +438,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
echo "🔎 Inspecting labels on '${TARGET_DOCKER_TAG}'" echo "🔎 Inspecting labels on '${TARGET_DOCKER_TAG}'"
$DRY docker inspect "${TARGET_DOCKER_TAG}" --format "{{json .Config.Labels}}" $DRY docker inspect "${TARGET_DOCKER_TAG}" --format "{{json .Config.Labels}}"
if [ "${BUILDX_BUILDER_CREATED}" == "yes" ]; then if [ -z "${BUILDX_KEEP_BUILDER}" ] && [ "${BUILDX_BUILDER_CREATED}" == "yes" ]; then
echo "👷 Removing Buildx Builder '${BUILDX_BUILDER_NAME}'" echo "👷 Removing Buildx Builder '${BUILDX_BUILDER_NAME}'"
$DRY docker buildx rm "${BUILDX_BUILDER_NAME}" $DRY docker buildx rm "${BUILDX_BUILDER_NAME}"
fi fi