Simplify build script

This commit is contained in:
Christian Mäder 2021-03-14 17:40:50 +01:00 committed by cimnine
parent 7e78ca7f61
commit 1bc6844c66
1 changed files with 106 additions and 107 deletions

213
build.sh
View File

@ -275,7 +275,6 @@ echo "🏭 Building the following targets:" "${DOCKER_TARGETS[@]}"
### ###
# Build each target # Build each target
### ###
export DOCKER_BUILDKIT=${DOCKER_BUILDKIT-1}
for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
echo "🏗 Building the target '${DOCKER_TARGET}'" echo "🏗 Building the target '${DOCKER_TARGET}'"
@ -379,121 +378,121 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
if [ "${SHOULD_BUILD}" != "true" ]; then if [ "${SHOULD_BUILD}" != "true" ]; then
echo "⏯ Build skipped because sources didn't change" echo "⏯ Build skipped because sources didn't change"
echo "::set-output name=skipped::true" echo "::set-output name=skipped::true"
else continue
### fi
# Composing all arguments for `docker build`
###
DOCKER_BUILD_ARGS=(
--pull
--target "${DOCKER_TARGET}"
-f "${DOCKERFILE}"
-t "${TARGET_DOCKER_TAG}"
)
if [ -n "${TARGET_DOCKER_SHORT_TAG}" ]; then
DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_SHORT_TAG}")
DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_LATEST_TAG}")
fi
# --label ###
# Composing all arguments for `docker build`
###
DOCKER_BUILD_ARGS=(
--pull
--target "${DOCKER_TARGET}"
-f "${DOCKERFILE}"
-t "${TARGET_DOCKER_TAG}"
)
if [ -n "${TARGET_DOCKER_SHORT_TAG}" ]; then
DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_SHORT_TAG}")
DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_LATEST_TAG}")
fi
# --label
DOCKER_BUILD_ARGS+=(
--label "ORIGINAL_TAG=${TARGET_DOCKER_TAG}"
--label "org.label-schema.build-date=${BUILD_DATE}"
--label "org.opencontainers.image.created=${BUILD_DATE}"
--label "org.label-schema.version=${PROJECT_VERSION}"
--label "org.opencontainers.image.version=${PROJECT_VERSION}"
)
if [ -d ".git" ]; then
DOCKER_BUILD_ARGS+=( DOCKER_BUILD_ARGS+=(
--label "ORIGINAL_TAG=${TARGET_DOCKER_TAG}" --label "org.label-schema.vcs-ref=${GIT_REF}"
--label "org.opencontainers.image.revision=${GIT_REF}"
--label "org.label-schema.build-date=${BUILD_DATE}"
--label "org.opencontainers.image.created=${BUILD_DATE}"
--label "org.label-schema.version=${PROJECT_VERSION}"
--label "org.opencontainers.image.version=${PROJECT_VERSION}"
) )
if [ -d ".git" ]; then fi
DOCKER_BUILD_ARGS+=( if [ -d "${NETBOX_PATH}/.git" ]; then
--label "org.label-schema.vcs-ref=${GIT_REF}" DOCKER_BUILD_ARGS+=(
--label "org.opencontainers.image.revision=${GIT_REF}" --label "NETBOX_GIT_BRANCH=${NETBOX_GIT_BRANCH}"
) --label "NETBOX_GIT_REF=${NETBOX_GIT_REF}"
fi --label "NETBOX_GIT_URL=${NETBOX_GIT_URL}"
if [ -d "${NETBOX_PATH}/.git" ]; then )
DOCKER_BUILD_ARGS+=( fi
--label "NETBOX_GIT_BRANCH=${NETBOX_GIT_BRANCH}" if [ -n "${BUILD_REASON}" ]; then
--label "NETBOX_GIT_REF=${NETBOX_GIT_REF}" BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON")
--label "NETBOX_GIT_URL=${NETBOX_GIT_URL}" DOCKER_BUILD_ARGS+=(--label "BUILD_REASON=${BUILD_REASON}")
) fi
fi
if [ -n "${BUILD_REASON}" ]; then
BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON")
DOCKER_BUILD_ARGS+=(--label "BUILD_REASON=${BUILD_REASON}")
fi
# --build-arg # --build-arg
DOCKER_BUILD_ARGS+=(--build-arg "NETBOX_PATH=${NETBOX_PATH}") DOCKER_BUILD_ARGS+=(--build-arg "NETBOX_PATH=${NETBOX_PATH}")
if [ -n "${DOCKER_FROM}" ]; then if [ -n "${DOCKER_FROM}" ]; then
DOCKER_BUILD_ARGS+=(--build-arg "FROM=${DOCKER_FROM}") DOCKER_BUILD_ARGS+=(--build-arg "FROM=${DOCKER_FROM}")
fi fi
# shellcheck disable=SC2031 if [ -n "${HTTP_PROXY}" ]; then
if [ -n "${HTTP_PROXY}" ]; then DOCKER_BUILD_ARGS+=(--build-arg "http_proxy=${HTTP_PROXY}")
DOCKER_BUILD_ARGS+=(--build-arg "http_proxy=${HTTP_PROXY}") DOCKER_BUILD_ARGS+=(--build-arg "https_proxy=${HTTPS_PROXY}")
DOCKER_BUILD_ARGS+=(--build-arg "https_proxy=${HTTPS_PROXY}") fi
fi if [ -n "${NO_PROXY}" ]; then
if [ -n "${NO_PROXY}" ]; then DOCKER_BUILD_ARGS+=(--build-arg "no_proxy=${NO_PROXY}")
DOCKER_BUILD_ARGS+=(--build-arg "no_proxy=${NO_PROXY}") fi
fi
# --platform # --platform
DOCKER_BUILD_ARGS+=(--platform "${BUILDX_PLATFORMS-linux/amd64}") DOCKER_BUILD_ARGS+=(--platform "${BUILDX_PLATFORMS-linux/amd64}")
# --cache-from / --cache-to # --cache-from / --cache-to
if [ -n "${BUILDX_PULL_REMOTE_CACHE}" ]; then if [ -n "${BUILDX_PULL_REMOTE_CACHE}" ]; then
echo "📥 Pulling cache from '${CACHE_TO_DOCKER_TAG}' before build" echo "📥 Pulling cache from '${CACHE_TO_DOCKER_TAG}' before build"
DOCKER_BUILD_ARGS+=("--cache-from=type=registry,ref=${CACHE_FROM_DOCKER_TAG},mode=max") DOCKER_BUILD_ARGS+=("--cache-from=type=registry,ref=${CACHE_FROM_DOCKER_TAG},mode=max")
else else
DOCKER_BUILD_ARGS+=("--cache-from=type=local,src=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max") DOCKER_BUILD_ARGS+=("--cache-from=type=local,src=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max")
fi fi
if [ -n "${BUILDX_PUSH_REMOTE_CACHE}" ]; then if [ -n "${BUILDX_PUSH_REMOTE_CACHE}" ]; then
echo "📤 Pushing cache to '${CACHE_TO_DOCKER_TAG}' after build" echo "📤 Pushing cache to '${CACHE_TO_DOCKER_TAG}' after build"
DOCKER_BUILD_ARGS+=("--cache-to=type=registry,ref=${CACHE_TO_DOCKER_TAG},mode=max") DOCKER_BUILD_ARGS+=("--cache-to=type=registry,ref=${CACHE_TO_DOCKER_TAG},mode=max")
else else
DOCKER_BUILD_ARGS+=("--cache-to=type=local,dest=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max") DOCKER_BUILD_ARGS+=("--cache-to=type=local,dest=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max")
fi fi
### ###
# Pushing the docker images if `--push` is passed # Pushing the docker images if `--push` is passed
### ###
if [ "${2}" == "--push" ]; then if [ "${2}" == "--push" ]; then
# output type=docker does not work with pushing # output type=docker does not work with pushing
DOCKER_BUILD_ARGS+=( DOCKER_BUILD_ARGS+=(
--output=type=image --output=type=image
--push --push
) )
else else
DOCKER_BUILD_ARGS+=( DOCKER_BUILD_ARGS+=(
--output=type=docker --output=type=docker
) )
fi fi
if [ -z "${BUILDX_BUILDER_NAME}" ]; then if [ -z "${BUILDX_BUILDER_NAME}" ]; then
BUILDX_BUILDER_NAME="$(basename "${PWD}")" BUILDX_BUILDER_NAME="$(basename "${PWD}")"
if ! docker buildx ls | grep --quiet --word-regexp "${BUILDX_BUILDER_NAME}"; then if ! docker buildx ls | grep --quiet --word-regexp "${BUILDX_BUILDER_NAME}"; then
echo "👷 Creating new Buildx Builder '${BUILDX_BUILDER_NAME}'" echo "👷 Creating new Buildx Builder '${BUILDX_BUILDER_NAME}'"
$DRY docker buildx create --name "${BUILDX_BUILDER_NAME}" $DRY docker buildx create --name "${BUILDX_BUILDER_NAME}"
BUILDX_BUILDER_CREATED="yes" BUILDX_BUILDER_CREATED="yes"
fi
fi
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' on '${BUILDX_BUILDER_NAME}'."
echo " Build reason set to: ${BUILD_REASON}"
$DRY docker buildx \
--builder "${BUILDX_BUILDER_NAME}" \
build \
"${DOCKER_BUILD_ARGS[@]}" \
.
echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'"
echo "🔎 Inspecting labels on '${TARGET_DOCKER_TAG}'"
$DRY docker inspect "${TARGET_DOCKER_TAG}" --format "{{json .Config.Labels}}"
if [ -n "${BUILDX_REMOVE_BUILDER}" ] && [ "${BUILDX_BUILDER_CREATED}" == "yes" ]; then
echo "👷 Removing Buildx Builder '${BUILDX_BUILDER_NAME}'"
$DRY docker buildx rm "${BUILDX_BUILDER_NAME}"
fi fi
fi fi
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' on '${BUILDX_BUILDER_NAME}'."
echo " Build reason set to: ${BUILD_REASON}"
$DRY docker buildx \
--builder "${BUILDX_BUILDER_NAME}" \
build \
"${DOCKER_BUILD_ARGS[@]}" \
.
echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'"
echo "🔎 Inspecting labels on '${TARGET_DOCKER_TAG}'"
$DRY docker inspect "${TARGET_DOCKER_TAG}" --format "{{json .Config.Labels}}"
if [ -n "${BUILDX_REMOVE_BUILDER}" ] && [ "${BUILDX_BUILDER_CREATED}" == "yes" ]; then
echo "👷 Removing Buildx Builder '${BUILDX_BUILDER_NAME}'"
$DRY docker buildx rm "${BUILDX_BUILDER_NAME}"
fi
done done