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-06-19 01:44:26 -04:00
|
|
|
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace
|
2020-01-09 14:04:41 -05:00
|
|
|
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
|
2020-01-09 15:06:13 -05:00
|
|
|
IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image
|
2020-01-09 14:04:41 -05:00
|
|
|
DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
############################ FUNCTIONS BELOW ###################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
#### Function Header ###########################################################
|
|
|
|
Header()
|
|
|
|
{
|
|
|
|
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 ####################################################
|
|
|
|
ValidateInput()
|
|
|
|
{
|
|
|
|
# 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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [GITHUB_WORKSPACE]!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$GITHUB_WORKSPACE]"
|
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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [IMAGE_REPO]!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$IMAGE_REPO]"
|
2020-06-18 13:58:57 -04:00
|
|
|
exit 1
|
|
|
|
elif [[ "$IMAGE_REPO" == "github/super-linter" ]]; then
|
|
|
|
# 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
|
|
|
|
echo "WARN! No image to cleanup as this is a forked branch, and not being built with current automation!"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Validate IMAGE_VERSION #
|
|
|
|
##########################
|
|
|
|
if [ -z "$IMAGE_VERSION" ]; then
|
2020-07-01 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [IMAGE_VERSION]!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$IMAGE_VERSION]"
|
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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [DOCKER_USERNAME]!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$DOCKER_USERNAME]"
|
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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to get [DOCKER_PASSWORD]!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$DOCKER_PASSWORD]"
|
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 #
|
|
|
|
##################################################
|
|
|
|
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 ####################################################
|
|
|
|
LoginToDocker()
|
|
|
|
{
|
|
|
|
################
|
|
|
|
# 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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to authenticate to DockerHub!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$LOGIN_CMD]"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
|
|
|
echo "Successfully authenticated to DockerHub!"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
################################################################################
|
|
|
|
#### Function RemoveImage ######################################################
|
|
|
|
RemoveImage()
|
|
|
|
{
|
|
|
|
################
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
#######################
|
|
|
|
# Load the ERROR_CODE #
|
|
|
|
#######################
|
|
|
|
ERROR_CODE=$?
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Check the shell for errors #
|
|
|
|
##############################
|
|
|
|
if [ $ERROR_CODE -ne 0 ]; then
|
|
|
|
# ERROR
|
2020-07-01 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to gain token from DockerHub!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$TOKEN]"
|
2020-01-09 14:04:41 -05:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
# SUCCESS
|
|
|
|
echo "Successfully gained auth token from DockerHub!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
#################################
|
|
|
|
# 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 15:00:05 -04:00
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR!${NC} Failed to remove tag from DockerHub!"
|
|
|
|
echo -e "${NC}${B[R]}${F[W]}ERROR:${NC}[$REMOVE_CMD]"
|
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 ###########################################################
|
|
|
|
Footer()
|
|
|
|
{
|
|
|
|
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
|