superlint/.automation/cleanup-docker.sh

253 lines
8.1 KiB
Bash
Raw Normal View History

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.
#
# 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
#########################
# Source Function Files #
#########################
# shellcheck source=/dev/null
source "${GITHUB_WORKSPACE}/lib/log.sh" # Source the function script(s)
2020-01-09 14:04:41 -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 remove image from DockerHub ------"
info "-------------------------------------------------------"
2020-01-09 14:04:41 -05:00
}
################################################################################
#### 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 #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Gathering variables..."
info "----------------------------------------------"
2020-01-09 14:04:41 -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}]"
2020-01-09 14:04:41 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:[GITHUB_WORKSPACE], value:[${GITHUB_WORKSPACE}]"
2020-01-09 14:04:41 -05:00
fi
2020-06-18 13:58:57 -04:00
#######################
# Validate IMAGE_REPO #
#######################
2020-07-21 13:09:07 -04:00
if [ -z "${IMAGE_REPO}" ]; then
2020-06-18 13:58:57 -04:00
# No repo was pulled
2020-07-27 17:35:53 -04:00
error "Failed to get [IMAGE_REPO]!"
fatal "[${IMAGE_REPO}]"
2020-07-21 13:09:07 -04:00
elif [[ ${IMAGE_REPO} == "github/super-linter" ]]; then
2020-06-18 13:58:57 -04:00
# Found our main repo
2020-07-30 15:15:42 -04:00
info "Successfully found:[IMAGE_REPO], value:[${IMAGE_REPO}]"
2020-06-18 13:58:57 -04:00
else
# This is a fork and we cant pull vars or any info
2020-07-30 15:15:42 -04:00
warn "No image to cleanup as this is a forked branch, and not being built with current automation!"
2020-06-18 13:58:57 -04:00
exit 0
fi
##########################
# Validate IMAGE_VERSION #
##########################
2020-07-21 13:09:07 -04:00
if [ -z "${IMAGE_VERSION}" ]; then
2020-07-27 17:35:53 -04:00
error "Failed to get [IMAGE_VERSION]!"
fatal "[${IMAGE_VERSION}]"
2020-06-18 13:58:57 -04:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:[IMAGE_VERSION], value:[${IMAGE_VERSION}]"
2020-06-18 13:58:57 -04:00
fi
2020-01-09 14:04:41 -05:00
############################
# 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}]"
2020-01-09 14:04:41 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:[DOCKER_USERNAME], value:[${DOCKER_USERNAME}]"
2020-01-09 14:04:41 -05:00
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}]"
2020-01-09 14:04:41 -05:00
else
2020-07-30 15:15:42 -04:00
info "Successfully found:[DOCKER_PASSWORD], value:[********]"
2020-01-09 14:04:41 -05:00
fi
##################################################
# Check if we need to get the name of the branch #
##################################################
2020-07-21 13:09:07 -04:00
if [[ ${IMAGE_VERSION} != "latest" ]]; then
2020-06-20 23:56:56 -04:00
##################################
# Remove non alpha-numeric chars #
##################################
2020-07-21 13:09:07 -04: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 #
#############################################
2020-07-27 17:20:06 -04:00
error "Image Tag is set to:[latest]..."
error "We will never destroy latest..."
fatal "Bye!"
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 #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Login to DockerHub..."
info "----------------------------------------------"
2020-01-09 14:04:41 -05:00
######################
# Login to DockerHub #
######################
2020-07-21 13:09:07 -04:00
LOGIN_CMD=$(docker login --username "${DOCKER_USERNAME}" --password "${DOCKER_PASSWORD}" 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-01-09 14:04:41 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
error "Failed to authenticate to DockerHub!"
fatal "[${LOGIN_CMD}]"
2020-01-09 14:04:41 -05:00
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully authenticated to DockerHub!"
2020-01-09 14:04:41 -05:00
fi
}
################################################################################
#### Function RemoveImage ######################################################
2020-07-01 17:40:40 -04:00
RemoveImage() {
2020-01-09 14:04:41 -05:00
################
# Print header #
################
2020-07-30 15:15:42 -04:00
info "----------------------------------------------"
info "Removing the DockerFile image:[${IMAGE_REPO}:${IMAGE_VERSION}]"
info "----------------------------------------------"
2020-01-09 14:04:41 -05:00
#####################################
# Create Token to auth to DockerHub #
#####################################
TOKEN=$(curl -s -k \
-H "Content-Type: application/json" \
-X POST \
2020-07-21 13:09:07 -04: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 #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-07-01 17:40:40 -04:00
# ERROR
2020-07-27 17:35:53 -04:00
error "Failed to gain token from DockerHub!"
fatal "[${TOKEN}]"
2020-07-01 17:40:40 -04:00
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "Successfully gained auth token from DockerHub!"
2020-07-01 17:40:40 -04:00
fi
2020-01-09 14:04:41 -05:00
#################################
# Remove the tag from DockerHub #
#################################
2020-07-21 13:09:07 -04:00
REMOVE_CMD=$(curl "https://hub.docker.com/v2/repositories/${IMAGE_REPO}/tags/${IMAGE_VERSION}/" \
2020-01-09 14:04:41 -05:00
-X DELETE \
2020-07-21 13:09:07 -04:00
-H "Authorization: JWT ${TOKEN}" 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-01-09 14:04:41 -05:00
# ERROR
2020-07-27 17:35:53 -04:00
error "Failed to remove tag from DockerHub!"
fatal "[${REMOVE_CMD}]"
2020-01-09 14:04:41 -05:00
else
# SUCCESS
2020-07-30 15:15:42 -04:00
info "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-07-30 15:15:42 -04:00
info "-------------------------------------------------------"
info "The step has completed"
info "-------------------------------------------------------"
2020-01-09 14:04:41 -05:00
}
################################################################################
################################## MAIN ########################################
################################################################################
##########
# Header #
##########
Header
##################
# Validate Input #
##################
ValidateInput
######################
# Login to DockerHub #
######################
LoginToDocker
####################
# Remove the image #
####################
RemoveImage
##########
# Footer #
##########
Footer