2020-06-23 12:02:45 -04:00
|
|
|
#!/usr/bin/env bash
|
2020-01-09 14:04:41 -05:00
|
|
|
|
|
|
|
################################################################################
|
2020-06-18 15:42:33 -04:00
|
|
|
############# Cleanup Image on DockerHub @admiralawkbar ########################
|
2020-01-09 14:04:41 -05:00
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# NOTES: This script is used to remove a tagged image on DockerHub
|
|
|
|
# Its based on being built from a GitHub Action, but could be easily updated
|
|
|
|
# To be ran in a different medium.
|
|
|
|
#
|
2020-06-21 19:41:24 -04:00
|
|
|
# PRE-Requirements:
|
2020-01-09 14:04:41 -05:00
|
|
|
# - Dockerfile
|
|
|
|
# - System with Docker installed
|
|
|
|
# - Global variables met
|
|
|
|
|
|
|
|
###########
|
|
|
|
# Globals #
|
|
|
|
###########
|
2020-07-01 17:40:40 -04:00
|
|
|
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace
|
|
|
|
DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub
|
|
|
|
DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub
|
|
|
|
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
|
2020-01-09 14:04:41 -05:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
############################ FUNCTIONS BELOW ###################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
#### Function Header ###########################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
Header() {
|
2020-01-09 14:04:41 -05:00
|
|
|
echo ""
|
|
|
|
echo "-------------------------------------------------------"
|
2020-06-19 01:44:26 -04:00
|
|
|
echo "----- GitHub Actions remove image from DockerHub ------"
|
2020-01-09 14:04:41 -05:00
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function ValidateInput ####################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
ValidateInput() {
|
2020-01-09 14:04:41 -05:00
|
|
|
# Need to validate we have the basic variables
|
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Gathering variables..."
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
############################
|
|
|
|
# Validate GITHUB_WORKSPACE #
|
|
|
|
############################
|
|
|
|
if [ -z "$GITHUB_WORKSPACE" ]; then
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [GITHUB_WORKSPACE]!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$GITHUB_WORKSPACE]${NC}"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[GITHUB_WORKSPACE], value:[$GITHUB_WORKSPACE]"
|
|
|
|
fi
|
|
|
|
|
2020-06-18 13:58:57 -04:00
|
|
|
#######################
|
|
|
|
# Validate IMAGE_REPO #
|
|
|
|
#######################
|
|
|
|
if [ -z "$IMAGE_REPO" ]; then
|
|
|
|
# No repo was pulled
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [IMAGE_REPO]!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$IMAGE_REPO]${NC}"
|
2020-06-18 13:58:57 -04:00
|
|
|
exit 1
|
2020-07-01 17:40:40 -04:00
|
|
|
elif [[ $IMAGE_REPO == "github/super-linter" ]]; then
|
2020-06-18 13:58:57 -04:00
|
|
|
# Found our main repo
|
|
|
|
echo "Successfully found:[IMAGE_REPO], value:[$IMAGE_REPO]"
|
|
|
|
else
|
|
|
|
# This is a fork and we cant pull vars or any info
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${F[Y]}WARN!${NC} No image to cleanup as this is a forked branch, and not being built with current automation!${NC}"
|
2020-06-18 13:58:57 -04:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Validate IMAGE_VERSION #
|
|
|
|
##########################
|
|
|
|
if [ -z "$IMAGE_VERSION" ]; then
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [IMAGE_VERSION]!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$IMAGE_VERSION]${NC}"
|
2020-06-18 13:58:57 -04:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]"
|
|
|
|
fi
|
|
|
|
|
2020-01-09 14:04:41 -05:00
|
|
|
############################
|
|
|
|
# Validate DOCKER_USERNAME #
|
|
|
|
############################
|
|
|
|
if [ -z "$DOCKER_USERNAME" ]; then
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [DOCKER_USERNAME]!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$DOCKER_USERNAME]${NC}"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[DOCKER_USERNAME], value:[$DOCKER_USERNAME]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
############################
|
|
|
|
# Validate DOCKER_PASSWORD #
|
|
|
|
############################
|
|
|
|
if [ -z "$DOCKER_PASSWORD" ]; then
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [DOCKER_PASSWORD]!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$DOCKER_PASSWORD]${NC}"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Successfully found:[DOCKER_PASSWORD], value:[********]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Check if we need to get the name of the branch #
|
|
|
|
##################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
if [[ $IMAGE_VERSION != "latest" ]]; then
|
2020-06-20 23:56:56 -04:00
|
|
|
##################################
|
|
|
|
# Remove non alpha-numeric chars #
|
|
|
|
##################################
|
2020-01-09 15:10:43 -05:00
|
|
|
IMAGE_VERSION=$(echo "$IMAGE_VERSION" | tr -cd '[:alnum:]')
|
2020-01-09 14:04:41 -05:00
|
|
|
else
|
|
|
|
#############################################
|
|
|
|
# Image is 'latest' and we will not destroy #
|
|
|
|
#############################################
|
|
|
|
echo "Image Tag is set to:[latest]..."
|
|
|
|
echo "We will never destroy latest..."
|
|
|
|
echo "Bye!"
|
2020-01-09 15:10:58 -05:00
|
|
|
exit 1
|
2020-01-09 14:04:41 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function LoginToDocker ####################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
LoginToDocker() {
|
2020-01-09 14:04:41 -05:00
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Login to DockerHub..."
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Login to DockerHub #
|
|
|
|
######################
|
|
|
|
LOGIN_CMD=$(docker login --username "$DOCKER_USERNAME" --password "$DOCKER_PASSWORD" 2>&1)
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the error code #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to authenticate to DockerHub!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$LOGIN_CMD]${NC}"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
|
|
|
echo "Successfully authenticated to DockerHub!"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function RemoveImage ######################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
RemoveImage() {
|
2020-01-09 14:04:41 -05:00
|
|
|
################
|
|
|
|
# Print header #
|
|
|
|
################
|
|
|
|
echo ""
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo "Removing the DockerFile image:[$IMAGE_REPO:$IMAGE_VERSION]"
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
# Create Token to auth to DockerHub #
|
|
|
|
#####################################
|
|
|
|
TOKEN=$(curl -s -k \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-X POST \
|
2020-01-09 14:19:21 -05:00
|
|
|
-d "{\"username\": \"$DOCKER_USERNAME\", \"password\": \"$DOCKER_PASSWORD\"}" \
|
2020-01-09 14:04:41 -05:00
|
|
|
"https://hub.docker.com/v2/users/login/" | jq -r .token 2>&1)
|
|
|
|
|
2020-07-01 17:40:40 -04:00
|
|
|
#######################
|
|
|
|
# Load the ERROR_CODE #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
2020-01-09 14:04:41 -05:00
|
|
|
|
2020-07-01 17:40:40 -04:00
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to gain token from DockerHub!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$TOKEN]${NC}"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
|
|
|
echo "Successfully gained auth token from DockerHub!"
|
|
|
|
fi
|
2020-01-09 14:04:41 -05:00
|
|
|
|
|
|
|
#################################
|
|
|
|
# Remove the tag from DockerHub #
|
|
|
|
#################################
|
|
|
|
REMOVE_CMD=$(curl "https://hub.docker.com/v2/repositories/$IMAGE_REPO/tags/$IMAGE_VERSION/" \
|
|
|
|
-X DELETE \
|
|
|
|
-H "Authorization: JWT $TOKEN" 2>&1)
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the ERROR_CODE #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
2020-07-01 16:47:24 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to remove tag from DockerHub!${NC}"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$REMOVE_CMD]${NC}"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
2020-01-09 14:19:21 -05:00
|
|
|
echo "Successfully [removed] Docker image tag:[$IMAGE_VERSION] from DockerHub!"
|
2020-01-09 14:04:41 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function Footer ###########################################################
|
2020-07-01 17:40:40 -04:00
|
|
|
Footer() {
|
2020-01-09 14:04:41 -05:00
|
|
|
echo ""
|
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo "The step has completed"
|
|
|
|
echo "-------------------------------------------------------"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
################################## MAIN ########################################
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
##########
|
|
|
|
# Header #
|
|
|
|
##########
|
|
|
|
Header
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Validate Input #
|
|
|
|
##################
|
|
|
|
ValidateInput
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Login to DockerHub #
|
|
|
|
######################
|
|
|
|
LoginToDocker
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Remove the image #
|
|
|
|
####################
|
|
|
|
RemoveImage
|
|
|
|
|
|
|
|
##########
|
|
|
|
# Footer #
|
|
|
|
##########
|
|
|
|
Footer
|