forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushimage.sh
executable file
·130 lines (107 loc) · 2.58 KB
/
pushimage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
set +e
set -o noglob
if [ "$1" == "" ];then
echo "This shell will push specific image to registry server."
echo "Usage: #./pushimage [image tag] [registry username] [registry password] [registry server]"
exit 1
fi
#
# Set Colors
#
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
#
# Headers and Logging
#
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
type_exists() {
if [ $(type -P $1) ]; then
return 0
fi
return 1
}
# Check variables
if [ -z $1 ]; then
error "Please set the 'image' variable"
exit 1
fi
if [ -z $2 ]; then
error "Please set the 'username' variable"
exit 1
fi
if [ -z $3 ]; then
error "Please set the 'password' variable"
exit 1
fi
if [ -z $4 ]; then
info "Using default registry server (dockerhub)."
fi
# Check Docker is installed
if ! type_exists 'docker'; then
error "Docker is not installed."
info "Please install docker package."
exit 1
fi
# Variables
IMAGE="$1"
USERNAME="$2"
PASSWORD="$3"
REGISTRY="$4"
PULL_BASE_FROM_DOCKERHUB="$5"
set -e
set -x
# ----- Pushing image(s) -----
# see documentation :
# - https://docs.docker.com/reference/commandline/cli/#login
# - https://docs.docker.com/reference/commandline/cli/#push
# - https://docs.docker.com/reference/commandline/cli/#logout
# ---------------------------
# Push the docker image
h2 "Pushing image to Docker registry"
DOCKER_PUSH="docker push $IMAGE"
info "$DOCKER_PUSH"
DOCKER_PUSH_OUTPUT=$($DOCKER_PUSH)
if [ $? -ne 0 ];then
warn $DOCKER_PUSH_OUTPUT
error "Pushing image $IMAGE failed";
else
success "Pushing image $IMAGE succeeded";
fi
if [ "$PULL_BASE_FROM_DOCKERHUB" == "true" ];then
h2 "Remove local goharbor images"
DOCKER_RMI="docker rmi -f $(docker images | grep "${IMAGE%:*}" | awk '{print $3}') -f"
info "$DOCKER_RMI"
DOCKER_RMI_OUTPUT=$($DOCKER_RMI)
if [ $? -ne 0 ];then
warn $DOCKER_RMI_OUTPUT
error "Clean local goharbor images failed";
else
success "Clean local goharbor images succeeded";
fi
fi