superlint/.automation/upload-docker.sh

644 lines
22 KiB
Bash
Raw Normal View History

2020-06-23 12:02:45 -04:00
#!/usr/bin/env bash
2019-11-08 09:31:16 -05:00
################################################################################
############# Deploy Container to DockerHub @admiralawkbar #####################
################################################################################
# NOTES: This script is used to upload a Dockerfile to DockerHub
2020-06-18 15:02:54 -04:00
# under the GitHub organization
2019-11-08 09:31:16 -05:00
# Its based on being built from a GitHub Action, but could be easily updated
# To be ran in a different medium.
#
# PRE-Requirements:
2019-11-08 09:31:16 -05:00
# - Dockerfile
# - System with Docker installed
# - Global variables met
###########
# Globals #
###########
2020-08-20 13:58:11 -04:00
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace
GITHUB_REPOSITORY="${GITHUB_REPOSITORY}" # GitHub Org/Repo passed from system
DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub
DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub
GCR_USERNAME="${GCR_USERNAME}" # Username to login to GitHub package registry
GCR_TOKEN="${GCR_TOKEN}" # Password to login to GitHub package registry
REGISTRY="${REGISTRY}" # What registry to upload | <GCR> or <Docker>
IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image
IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image
DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded
MAJOR_TAG='' # Major tag version if we need to update it
UPDATE_MAJOR_TAG=0 # Flag to deploy the major tag version as well
2020-09-01 13:44:05 -04:00
GCR_URL='ghcr.io' # URL to Github Container Registry
2020-08-20 13:58:11 -04:00
DOCKER_IMAGE_REPO='' # Docker tag for the image when created
GCR_IMAGE_REPO='' # Docker tag for the image when created
FOUND_IMAGE=0 # Flag for if the image has already been built
2020-08-24 14:00:34 -04:00
CONTAINER_URL='' # Final URL to upload
2020-08-20 13:58:11 -04:00
2020-09-02 11:18:13 -04:00
###########################################################
# Dynamic build variables to pass to container when built #
###########################################################
2020-10-30 21:27:20 -04:00
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') # Current build date EX> "2017-08-28T09:24:41Z"
BUILD_REVISION=$(git rev-parse --short HEAD) # Current git commit EX> "e89faa7"
BUILD_VERSION='' # Current version of the container being built
(( LOG_TRACE=LOG_DEBUG=LOG_VERBOSE=LOG_NOTICE=LOG_WARN=LOG_ERROR="true" )) # Enable all loging
export LOG_TRACE LOG_DEBUG LOG_VERBOSE LOG_NOTICE LOG_WARN LOG_ERROR
2020-09-02 11:18:13 -04:00
#########################
# Source Function Files #
#########################
# shellcheck source=/dev/null
source "${GITHUB_WORKSPACE}/lib/log.sh" # Source the function script(s)
2019-11-08 09:31:16 -05:00
################################################################################
############################ FUNCTIONS BELOW ###################################
################################################################################
################################################################################
#### Function Header ###########################################################
2020-07-01 17:40:40 -04:00
Header() {
2020-07-30 15:15:42 -04:00
info "-------------------------------------------------------"
info "---- GitHub Actions Upload image to [${REGISTRY}] ----"
info "-------------------------------------------------------"
2019-11-08 09:31:16 -05:00
}
################################################################################
#### Function ValidateInput ####################################################
2020-07-01 17:40:40 -04:00
ValidateInput() {
2019-11-08 09:31:16 -05:00
# Need to validate we have the basic variables
################
# Print header #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Gathering variables..."
info "----------------------------------------------"
2019-11-08 09:31:16 -05:00
#############################
2019-11-08 09:31:16 -05:00
# Validate GITHUB_WORKSPACE #
#############################
2020-07-21 13:09:07 -04:00
if [ -z "${GITHUB_WORKSPACE}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [GITHUB_WORKSPACE]!"
fatal "[${GITHUB_WORKSPACE}]"
2019-11-08 09:31:16 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[GITHUB_WORKSPACE]${F[B]}, value:${F[W]}[${GITHUB_WORKSPACE}]"
2019-11-08 09:31:16 -05:00
fi
#####################
# Validate REGISTRY #
#####################
2020-07-21 13:09:07 -04:00
if [ -z "${REGISTRY}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [REGISTRY]!"
fatal "[${REGISTRY}]"
2019-11-08 09:31:16 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[REGISTRY]${F[B]}, value:${F[W]}[${REGISTRY}]"
2019-11-08 09:31:16 -05:00
fi
#####################################################
# See if we need values for GitHub package Registry #
#####################################################
2020-08-20 13:58:11 -04:00
if [[ ${REGISTRY} == "GCR" ]]; then
#########################
2020-08-20 13:58:11 -04:00
# Validate GCR_USERNAME #
#########################
2020-08-20 13:58:11 -04:00
if [ -z "${GCR_USERNAME}" ]; then
error "Failed to get [GCR_USERNAME]!"
fatal "[${GCR_USERNAME}]"
else
2020-08-20 13:58:11 -04:00
info "Successfully found:${F[W]}[GCR_USERNAME]${F[B]}, value:${F[W]}[${GCR_USERNAME}]"
fi
######################
2020-08-20 13:58:11 -04:00
# Validate GCR_TOKEN #
######################
2020-08-20 13:58:11 -04:00
if [ -z "${GCR_TOKEN}" ]; then
error "Failed to get [GCR_TOKEN]!"
fatal "[${GCR_TOKEN}]"
else
2020-08-20 13:58:11 -04:00
info "Successfully found:${F[W]}[GCR_TOKEN]${F[B]}, value:${F[W]}[********]"
fi
########################################
# See if we need values for Ducker hub #
########################################
2020-07-21 13:09:07 -04:00
elif [[ ${REGISTRY} == "Docker" ]]; then
############################
# Validate DOCKER_USERNAME #
############################
2020-07-21 13:09:07 -04:00
if [ -z "${DOCKER_USERNAME}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [DOCKER_USERNAME]!"
fatal "[${DOCKER_USERNAME}]"
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[DOCKER_USERNAME]${F[B]}, value:${F[W]}[${DOCKER_USERNAME}]"
fi
############################
# Validate DOCKER_PASSWORD #
############################
2020-07-21 13:09:07 -04:00
if [ -z "${DOCKER_PASSWORD}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [DOCKER_PASSWORD]!"
fatal "[${DOCKER_PASSWORD}]"
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[DOCKER_PASSWORD]${F[B]}, value:${F[B]}[********]"
fi
###########################################
# We were not passed a registry to update #
###########################################
2019-11-08 09:31:16 -05:00
else
2020-07-27 17:35:53 -04:00
error "Failed to find a valid registry!"
2020-07-27 17:20:06 -04:00
fatal "Registry:[${REGISTRY}]"
2019-11-08 09:31:16 -05:00
fi
#######################
# Validate IMAGE_REPO #
#######################
2020-07-21 13:09:07 -04:00
if [ -z "${IMAGE_REPO}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [IMAGE_REPO]!"
fatal "[${IMAGE_REPO}]"
2019-11-08 09:31:16 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]"
2020-08-24 14:00:34 -04:00
# Set the docker Image repo and GCR image repo
2020-08-20 13:58:11 -04:00
DOCKER_IMAGE_REPO="${IMAGE_REPO}"
2020-08-31 15:45:30 -04:00
GCR_IMAGE_REPO="${GCR_URL}/${IMAGE_REPO}"
2020-08-24 14:00:34 -04:00
#########################
# Set the container URL #
#########################
if [[ ${REGISTRY} == "Docker" ]]; then
CONTAINER_URL="${DOCKER_IMAGE_REPO}"
elif [[ ${REGISTRY} == "GCR" ]]; then
CONTAINER_URL="${GCR_IMAGE_REPO}"
fi
2019-11-08 09:31:16 -05:00
fi
##########################
# Validate IMAGE_VERSION #
##########################
2020-07-21 13:09:07 -04:00
if [ -z "${IMAGE_VERSION}" ]; then
2020-07-30 15:15:42 -04:00
warn "Failed to get [IMAGE_VERSION]!"
info "Pulling from Branch Name..."
2020-01-09 14:04:41 -05:00
##############################
# Get the name of the branch #
##############################
2020-09-01 13:57:10 -04:00
BRANCH_NAME=$(git -C "${GITHUB_WORKSPACE}" branch --contains "${GITHUB_SHA}" | awk '{print $2}' 2>&1)
2020-01-09 14:04:41 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get branch name!"
fatal "[${BRANCH_NAME}]"
2020-01-09 14:04:41 -05:00
fi
2020-06-20 23:56:56 -04:00
##################################
# Remove non alpha-numeric chars #
##################################
2020-07-21 13:09:07 -04:00
BRANCH_NAME=$(echo "${BRANCH_NAME}" | tr -cd '[:alnum:]')
2020-01-09 14:04:41 -05:00
############################################
# Set the IMAGE_VERSION to the BRANCH_NAME #
############################################
2020-07-21 13:09:07 -04:00
IMAGE_VERSION="${BRANCH_NAME}"
2020-09-02 11:18:13 -04:00
BUILD_VERSION="${IMAGE_VERSION}"
2020-07-30 15:15:42 -04:00
info "Tag:[${IMAGE_VERSION}]"
2020-01-09 14:10:40 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[IMAGE_VERSION]${F[B]}, value:${F[W]}[${IMAGE_VERSION}]"
2020-09-02 11:18:13 -04:00
#########################
# Set the build version #
#########################
BUILD_VERSION="${IMAGE_VERSION}"
2020-01-09 14:04:41 -05:00
fi
##################################
# Set regex for getting tag info #
##################################
REGEX='(v[0-9]+\.[0-9]+\.[0-9]+)' # Matches 'v1.2.3'
######################################################################
# Check if this is a latest to a versioned release at create new tag #
######################################################################
2020-07-21 13:09:07 -04:00
if [[ ${IMAGE_VERSION} =~ ${REGEX} ]]; then
# Need to get the major version, and set flag to update
#####################
# Set the major tag #
#####################
2020-07-21 13:09:07 -04:00
MAJOR_TAG=$(echo "${IMAGE_VERSION}" | cut -d '.' -f1)
###################################
# Set flag for updating major tag #
###################################
UPDATE_MAJOR_TAG=1
2020-07-30 15:15:42 -04:00
info "- Also deploying a major tag of:[${MAJOR_TAG}]"
fi
2019-11-08 09:31:16 -05:00
############################
# Validate DOCKERFILE_PATH #
############################
2020-07-21 13:09:07 -04:00
if [ -z "${DOCKERFILE_PATH}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [DOCKERFILE_PATH]!"
fatal "[${DOCKERFILE_PATH}]"
2019-11-08 09:31:16 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:${F[W]}[DOCKERFILE_PATH]${F[B]}, value:${F[W]}[${DOCKERFILE_PATH}]"
2019-11-08 09:31:16 -05:00
fi
}
################################################################################
#### Function Authenticate #####################################################
2020-07-01 17:40:40 -04:00
Authenticate() {
################
# Pull in Vars #
################
2020-07-21 13:09:07 -04:00
USERNAME="${1}" # Name to auth with
PASSWORD="${2}" # Password to auth with
URL="${3}" # Url to auth towards
NAME="${4}" # name of the service
2019-11-08 09:31:16 -05:00
################
# Print header #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Login to ${NAME}..."
info "----------------------------------------------"
2019-11-08 09:31:16 -05:00
###################
# Auth to service #
###################
2020-07-21 13:09:07 -04:00
LOGIN_CMD=$(docker login "${URL}" --username "${USERNAME}" --password "${PASSWORD}" 2>&1)
2019-11-08 09:31:16 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2019-11-08 09:31:16 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
error "Failed to authenticate to ${NAME}!"
fatal "[${LOGIN_CMD}]"
2020-07-30 15:19:07 -04:00
else
2019-11-08 09:31:16 -05:00
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully authenticated to ${F[C]}${NAME}${F[B]}!"
2019-11-08 09:31:16 -05:00
fi
}
################################################################################
#### Function BuildImage #######################################################
2020-07-01 17:40:40 -04:00
BuildImage() {
2019-11-08 09:31:16 -05:00
################
# Print header #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
2020-08-24 14:00:34 -04:00
info "Building the Dockerfile image..."
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
2019-11-08 09:31:16 -05:00
################################
# Validate the DOCKERFILE_PATH #
################################
2020-07-21 13:09:07 -04:00
if [ ! -f "${DOCKERFILE_PATH}" ]; then
2019-11-08 09:31:16 -05:00
# No file found
2020-07-27 17:35:53 -04:00
error "failed to find Dockerfile at:[${DOCKERFILE_PATH}]"
2020-07-27 17:20:06 -04:00
error "Please make sure you give full path!"
fatal "Example:[/configs/Dockerfile] or [Dockerfile] if at root directory"
2019-11-08 09:31:16 -05:00
fi
###################
# Build the image #
###################
2020-09-04 04:09:20 -04:00
docker build --no-cache --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${BUILD_VERSION}" -t "${CONTAINER_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" . 2>&1
2019-11-08 09:31:16 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2019-11-08 09:31:16 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
fatal "failed to [build] Dockerfile!"
2019-11-08 10:00:07 -05:00
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully Built image!"
fi
########################################################
# Need to see if we need to tag a major update as well #
########################################################
2020-07-21 13:09:07 -04:00
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
# Tag the image with the major tag as well
2020-09-04 04:09:20 -04:00
docker build --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${CONTAINER_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
2020-07-27 17:35:53 -04:00
fatal "failed to [tag] Dockerfile!"
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully tagged image!"
fi
2019-11-08 09:31:16 -05:00
fi
2020-08-24 14:00:34 -04:00
#########################
# Set var to be updated #
#########################
ADDITONAL_URL=''
####################################
# Set the additional container URL #
####################################
if [[ ${REGISTRY} == "Docker" ]]; then
ADDITONAL_URL="${GCR_IMAGE_REPO}"
elif [[ ${REGISTRY} == "GCR" ]]; then
ADDITONAL_URL="${DOCKER_IMAGE_REPO}"
fi
###################
# Build the image #
###################
2020-09-04 10:52:04 -04:00
docker build --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${BUILD_VERSION}" -t "${ADDITONAL_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" . 2>&1
2020-08-20 13:58:11 -04:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-08-24 14:00:34 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [tag] Version:[${IMAGE_VERSION}] Additonal location Dockerfile!"
2020-08-20 13:58:11 -04:00
else
2020-08-24 14:00:34 -04:00
# SUCCESS
info "Successfull [tag] Version:[${IMAGE_VERSION}] of additonal image!"
2020-08-20 13:58:11 -04:00
fi
2020-09-01 12:08:40 -04:00
########################################################
# Need to see if we need to tag a major update as well #
########################################################
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
###################
# Build the image #
###################
2020-09-04 10:52:04 -04:00
docker build --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${ADDITONAL_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . 2>&1
2020-08-24 14:00:34 -04:00
2020-09-01 12:08:40 -04:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
2020-08-24 14:00:34 -04:00
2020-09-01 12:08:40 -04:00
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [tag] Version:[${MAJOR_TAG}]Additonal location Dockerfile!"
else
# SUCCESS
info "Successfull [tag] Version:[${MAJOR_TAG}] of additonal image!"
fi
2020-08-24 14:00:34 -04:00
fi
2020-08-20 13:58:11 -04:00
}
################################################################################
2019-11-08 09:31:16 -05:00
#### Function UploadImage ######################################################
2020-07-01 17:40:40 -04:00
UploadImage() {
2019-11-08 09:31:16 -05:00
################
# Print header #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Uploading the DockerFile image to ${REGISTRY}..."
info "----------------------------------------------"
2019-11-08 09:31:16 -05:00
############################################
# Upload the docker image that was created #
############################################
2020-08-24 14:00:34 -04:00
docker push "${CONTAINER_URL}:${IMAGE_VERSION}" 2>&1
2019-11-08 09:31:16 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2019-11-08 09:31:16 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
fatal "failed to [upload] Dockerfile!"
2019-11-08 09:31:16 -05:00
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully Uploaded Docker image:${F[W]}[${IMAGE_VERSION}]${F[B]} to ${F[C]}${REGISTRY}${F[B]}!"
2019-11-08 09:31:16 -05:00
fi
#########################
# Get Image information #
#########################
2019-11-08 10:00:07 -05:00
IFS=$'\n' # Set the delimit to newline
2020-08-24 14:00:34 -04:00
GET_INFO_CMD=$(docker images | grep "${CONTAINER_URL}" | grep "${IMAGE_VERSION}" 2>&1)
2019-11-08 09:31:16 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2019-11-08 09:31:16 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
error "Failed to get information about built Image!"
fatal "[${GET_INFO_CMD}]"
2019-11-08 09:31:16 -05:00
else
################
# Get the data #
################
2020-09-01 13:57:10 -04:00
REPO=$(echo "${GET_INFO_CMD}" | awk '{print $1}')
TAG=$(echo "${GET_INFO_CMD}" | awk '{print $2}')
IMAGE_ID=$(echo "${GET_INFO_CMD}" | awk '{print $3}')
2020-06-22 09:59:31 -04:00
SIZE="${GET_INFO_CMD##* }"
2019-11-08 09:31:16 -05:00
###################
# Print the goods #
###################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Docker Image Details:"
info "Repository:[${REPO}]"
info "Tag:[${TAG}]"
info "Image_ID:[${IMAGE_ID}]"
info "Size:[${SIZE}]"
info "----------------------------------------------"
2019-11-08 09:31:16 -05:00
fi
###############################################################
# Check if we need to upload the major tagged version as well #
###############################################################
2020-07-21 13:09:07 -04:00
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
############################################
# Upload the docker image that was created #
############################################
2020-08-24 14:00:34 -04:00
docker push "${CONTAINER_URL}:${MAJOR_TAG}" 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
2020-07-27 17:35:53 -04:00
fatal "failed to [upload] MAJOR_TAG:[${MAJOR_TAG}] Dockerfile!"
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully Uploaded TAG:${F[W]}[${MAJOR_TAG}]${F[B]} of Docker image to ${F[C]}${REGISTRY}${F[B]}!"
fi
fi
2019-11-08 09:31:16 -05:00
}
################################################################################
2020-08-20 13:58:11 -04:00
#### Function FindBuiltImage ###################################################
FindBuiltImage() {
# Check the local system to see if an image has already been built
# if so, we only need to update tags and push
# Set FOUND_IMAGE=1 when found
2020-08-20 14:18:17 -04:00
##############
# Local vars #
##############
2020-08-24 14:00:34 -04:00
CHECK_IMAGE_REPO='' # Repo to look for
####################################
# Set the additional container URL #
####################################
if [[ ${REGISTRY} == "GCR" ]]; then
CHECK_IMAGE_REPO="${GCR_IMAGE_REPO}"
elif [[ ${REGISTRY} == "Docker" ]]; then
CHECK_IMAGE_REPO="${DOCKER_IMAGE_REPO}"
fi
2020-08-20 13:58:11 -04:00
#######################################
# Look for Release image in DockerHub #
#######################################
2020-08-24 14:00:34 -04:00
FIND_VERSION_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
2020-08-20 13:58:11 -04:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
2020-08-24 14:00:34 -04:00
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${IMAGE_VERSION}] already built on instance"
2020-08-20 14:18:17 -04:00
# Increment flag
2020-08-24 14:00:34 -04:00
FOUND_RELASE=1
2020-08-20 13:58:11 -04:00
else
2020-08-24 14:00:34 -04:00
info "Failed to find locally created Docker image:[${CHECK_IMAGE_REPO}]"
2020-08-24 14:05:33 -04:00
info "${FIND_VERSION_CMD}"
2020-08-20 13:58:11 -04:00
fi
#####################################
# Look for Major image in DockerHub #
#####################################
2020-08-24 14:00:34 -04:00
FIND_MAJOR_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1)
2020-08-20 13:58:11 -04:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
2020-08-24 14:00:34 -04:00
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${MAJOR_TAG}] already built on instance"
2020-08-20 14:18:17 -04:00
# Increment flag
2020-08-24 14:00:34 -04:00
FOUND_MAJOR=1
2020-08-20 13:58:11 -04:00
else
2020-08-24 14:00:34 -04:00
info "Failed to find locally created Docker image:[${FIND_MAJOR_CMD}]"
2020-08-24 14:05:33 -04:00
info "${FIND_MAJOR_CMD}"
2020-08-20 13:58:11 -04:00
fi
2020-08-24 14:00:34 -04:00
###############################
# Check if we found the image #
###############################
if [ ${FOUND_MAJOR} -eq 1 ] && [ ${FOUND_RELASE} -eq 1 ]; then
FOUND_IMAGE=1
2020-08-20 13:58:11 -04:00
fi
}
################################################################################
2019-11-08 09:31:16 -05:00
#### Function Footer ###########################################################
2020-07-01 17:40:40 -04:00
Footer() {
2020-07-30 15:15:42 -04:00
info "-------------------------------------------------------"
info "The step has completed"
info "-------------------------------------------------------"
2019-11-08 09:31:16 -05:00
}
################################################################################
################################## MAIN ########################################
################################################################################
##########
# Header #
##########
Header
##################
# Validate Input #
##################
ValidateInput
2020-08-20 13:58:11 -04:00
###############################
# Find Image if already built #
###############################
FindBuiltImage
2019-11-08 09:31:16 -05:00
###################
# Build the image #
###################
2020-08-20 13:58:11 -04:00
if [ "$FOUND_IMAGE" -ne 0 ]; then
BuildImage
fi
2019-11-08 09:31:16 -05:00
######################
# Login to DockerHub #
######################
2020-07-21 13:09:07 -04:00
if [[ ${REGISTRY} == "Docker" ]]; then
# Authenticate "Username" "Password" "Url" "Name"
2020-07-21 13:09:07 -04:00
Authenticate "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}" "" "Dockerhub"
2020-08-20 14:18:17 -04:00
######################################
# Login to GitHub Container Registry #
######################################
2020-08-20 13:58:11 -04:00
elif [[ ${REGISTRY} == "GCR" ]]; then
# Authenticate "Username" "Password" "Url" "Name"
2020-08-20 14:18:17 -04:00
Authenticate "${GCR_USERNAME}" "${GCR_TOKEN}" "https://${GCR_URL}" "GitHub Container Registry"
else
#########
# ERROR #
#########
2020-07-27 17:35:53 -04:00
error "Registry not set correctly!"
2020-07-27 17:20:06 -04:00
fatal "Registry:[${REGISTRY}]"
fi
2019-11-08 09:31:16 -05:00
####################
# Upload the image #
####################
UploadImage
##########
# Footer #
##########
Footer