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-Reqs:
|
|
|
|
# - Dockerfile
|
|
|
|
# - System with Docker installed
|
|
|
|
# - Global variables met
|
|
|
|
|
|
|
|
###########
|
|
|
|
# Globals #
|
|
|
|
###########
|
2020-06-19 01:44:26 -04:00
|
|
|
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace
|
2019-11-08 09:31:16 -05:00
|
|
|
DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub
|
|
|
|
DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub
|
2020-06-23 13:06:12 -04:00
|
|
|
GPR_USERNAME="${GPR_USERNAME}" # Username to login to GitHub package registry
|
|
|
|
GPR_TOKEN="${GPR_TOKEN}" # Password to login to GitHub package registry
|
|
|
|
REGISTRY="${REGISTRY}" # What registry to upload | <GPR> or <Docker>
|
2019-11-08 09:31:16 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
############################ FUNCTIONS BELOW ###################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
#### Function Header ###########################################################
|
|
|
|
Header()
|
|
|
|
{
|
|
|
|
echo ""
|
|
|
|
echo "-------------------------------------------------------"
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "---- GitHub Actions Upload image to [$REGISTRY] ----"
|
2019-11-08 09:31:16 -05:00
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function ValidateInput ####################################################
|
|
|
|
ValidateInput()
|
|
|
|
{
|
|
|
|
# Need to validate we have the basic variables
|
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Gathering variables..."
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
2020-06-23 13:06:12 -04:00
|
|
|
#############################
|
2019-11-08 09:31:16 -05:00
|
|
|
# Validate GITHUB_WORKSPACE #
|
2020-06-23 13:06:12 -04:00
|
|
|
#############################
|
2019-11-08 09:31:16 -05:00
|
|
|
if [ -z "$GITHUB_WORKSPACE" ]; then
|
|
|
|
echo "ERROR! Failed to get [GITHUB_WORKSPACE]!"
|
|
|
|
echo "ERROR:[$GITHUB_WORKSPACE]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[GITHUB_WORKSPACE], value:[$GITHUB_WORKSPACE]"
|
|
|
|
fi
|
|
|
|
|
2020-06-23 13:06:12 -04:00
|
|
|
#####################
|
|
|
|
# Validate REGISTRY #
|
|
|
|
#####################
|
|
|
|
if [ -z "$REGISTRY" ]; then
|
|
|
|
echo "ERROR! Failed to get [REGISTRY]!"
|
|
|
|
echo "ERROR:[$REGISTRY]"
|
2019-11-08 09:31:16 -05:00
|
|
|
exit 1
|
|
|
|
else
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "Successfully found:[REGISTRY], value:[$REGISTRY]"
|
2019-11-08 09:31:16 -05:00
|
|
|
fi
|
|
|
|
|
2020-06-23 13:06:12 -04:00
|
|
|
#####################################################
|
|
|
|
# See if we need values for GitHub package Registry #
|
|
|
|
#####################################################
|
|
|
|
if [[ "$REGISTRY" == "GPR" ]]; then
|
|
|
|
#########################
|
|
|
|
# Validate GPR_USERNAME #
|
|
|
|
#########################
|
|
|
|
if [ -z "$GPR_USERNAME" ]; then
|
|
|
|
echo "ERROR! Failed to get [GPR_USERNAME]!"
|
|
|
|
echo "ERROR:[$GPR_USERNAME]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[GPR_USERNAME], value:[$GPR_USERNAME]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Validate GPR_TOKEN #
|
|
|
|
######################
|
|
|
|
if [ -z "$GPR_TOKEN" ]; then
|
|
|
|
echo "ERROR! Failed to get [GPR_TOKEN]!"
|
|
|
|
echo "ERROR:[$GPR_TOKEN]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[GPR_TOKEN], value:[********]"
|
|
|
|
fi
|
|
|
|
########################################
|
|
|
|
# See if we need values for Ducker hub #
|
|
|
|
########################################
|
|
|
|
elif [[ "$REGISTRY" == "Docker" ]]; then
|
|
|
|
############################
|
|
|
|
# Validate DOCKER_USERNAME #
|
|
|
|
############################
|
|
|
|
if [ -z "$DOCKER_USERNAME" ]; then
|
|
|
|
echo "ERROR! Failed to get [DOCKER_USERNAME]!"
|
|
|
|
echo "ERROR:[$DOCKER_USERNAME]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[DOCKER_USERNAME], value:[$DOCKER_USERNAME]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
############################
|
|
|
|
# Validate DOCKER_PASSWORD #
|
|
|
|
############################
|
|
|
|
if [ -z "$DOCKER_PASSWORD" ]; then
|
|
|
|
echo "ERROR! Failed to get [DOCKER_PASSWORD]!"
|
|
|
|
echo "ERROR:[$DOCKER_PASSWORD]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[DOCKER_PASSWORD], value:[********]"
|
|
|
|
fi
|
|
|
|
###########################################
|
|
|
|
# We were not passed a registry to update #
|
|
|
|
###########################################
|
2019-11-08 09:31:16 -05:00
|
|
|
else
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "ERROR! Failed to find a valid registry!"
|
|
|
|
echo "Registry:[$REGISTRY]"
|
|
|
|
exit 1
|
2019-11-08 09:31:16 -05:00
|
|
|
fi
|
|
|
|
|
2020-06-23 13:06:12 -04:00
|
|
|
|
2019-11-08 09:31:16 -05:00
|
|
|
#######################
|
|
|
|
# Validate IMAGE_REPO #
|
|
|
|
#######################
|
|
|
|
if [ -z "$IMAGE_REPO" ]; then
|
|
|
|
echo "ERROR! Failed to get [IMAGE_REPO]!"
|
|
|
|
echo "ERROR:[$IMAGE_REPO]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[IMAGE_REPO], value:[$IMAGE_REPO]"
|
2020-06-23 13:06:12 -04:00
|
|
|
###############################################
|
|
|
|
# Need to see if GPR registry and update name #
|
|
|
|
###############################################
|
|
|
|
if [[ "$REGISTRY" == "GPR" ]]; then
|
|
|
|
NAME="docker.pkg.github/$IMAGE_REPO"
|
|
|
|
IMAGE_REPO="$NAME"
|
|
|
|
echo "Updated [IMAGE_REPO] to:[$IMAGE_REPO] for GPR"
|
|
|
|
fi
|
2019-11-08 09:31:16 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Validate IMAGE_VERSION #
|
|
|
|
##########################
|
|
|
|
if [ -z "$IMAGE_VERSION" ]; then
|
2020-01-09 14:10:40 -05:00
|
|
|
echo "WARN! Failed to get [IMAGE_VERSION]!"
|
|
|
|
echo "Pulling from Branch Name..."
|
2020-01-09 14:04:41 -05:00
|
|
|
##############################
|
|
|
|
# Get the name of the branch #
|
|
|
|
##############################
|
|
|
|
BRANCH_NAME=$(git branch --contains "$GITHUB_SHA" |awk '{print $2}' 2>&1)
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
echo "ERROR! Failed to get branch name!"
|
|
|
|
echo "ERROR:[$BRANCH_NAME]"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-06-20 23:56:56 -04:00
|
|
|
##################################
|
|
|
|
# Remove non alpha-numeric chars #
|
|
|
|
##################################
|
2020-01-09 14:04:41 -05:00
|
|
|
BRANCH_NAME=$(echo "$BRANCH_NAME" | tr -cd '[:alnum:]')
|
|
|
|
|
|
|
|
############################################
|
|
|
|
# Set the IMAGE_VERSION to the BRANCH_NAME #
|
|
|
|
############################################
|
|
|
|
IMAGE_VERSION="$BRANCH_NAME"
|
2020-01-09 14:19:21 -05:00
|
|
|
echo "Tag:[$IMAGE_VERSION]"
|
2020-01-09 14:10:40 -05:00
|
|
|
else
|
|
|
|
echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]"
|
2020-01-09 14:04:41 -05:00
|
|
|
fi
|
|
|
|
|
2019-11-08 09:31:16 -05:00
|
|
|
############################
|
|
|
|
# Validate DOCKERFILE_PATH #
|
|
|
|
############################
|
|
|
|
if [ -z "$DOCKERFILE_PATH" ]; then
|
|
|
|
echo "ERROR! Failed to get [DOCKERFILE_PATH]!"
|
|
|
|
echo "ERROR:[$DOCKERFILE_PATH]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[DOCKERFILE_PATH], value:[$DOCKERFILE_PATH]"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
2020-06-23 13:06:12 -04:00
|
|
|
#### Function Authenticate #####################################################
|
|
|
|
Authenticate()
|
2019-11-08 09:31:16 -05:00
|
|
|
{
|
2020-06-23 13:06:12 -04:00
|
|
|
################
|
|
|
|
# Pull in Vars #
|
|
|
|
################
|
2020-06-23 13:10:36 -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
|
2020-06-23 13:06:12 -04:00
|
|
|
|
2019-11-08 09:31:16 -05:00
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "Login to $NAME..."
|
2019-11-08 09:31:16 -05:00
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
2020-06-23 13:06:12 -04:00
|
|
|
###################
|
|
|
|
# Auth to service #
|
|
|
|
###################
|
|
|
|
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 #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "ERROR! Failed to authenticate to $NAME!"
|
2019-11-08 09:31:16 -05:00
|
|
|
echo "ERROR:[$LOGIN_CMD]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "Successfully authenticated to $NAME!"
|
2019-11-08 09:31:16 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function BuildImage #######################################################
|
|
|
|
BuildImage()
|
|
|
|
{
|
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Building the DockerFile image..."
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
################################
|
|
|
|
# Validate the DOCKERFILE_PATH #
|
|
|
|
################################
|
|
|
|
if [ ! -f "$DOCKERFILE_PATH" ]; then
|
|
|
|
# No file found
|
|
|
|
echo "ERROR! failed to find Dockerfile at:[$DOCKERFILE_PATH]"
|
|
|
|
echo "Please make sure you give full path!"
|
|
|
|
echo "Example:[/configs/Dockerfile] or [Dockerfile] if at root directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
###################
|
|
|
|
# Build the image #
|
|
|
|
###################
|
2020-01-09 14:04:41 -05:00
|
|
|
docker build --no-cache -t "$IMAGE_REPO:$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 #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
|
|
|
echo "ERROR! failed to [build] Dockerfile!"
|
|
|
|
exit 1
|
2019-11-08 10:00:07 -05:00
|
|
|
else
|
|
|
|
# SUCCESS
|
|
|
|
echo "Successfully Built image!"
|
|
|
|
echo "Info:[$BUILD_CMD]"
|
2019-11-08 09:31:16 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function UploadImage ######################################################
|
|
|
|
UploadImage()
|
|
|
|
{
|
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "Uploading the DockerFile image to $REGISTRY..."
|
2019-11-08 09:31:16 -05:00
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
############################################
|
|
|
|
# Upload the docker image that was created #
|
|
|
|
############################################
|
2020-01-09 14:04:41 -05:00
|
|
|
docker push "$IMAGE_REPO:$IMAGE_VERSION" 2>&1
|
2019-11-08 09:31:16 -05:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
2020-01-09 14:04:41 -05:00
|
|
|
echo "ERROR! failed to [upload] Dockerfile!"
|
2019-11-08 09:31:16 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
2020-06-23 13:06:12 -04:00
|
|
|
echo "Successfully Uploaded Docker image to $REGISTRY!"
|
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
|
2019-11-08 09:31:16 -05:00
|
|
|
GET_INFO_CMD=$(docker images | grep "$IMAGE_REPO" | grep "$IMAGE_VERSION" 2>&1)
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
|
|
|
echo "ERROR! Failed to get information about built Image!"
|
|
|
|
echo "ERROR:[$GET_INFO_CMD]"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
################
|
|
|
|
# Get the data #
|
|
|
|
################
|
2019-11-08 10:07:53 -05: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}')
|
2019-11-08 09:37:14 -05:00
|
|
|
# shellcheck disable=SC2116
|
2019-11-08 09:31:16 -05:00
|
|
|
SIZE=$(echo "${GET_INFO_CMD##* }")
|
|
|
|
|
|
|
|
###################
|
|
|
|
# Print the goods #
|
|
|
|
###################
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Docker Image Details:"
|
|
|
|
echo "Repository:[$REPO]"
|
|
|
|
echo "Tag:[$TAG]"
|
|
|
|
echo "Image_ID:[$IMAGE_ID]"
|
|
|
|
echo "Size:[$SIZE]"
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function Footer ###########################################################
|
|
|
|
Footer()
|
|
|
|
{
|
|
|
|
echo ""
|
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo "The step has completed"
|
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
################################## MAIN ########################################
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
##########
|
|
|
|
# Header #
|
|
|
|
##########
|
|
|
|
Header
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Validate Input #
|
|
|
|
##################
|
|
|
|
ValidateInput
|
|
|
|
|
2020-06-23 13:19:37 -04:00
|
|
|
###################
|
|
|
|
# Build the image #
|
|
|
|
###################
|
|
|
|
BuildImage
|
|
|
|
|
2019-11-08 09:31:16 -05:00
|
|
|
######################
|
|
|
|
# Login to DockerHub #
|
|
|
|
######################
|
2020-06-23 13:06:12 -04:00
|
|
|
if [[ "$REGISTRY" == "Docker" ]]; then
|
|
|
|
# Authenticate "Username" "Password" "Url" "Name"
|
2020-06-23 13:26:28 -04:00
|
|
|
Authenticate "$DOCKER_USERNAME" "$DOCKER_PASSWORD" "" "Dockerhub"
|
2020-06-23 13:06:12 -04:00
|
|
|
|
|
|
|
####################################
|
|
|
|
# Login to GitHub Package Registry #
|
|
|
|
####################################
|
|
|
|
elif [[ "$REGISTRY" == "GPR" ]]; then
|
|
|
|
# Authenticate "Username" "Password" "Url" "Name"
|
|
|
|
Authenticate "$GPR_USERNAME" "$GPR_TOKEN" "https://docker.pkg.github.com" "GitHub Package Registry"
|
|
|
|
|
|
|
|
else
|
|
|
|
#########
|
|
|
|
# ERROR #
|
|
|
|
#########
|
|
|
|
echo "ERROR! Registry not set correctly!"
|
|
|
|
echo "Registry:[$REGISTRY]"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-11-08 09:31:16 -05:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Upload the image #
|
|
|
|
####################
|
|
|
|
UploadImage
|
|
|
|
|
|
|
|
##########
|
|
|
|
# Footer #
|
|
|
|
##########
|
|
|
|
Footer
|