2017-06-09 03:14:43 -05:00
|
|
|
#!/bin/bash
|
2021-02-04 14:48:08 -06:00
|
|
|
# Clones the NetBox repository with git from Github and builds the Dockerfile
|
2017-06-09 03:14:43 -05:00
|
|
|
|
2018-04-10 03:58:31 -05:00
|
|
|
echo "▶️ $0 $*"
|
2018-04-10 04:42:06 -05:00
|
|
|
|
2017-06-09 03:14:43 -05:00
|
|
|
set -e
|
|
|
|
|
|
|
|
if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
|
2022-10-08 10:08:39 -05:00
|
|
|
_BOLD=$(tput bold)
|
|
|
|
_GREEN=$(tput setaf 2)
|
|
|
|
_CYAN=$(tput setaf 6)
|
|
|
|
_CLEAR=$(tput sgr0)
|
|
|
|
|
|
|
|
cat <<END_OF_HELP
|
|
|
|
${_BOLD}Usage:${_CLEAR} ${0} <branch> [--push]
|
|
|
|
|
|
|
|
branch The branch or tag to build. Required.
|
|
|
|
--push Pushes the built container image to the registry.
|
|
|
|
|
|
|
|
${_BOLD}You can use the following ENV variables to customize the build:${_CLEAR}
|
|
|
|
|
|
|
|
SRC_ORG Which fork of netbox to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO}).
|
|
|
|
${_GREEN}Default:${_CLEAR} netbox-community
|
|
|
|
|
|
|
|
SRC_REPO The name of the repository to use (i.e. github.com/\${SRC_ORG}/\${SRC_REPO}).
|
|
|
|
${_GREEN}Default:${_CLEAR} netbox
|
|
|
|
|
|
|
|
URL Where to fetch the code from.
|
|
|
|
Must be a git repository. Can be private.
|
|
|
|
${_GREEN}Default:${_CLEAR} https://github.com/\${SRC_ORG}/\${SRC_REPO}.git
|
|
|
|
|
|
|
|
NETBOX_PATH The path where netbox will be checkout out.
|
|
|
|
Must not be outside of the netbox-docker repository (because of Docker)!
|
|
|
|
${_GREEN}Default:${_CLEAR} .netbox
|
|
|
|
|
|
|
|
SKIP_GIT If defined, git is not invoked and \${NETBOX_PATH} will not be altered.
|
|
|
|
This may be useful, if you are manually managing the NETBOX_PATH.
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
TAG The version part of the image tag.
|
|
|
|
${_GREEN}Default:${_CLEAR}
|
|
|
|
When <branch>=master: latest
|
|
|
|
When <branch>=develop: snapshot
|
|
|
|
Else: same as <branch>
|
|
|
|
|
|
|
|
IMAGE_NAMES The names used for the image including the registry
|
|
|
|
Used for tagging the image.
|
|
|
|
${_GREEN}Default:${_CLEAR} docker.io/netboxcommunity/netbox
|
|
|
|
${_CYAN}Example:${_CLEAR} 'docker.io/netboxcommunity/netbox quay.io/netboxcommunity/netbox'
|
|
|
|
|
|
|
|
DOCKER_TAG The name of the tag which is applied to the image.
|
|
|
|
Useful for pushing into another registry than hub.docker.com.
|
|
|
|
${_GREEN}Default:${_CLEAR} \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:\${TAG}
|
|
|
|
|
|
|
|
DOCKER_SHORT_TAG The name of the short tag which is applied to the
|
|
|
|
image. This is used to tag all patch releases to their
|
|
|
|
containing version e.g. v2.5.1 -> v2.5
|
|
|
|
${_GREEN}Default:${_CLEAR} \${DOCKER_REGISTRY}/\${DOCKER_ORG}/\${DOCKER_REPO}:<MAJOR>.<MINOR>
|
|
|
|
|
|
|
|
DOCKERFILE The name of Dockerfile to use.
|
|
|
|
${_GREEN}Default:${_CLEAR} Dockerfile
|
|
|
|
|
|
|
|
DOCKER_FROM The base image to use.
|
|
|
|
${_GREEN}Default:${_CLEAR} 'ubuntu:22.04'
|
|
|
|
|
|
|
|
BUILDX_PLATFORMS
|
|
|
|
Specifies the platform(s) to build the image for.
|
|
|
|
${_CYAN}Example:${_CLEAR} 'linux/amd64,linux/arm64'
|
|
|
|
${_GREEN}Default:${_CLEAR} 'linux/amd64'
|
|
|
|
|
|
|
|
BUILDX_BUILDER_NAME
|
|
|
|
If defined, the image build will be assigned to the given builder.
|
|
|
|
If you specify this variable, make sure that the builder exists.
|
|
|
|
If this value is not defined, a new builx builder with the directory name of the
|
|
|
|
current directory (i.e. '$(basename "${PWD}")') is created."
|
|
|
|
${_CYAN}Example:${_CLEAR} 'clever_lovelace'
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
BUILDX_REMOVE_BUILDER
|
|
|
|
If defined (and only if BUILDX_BUILDER_NAME is undefined),
|
|
|
|
then the buildx builder created by this script will be removed after use.
|
|
|
|
This is useful if you build NetBox Docker on an automated system that does
|
|
|
|
not manage the builders for you.
|
|
|
|
${_CYAN}Example:${_CLEAR} 'on'
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
HTTP_PROXY The proxy to use for http requests.
|
|
|
|
${_CYAN}Example:${_CLEAR} http://proxy.domain.tld:3128
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
NO_PROXY Comma-separated list of domain extensions proxy should not be used for.
|
|
|
|
${_CYAN}Example:${_CLEAR} .domain1.tld,.domain2.tld
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
DEBUG If defined, the script does not stop when certain checks are unsatisfied.
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
DRY_RUN Prints all build statements instead of running them.
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
GH_ACTION If defined, special 'echo' statements are enabled that set the
|
|
|
|
following environment variables in Github Actions:
|
|
|
|
- FINAL_DOCKER_TAG: The final value of the DOCKER_TAG env variable
|
|
|
|
${_GREEN}Default:${_CLEAR} undefined
|
|
|
|
|
|
|
|
${_BOLD}Examples:${_CLEAR}
|
|
|
|
|
|
|
|
${0} master
|
|
|
|
This will fetch the latest 'master' branch, build a Docker Image and tag it
|
|
|
|
'netboxcommunity/netbox:latest'.
|
|
|
|
|
|
|
|
${0} develop
|
|
|
|
This will fetch the latest 'develop' branch, build a Docker Image and tag it
|
|
|
|
'netboxcommunity/netbox:snapshot'.
|
|
|
|
|
|
|
|
${0} v2.6.6
|
|
|
|
This will fetch the 'v2.6.6' tag, build a Docker Image and tag it
|
|
|
|
'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'.
|
|
|
|
END_OF_HELP
|
2017-06-09 03:14:43 -05:00
|
|
|
|
|
|
|
if [ "${1}x" == "x" ]; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-03-15 07:01:07 -05:00
|
|
|
# Check if we have everything needed for the build
|
|
|
|
source ./build-functions/check-commands.sh
|
2023-04-19 03:14:44 -05:00
|
|
|
# Load all build functions
|
|
|
|
source ./build-functions/get-public-image-config.sh
|
2021-10-05 03:22:00 -05:00
|
|
|
source ./build-functions/gh-functions.sh
|
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
IMAGE_NAMES="${IMAGE_NAMES-docker.io/netboxcommunity/netbox}"
|
|
|
|
IFS=' ' read -ra IMAGE_NAMES <<<"${IMAGE_NAMES}"
|
|
|
|
|
2019-10-13 09:00:42 -05:00
|
|
|
###
|
2019-12-23 10:53:19 -06:00
|
|
|
# Enabling dry-run mode
|
2019-10-13 09:00:42 -05:00
|
|
|
###
|
2019-10-14 14:54:49 -05:00
|
|
|
if [ -z "${DRY_RUN}" ]; then
|
2019-10-13 09:00:42 -05:00
|
|
|
DRY=""
|
|
|
|
else
|
|
|
|
echo "⚠️ DRY_RUN MODE ON ⚠️"
|
|
|
|
DRY="echo"
|
|
|
|
fi
|
|
|
|
|
2021-10-05 03:22:00 -05:00
|
|
|
gh_echo "::group::⤵️ Fetching the NetBox source code"
|
|
|
|
|
2019-10-10 05:42:10 -05:00
|
|
|
###
|
2021-02-04 14:48:08 -06:00
|
|
|
# Variables for fetching the NetBox source
|
2019-10-10 05:42:10 -05:00
|
|
|
###
|
2019-07-02 14:32:58 -05:00
|
|
|
SRC_ORG="${SRC_ORG-netbox-community}"
|
2018-02-02 05:48:38 -06:00
|
|
|
SRC_REPO="${SRC_REPO-netbox}"
|
2019-12-10 14:44:11 -06:00
|
|
|
NETBOX_BRANCH="${1}"
|
2019-10-14 14:54:49 -05:00
|
|
|
URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}.git}"
|
2019-11-20 15:25:31 -06:00
|
|
|
NETBOX_PATH="${NETBOX_PATH-.netbox}"
|
2018-02-02 05:48:38 -06:00
|
|
|
|
2019-10-13 09:00:42 -05:00
|
|
|
###
|
2021-02-04 14:48:08 -06:00
|
|
|
# Fetching the NetBox source
|
2019-10-13 09:00:42 -05:00
|
|
|
###
|
2021-02-08 05:16:04 -06:00
|
|
|
if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ]; then
|
2021-09-13 02:09:24 -05:00
|
|
|
REMOTE_EXISTS=$(git ls-remote --heads --tags "${URL}" "${NETBOX_BRANCH}" | wc -l)
|
2021-09-13 15:50:06 -05:00
|
|
|
if [ "${REMOTE_EXISTS}" == "0" ]; then
|
2021-09-13 02:09:24 -05:00
|
|
|
echo "❌ Remote branch '${NETBOX_BRANCH}' not found in '${URL}'; Nothing to do"
|
2023-01-28 08:42:32 -06:00
|
|
|
gh_out "skipped=true"
|
2021-09-13 02:09:24 -05:00
|
|
|
exit 0
|
|
|
|
fi
|
2021-02-04 14:48:08 -06:00
|
|
|
echo "🌐 Checking out '${NETBOX_BRANCH}' of NetBox from the url '${URL}' into '${NETBOX_PATH}'"
|
2019-10-14 14:54:49 -05:00
|
|
|
if [ ! -d "${NETBOX_PATH}" ]; then
|
2019-12-10 14:44:11 -06:00
|
|
|
$DRY git clone -q --depth 10 -b "${NETBOX_BRANCH}" "${URL}" "${NETBOX_PATH}"
|
2019-10-14 14:54:49 -05:00
|
|
|
fi
|
2019-10-13 09:00:42 -05:00
|
|
|
|
|
|
|
(
|
2019-10-14 14:54:49 -05:00
|
|
|
$DRY cd "${NETBOX_PATH}"
|
2021-06-21 06:17:08 -05:00
|
|
|
# shellcheck disable=SC2030
|
2019-10-14 14:54:49 -05:00
|
|
|
if [ -n "${HTTP_PROXY}" ]; then
|
|
|
|
git config http.proxy "${HTTP_PROXY}"
|
|
|
|
fi
|
2019-10-13 09:00:42 -05:00
|
|
|
|
2019-10-14 14:54:49 -05:00
|
|
|
$DRY git remote set-url origin "${URL}"
|
2019-12-10 14:44:11 -06:00
|
|
|
$DRY git fetch -qp --depth 10 origin "${NETBOX_BRANCH}"
|
2019-10-14 14:54:49 -05:00
|
|
|
$DRY git checkout -qf FETCH_HEAD
|
|
|
|
$DRY git prune
|
2019-10-13 09:00:42 -05:00
|
|
|
)
|
2021-02-04 14:48:08 -06:00
|
|
|
echo "✅ Checked out NetBox"
|
2019-10-13 09:00:42 -05:00
|
|
|
fi
|
|
|
|
|
2021-10-05 03:22:00 -05:00
|
|
|
gh_echo "::endgroup::"
|
|
|
|
gh_echo "::group::🧮 Calculating Values"
|
|
|
|
|
2019-10-10 05:42:10 -05:00
|
|
|
###
|
|
|
|
# Determining the value for DOCKERFILE
|
|
|
|
# and checking whether it exists
|
|
|
|
###
|
|
|
|
DOCKERFILE="${DOCKERFILE-Dockerfile}"
|
2019-03-27 06:00:32 -05:00
|
|
|
if [ ! -f "${DOCKERFILE}" ]; then
|
2019-10-10 05:42:10 -05:00
|
|
|
echo "🚨 The Dockerfile ${DOCKERFILE} doesn't exist."
|
2019-03-27 06:00:32 -05:00
|
|
|
|
2019-10-14 14:54:49 -05:00
|
|
|
if [ -z "${DEBUG}" ]; then
|
2019-03-27 06:00:32 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "⚠️ Would exit here with code '1', but DEBUG is enabled."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-04-08 08:45:56 -05:00
|
|
|
###
|
|
|
|
# Determining the value for DOCKER_FROM
|
|
|
|
###
|
2020-05-14 06:50:09 -05:00
|
|
|
if [ -z "$DOCKER_FROM" ]; then
|
2023-03-15 03:13:09 -05:00
|
|
|
DOCKER_FROM="docker.io/ubuntu:22.04"
|
2020-04-09 03:26:21 -05:00
|
|
|
fi
|
2020-04-08 08:45:56 -05:00
|
|
|
|
2019-11-17 10:07:02 -06:00
|
|
|
###
|
2019-12-23 10:53:19 -06:00
|
|
|
# Variables for labelling the docker image
|
2019-11-17 10:07:02 -06:00
|
|
|
###
|
2019-12-02 14:17:40 -06:00
|
|
|
BUILD_DATE="$(date -u '+%Y-%m-%dT%H:%M+00:00')"
|
2019-11-17 10:07:02 -06:00
|
|
|
|
2022-05-25 09:43:23 -05:00
|
|
|
if [ -d ".git" ] && [ -z "${SKIP_GIT}" ]; then
|
2019-11-17 10:07:02 -06:00
|
|
|
GIT_REF="$(git rev-parse HEAD)"
|
|
|
|
fi
|
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# Read the project version from the `VERSION` file and trim it, see https://stackoverflow.com/a/3232433/172132
|
2019-11-17 10:07:02 -06:00
|
|
|
PROJECT_VERSION="${PROJECT_VERSION-$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' VERSION)}"
|
|
|
|
|
|
|
|
# Get the Git information from the netbox directory
|
2022-05-25 09:43:23 -05:00
|
|
|
if [ -d "${NETBOX_PATH}/.git" ] && [ -z "${SKIP_GIT}" ]; then
|
2021-02-08 05:16:04 -06:00
|
|
|
NETBOX_GIT_REF=$(
|
|
|
|
cd "${NETBOX_PATH}"
|
|
|
|
git rev-parse HEAD
|
|
|
|
)
|
|
|
|
NETBOX_GIT_BRANCH=$(
|
|
|
|
cd "${NETBOX_PATH}"
|
|
|
|
git rev-parse --abbrev-ref HEAD
|
|
|
|
)
|
|
|
|
NETBOX_GIT_URL=$(
|
|
|
|
cd "${NETBOX_PATH}"
|
|
|
|
git remote get-url origin
|
|
|
|
)
|
2019-11-17 10:07:02 -06:00
|
|
|
fi
|
|
|
|
|
2019-10-10 05:42:10 -05:00
|
|
|
###
|
2019-12-23 10:53:19 -06:00
|
|
|
# Variables for tagging the docker image
|
2019-10-10 05:42:10 -05:00
|
|
|
###
|
2019-11-17 10:07:02 -06:00
|
|
|
DOCKER_REGISTRY="${DOCKER_REGISTRY-docker.io}"
|
2019-02-06 04:46:20 -06:00
|
|
|
DOCKER_ORG="${DOCKER_ORG-netboxcommunity}"
|
2018-02-02 05:48:38 -06:00
|
|
|
DOCKER_REPO="${DOCKER_REPO-netbox}"
|
2019-12-10 14:44:11 -06:00
|
|
|
case "${NETBOX_BRANCH}" in
|
2021-02-08 05:16:04 -06:00
|
|
|
master)
|
|
|
|
TAG="${TAG-latest}"
|
|
|
|
;;
|
|
|
|
develop)
|
|
|
|
TAG="${TAG-snapshot}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
TAG="${TAG-$NETBOX_BRANCH}"
|
|
|
|
;;
|
2018-02-02 05:48:38 -06:00
|
|
|
esac
|
|
|
|
|
2019-10-10 07:48:45 -05:00
|
|
|
###
|
2022-06-15 01:22:11 -05:00
|
|
|
# composing the final TARGET_DOCKER_TAG
|
2019-10-10 07:48:45 -05:00
|
|
|
###
|
2022-06-15 01:22:11 -05:00
|
|
|
TARGET_DOCKER_TAG="${DOCKER_TAG-${TAG}}"
|
|
|
|
TARGET_DOCKER_TAG_PROJECT="${TARGET_DOCKER_TAG}-${PROJECT_VERSION}"
|
2019-10-10 07:48:45 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
###
|
|
|
|
# composing the additional DOCKER_SHORT_TAG,
|
|
|
|
# i.e. "v2.6.1" becomes "v2.6",
|
|
|
|
# which is only relevant for version tags
|
|
|
|
# Also let "latest" follow the highest version
|
|
|
|
###
|
|
|
|
if [[ "${TAG}" =~ ^v([0-9]+)\.([0-9]+)\.[0-9]+$ ]]; then
|
|
|
|
MAJOR=${BASH_REMATCH[1]}
|
|
|
|
MINOR=${BASH_REMATCH[2]}
|
|
|
|
|
|
|
|
TARGET_DOCKER_SHORT_TAG="${DOCKER_SHORT_TAG-v${MAJOR}.${MINOR}}"
|
|
|
|
TARGET_DOCKER_LATEST_TAG="latest"
|
|
|
|
TARGET_DOCKER_SHORT_TAG_PROJECT="${TARGET_DOCKER_SHORT_TAG}-${PROJECT_VERSION}"
|
|
|
|
TARGET_DOCKER_LATEST_TAG_PROJECT="${TARGET_DOCKER_LATEST_TAG}-${PROJECT_VERSION}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
IMAGE_NAME_TAGS=()
|
|
|
|
for IMAGE_NAME in "${IMAGE_NAMES[@]}"; do
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_TAG}")
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_TAG_PROJECT}")
|
|
|
|
done
|
|
|
|
if [ -n "${TARGET_DOCKER_SHORT_TAG}" ]; then
|
|
|
|
for IMAGE_NAME in "${IMAGE_NAMES[@]}"; do
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_SHORT_TAG}")
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_SHORT_TAG_PROJECT}")
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_LATEST_TAG}")
|
|
|
|
IMAGE_NAME_TAGS+=("${IMAGE_NAME}:${TARGET_DOCKER_LATEST_TAG_PROJECT}")
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2023-03-15 03:13:09 -05:00
|
|
|
FINAL_DOCKER_TAG="${IMAGE_NAME_TAGS[0]}"
|
2022-06-15 01:22:11 -05:00
|
|
|
gh_env "FINAL_DOCKER_TAG=${IMAGE_NAME_TAGS[0]}"
|
2021-10-05 03:22:00 -05:00
|
|
|
|
2019-10-10 07:48:45 -05:00
|
|
|
###
|
2022-06-15 01:22:11 -05:00
|
|
|
# Checking if the build is necessary,
|
|
|
|
# meaning build only if one of those values changed:
|
2023-04-19 03:14:44 -05:00
|
|
|
# - a new tag is beeing created
|
2022-06-15 01:22:11 -05:00
|
|
|
# - base image digest
|
|
|
|
# - netbox git ref (Label: netbox.git-ref)
|
|
|
|
# - netbox-docker git ref (Label: org.opencontainers.image.revision)
|
2019-10-10 07:48:45 -05:00
|
|
|
###
|
2023-04-19 03:14:44 -05:00
|
|
|
# Load information from registry (only for first registry in "IMAGE_NAMES")
|
2022-06-15 01:22:11 -05:00
|
|
|
SHOULD_BUILD="false"
|
|
|
|
BUILD_REASON=""
|
|
|
|
if [ -z "${GH_ACTION}" ]; then
|
|
|
|
# Asuming non Github builds should always proceed
|
|
|
|
SHOULD_BUILD="true"
|
|
|
|
BUILD_REASON="${BUILD_REASON} interactive"
|
2023-04-19 03:14:44 -05:00
|
|
|
elif [ "false" == "$(check_if_tags_exists "${IMAGE_NAMES[0]}" "$TARGET_DOCKER_TAG")" ]; then
|
|
|
|
SHOULD_BUILD="true"
|
|
|
|
BUILD_REASON="${BUILD_REASON} newtag"
|
2023-03-15 03:13:09 -05:00
|
|
|
else
|
|
|
|
echo "Checking labels for '${FINAL_DOCKER_TAG}'"
|
|
|
|
BASE_LAST_LAYER=$(get_image_last_layer "${DOCKER_FROM}")
|
2023-03-16 01:50:24 -05:00
|
|
|
OLD_BASE_LAST_LAYER=$(get_image_label netbox.last-base-image-layer "${FINAL_DOCKER_TAG}")
|
2023-03-15 03:13:09 -05:00
|
|
|
NETBOX_GIT_REF_OLD=$(get_image_label netbox.git-ref "${FINAL_DOCKER_TAG}")
|
|
|
|
GIT_REF_OLD=$(get_image_label org.opencontainers.image.revision "${FINAL_DOCKER_TAG}")
|
2022-06-15 01:22:11 -05:00
|
|
|
|
2023-03-16 01:50:24 -05:00
|
|
|
if [ "${BASE_LAST_LAYER}" != "${OLD_BASE_LAST_LAYER}" ]; then
|
2022-06-15 01:22:11 -05:00
|
|
|
SHOULD_BUILD="true"
|
2022-10-07 01:08:30 -05:00
|
|
|
BUILD_REASON="${BUILD_REASON} ubuntu"
|
2022-06-15 01:22:11 -05:00
|
|
|
fi
|
|
|
|
if [ "${NETBOX_GIT_REF}" != "${NETBOX_GIT_REF_OLD}" ]; then
|
|
|
|
SHOULD_BUILD="true"
|
|
|
|
BUILD_REASON="${BUILD_REASON} netbox"
|
|
|
|
fi
|
|
|
|
if [ "${GIT_REF}" != "${GIT_REF_OLD}" ]; then
|
|
|
|
SHOULD_BUILD="true"
|
|
|
|
BUILD_REASON="${BUILD_REASON} netbox-docker"
|
2018-02-22 07:49:38 -06:00
|
|
|
fi
|
2022-06-15 01:22:11 -05:00
|
|
|
fi
|
2021-09-17 09:37:23 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
if [ "${SHOULD_BUILD}" != "true" ]; then
|
|
|
|
echo "Build skipped because sources didn't change"
|
2023-01-28 08:42:32 -06:00
|
|
|
gh_out "skipped=true"
|
2022-06-15 01:22:11 -05:00
|
|
|
exit 0 # Nothing to do -> exit
|
|
|
|
else
|
2023-01-28 08:42:32 -06:00
|
|
|
gh_out "skipped=false"
|
2022-06-15 01:22:11 -05:00
|
|
|
fi
|
|
|
|
gh_echo "::endgroup::"
|
2018-02-22 07:49:38 -06:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
###
|
|
|
|
# Build the image
|
|
|
|
###
|
|
|
|
gh_echo "::group::🏗 Building the image"
|
|
|
|
###
|
|
|
|
# Composing all arguments for `docker build`
|
|
|
|
###
|
|
|
|
DOCKER_BUILD_ARGS=(
|
|
|
|
--pull
|
|
|
|
--target main
|
|
|
|
-f "${DOCKERFILE}"
|
|
|
|
)
|
|
|
|
for IMAGE_NAME in "${IMAGE_NAME_TAGS[@]}"; do
|
|
|
|
DOCKER_BUILD_ARGS+=(-t "${IMAGE_NAME}")
|
|
|
|
done
|
2017-06-09 03:14:43 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
# --label
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
|
|
|
--label "netbox.original-tag=${TARGET_DOCKER_TAG_PROJECT}"
|
|
|
|
--label "org.opencontainers.image.created=${BUILD_DATE}"
|
|
|
|
--label "org.opencontainers.image.version=${PROJECT_VERSION}"
|
|
|
|
)
|
|
|
|
if [ -d ".git" ] && [ -z "${SKIP_GIT}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
|
|
|
--label "org.opencontainers.image.revision=${GIT_REF}"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
if [ -d "${NETBOX_PATH}/.git" ] && [ -z "${SKIP_GIT}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
|
|
|
--label "netbox.git-branch=${NETBOX_GIT_BRANCH}"
|
|
|
|
--label "netbox.git-ref=${NETBOX_GIT_REF}"
|
|
|
|
--label "netbox.git-url=${NETBOX_GIT_URL}"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
if [ -n "${BUILD_REASON}" ]; then
|
|
|
|
BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON")
|
|
|
|
DOCKER_BUILD_ARGS+=(--label "netbox.build-reason=${BUILD_REASON}")
|
2023-03-16 01:50:24 -05:00
|
|
|
DOCKER_BUILD_ARGS+=(--label "netbox.last-base-image-layer=${BASE_LAST_LAYER}")
|
2022-06-15 01:22:11 -05:00
|
|
|
fi
|
2019-10-10 05:42:10 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
# --build-arg
|
|
|
|
DOCKER_BUILD_ARGS+=(--build-arg "NETBOX_PATH=${NETBOX_PATH}")
|
2019-03-27 06:00:32 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
if [ -n "${DOCKER_FROM}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(--build-arg "FROM=${DOCKER_FROM}")
|
|
|
|
fi
|
|
|
|
# shellcheck disable=SC2031
|
|
|
|
if [ -n "${HTTP_PROXY}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(--build-arg "http_proxy=${HTTP_PROXY}")
|
|
|
|
DOCKER_BUILD_ARGS+=(--build-arg "https_proxy=${HTTPS_PROXY}")
|
|
|
|
fi
|
|
|
|
if [ -n "${NO_PROXY}" ]; then
|
|
|
|
DOCKER_BUILD_ARGS+=(--build-arg "no_proxy=${NO_PROXY}")
|
|
|
|
fi
|
2019-11-17 10:07:02 -06:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
DOCKER_BUILD_ARGS+=(--platform "${BUILDX_PLATFORM-linux/amd64}")
|
|
|
|
if [ "${2}" == "--push" ]; then
|
|
|
|
# output type=docker does not work with pushing
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
|
|
|
--output=type=image
|
|
|
|
--push
|
|
|
|
)
|
|
|
|
else
|
|
|
|
DOCKER_BUILD_ARGS+=(
|
|
|
|
--output=type=docker
|
|
|
|
)
|
|
|
|
fi
|
2019-10-14 14:54:49 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
###
|
|
|
|
# Building the docker image
|
|
|
|
###
|
|
|
|
if [ -z "${BUILDX_BUILDER_NAME}" ]; then
|
|
|
|
BUILDX_BUILDER_NAME="$(basename "${PWD}")"
|
|
|
|
fi
|
|
|
|
if ! docker buildx ls | grep --quiet --word-regexp "${BUILDX_BUILDER_NAME}"; then
|
|
|
|
echo "👷 Creating new Buildx Builder '${BUILDX_BUILDER_NAME}'"
|
|
|
|
$DRY docker buildx create --name "${BUILDX_BUILDER_NAME}"
|
|
|
|
BUILDX_BUILDER_CREATED="yes"
|
|
|
|
fi
|
2019-03-27 06:00:32 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG_PROJECT}'."
|
|
|
|
echo " Build reason set to: ${BUILD_REASON}"
|
|
|
|
$DRY docker buildx \
|
|
|
|
--builder "${BUILDX_BUILDER_NAME}" \
|
|
|
|
build \
|
|
|
|
"${DOCKER_BUILD_ARGS[@]}" \
|
|
|
|
.
|
|
|
|
echo "✅ Finished building the Docker images"
|
|
|
|
gh_echo "::endgroup::" # End group for Build
|
|
|
|
|
|
|
|
gh_echo "::group::🏗 Image Labels"
|
|
|
|
echo "🔎 Inspecting labels on '${IMAGE_NAME_TAGS[0]}'"
|
|
|
|
$DRY docker inspect "${IMAGE_NAME_TAGS[0]}" --format "{{json .Config.Labels}}" | jq
|
|
|
|
gh_echo "::endgroup::"
|
2021-10-05 03:22:00 -05:00
|
|
|
|
2022-06-15 01:22:11 -05:00
|
|
|
gh_echo "::group::🏗 Clean up"
|
|
|
|
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
|
|
|
|
gh_echo "::endgroup::"
|