2017-09-29 04:38:19 -05:00
|
|
|
#!/bin/bash
|
2018-03-05 07:29:24 -06:00
|
|
|
# Builds the latest released version
|
2017-09-29 04:38:19 -05:00
|
|
|
|
2018-04-10 03:58:31 -05:00
|
|
|
echo "▶️ $0 $*"
|
|
|
|
|
2018-08-28 11:21:08 -05:00
|
|
|
if [ ! -z "${GITHUB_OAUTH_CLIENT_ID}" ] && [ ! -z "${GITHUB_OAUTH_CLIENT_SECRET}" ]; then
|
|
|
|
echo "🗝 Performing authenticated Github API calls."
|
|
|
|
GITHUB_OAUTH_PARAMS="client_id=${GITHUB_OAUTH_CLIENT_ID}&client_secret=${GITHUB_OAUTH_CLIENT_SECRET}"
|
|
|
|
else
|
|
|
|
echo "🕶 Performing unauthenticated Github API calls. This might result in lower Github rate limits!"
|
|
|
|
GITHUB_OAUTH_PARAMS=""
|
|
|
|
fi
|
|
|
|
|
2018-01-30 04:22:43 -06:00
|
|
|
ORIGINAL_GITHUB_REPO="digitalocean/netbox"
|
|
|
|
GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}"
|
2018-08-28 11:21:08 -05:00
|
|
|
URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/releases?${GITHUB_OAUTH_PARAMS}"
|
2017-09-29 04:38:19 -05:00
|
|
|
|
2017-10-02 04:08:50 -05:00
|
|
|
JQ_LATEST="group_by(.prerelease) | .[] | sort_by(.published_at) | reverse | .[0] | select(.prerelease==${PRERELEASE-false}) | .tag_name"
|
|
|
|
|
2018-03-05 07:29:24 -06:00
|
|
|
CURL="curl -sS"
|
2017-10-02 04:08:50 -05:00
|
|
|
|
2018-01-30 04:22:43 -06:00
|
|
|
VERSION=$($CURL "${URL_RELEASES}" | jq -r "${JQ_LATEST}")
|
2017-10-13 02:43:16 -05:00
|
|
|
|
|
|
|
# Check if the prerelease version is actually higher than stable version
|
|
|
|
if [ "${PRERELEASE}" == "true" ]; then
|
|
|
|
JQ_STABLE="group_by(.prerelease) | .[] | sort_by(.published_at) | reverse | .[0] | select(.prerelease==false) | .tag_name"
|
2018-03-05 07:29:24 -06:00
|
|
|
STABLE_VERSION=$($CURL "${URL_RELEASES}" | jq -r "${JQ_STABLE}")
|
2017-10-13 02:43:16 -05:00
|
|
|
|
2018-03-05 07:29:24 -06:00
|
|
|
# shellcheck disable=SC2003
|
2017-10-13 02:43:16 -05:00
|
|
|
MAJOR_STABLE=$(expr match "${STABLE_VERSION}" 'v\([0-9]\+\)')
|
2018-03-05 07:29:24 -06:00
|
|
|
# shellcheck disable=SC2003
|
2017-10-13 02:43:16 -05:00
|
|
|
MINOR_STABLE=$(expr match "${STABLE_VERSION}" 'v[0-9]\+\.\([0-9]\+\)')
|
2018-03-05 07:29:24 -06:00
|
|
|
# shellcheck disable=SC2003
|
2017-10-13 02:43:16 -05:00
|
|
|
MAJOR_UNSTABLE=$(expr match "${VERSION}" 'v\([0-9]\+\)')
|
2018-03-05 07:29:24 -06:00
|
|
|
# shellcheck disable=SC2003
|
2017-10-13 02:43:16 -05:00
|
|
|
MINOR_UNSTABLE=$(expr match "${VERSION}" 'v[0-9]\+\.\([0-9]\+\)')
|
2017-09-29 04:38:19 -05:00
|
|
|
|
2017-10-13 02:43:16 -05:00
|
|
|
if ( [ "$MAJOR_STABLE" -eq "$MAJOR_UNSTABLE" ] && [ "$MINOR_STABLE" -ge "$MINOR_UNSTABLE" ] ) \
|
|
|
|
|| [ "$MAJOR_STABLE" -gt "$MAJOR_UNSTABLE" ]; then
|
2018-03-05 07:29:24 -06:00
|
|
|
echo "❎ Latest unstable version ('$VERSION') is not higher than the latest stable version ('$STABLE_VERSION')."
|
2018-03-05 07:30:30 -06:00
|
|
|
if [ -z "$DEBUG" ]; then
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
echo "⚠️ Would exit here with code '0', but DEBUG is enabled."
|
|
|
|
fi
|
2017-10-13 02:43:16 -05:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-01-30 04:22:43 -06:00
|
|
|
# Check if that version is not already available on docker hub:
|
|
|
|
ORIGINAL_DOCKERHUB_REPO="ninech/netbox"
|
|
|
|
DOCKERHUB_REPO="${DOCKERHUB_REPO-$ORIGINAL_DOCKERHUB_REPO}"
|
|
|
|
URL_DOCKERHUB_TOKEN="https://auth.docker.io/token?service=registry.docker.io&scope=repository:${DOCKERHUB_REPO}:pull"
|
|
|
|
BEARER_TOKEN="$($CURL "${URL_DOCKERHUB_TOKEN}" | jq -r .token)"
|
|
|
|
|
|
|
|
URL_DOCKERHUB_TAG="https://registry.hub.docker.com/v2/${DOCKERHUB_REPO}/tags/list"
|
|
|
|
AUTHORIZATION_HEADER="Authorization: Bearer ${BEARER_TOKEN}"
|
2018-03-05 08:15:27 -06:00
|
|
|
|
|
|
|
if [ -z "$VARIANT" ]; then
|
|
|
|
DOCKER_TAG="${VERSION}"
|
|
|
|
else
|
|
|
|
DOCKER_TAG="${VERSION}-${VARIANT}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ALREADY_BUILT="$($CURL -H "${AUTHORIZATION_HEADER}" "${URL_DOCKERHUB_TAG}" | jq -e ".tags | any(.==\"${DOCKER_TAG}\")")"
|
2018-01-30 04:22:43 -06:00
|
|
|
|
|
|
|
if [ "$ALREADY_BUILT" == "false" ]; then
|
2018-03-05 07:29:24 -06:00
|
|
|
# shellcheck disable=SC2068
|
2018-01-30 04:22:43 -06:00
|
|
|
./build.sh "${VERSION}" $@
|
2018-03-05 09:28:35 -06:00
|
|
|
exit $?
|
2018-01-30 04:22:43 -06:00
|
|
|
else
|
2018-03-05 08:15:27 -06:00
|
|
|
echo "✅ ${DOCKER_TAG} already exists on https://hub.docker.com/r/${DOCKERHUB_REPO}"
|
2018-03-05 09:28:35 -06:00
|
|
|
exit 0
|
2018-01-30 04:22:43 -06:00
|
|
|
fi
|