forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushimage.sh
executable file
·139 lines (113 loc) · 2.84 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
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
set +e
set -o noglob
echo "This shell will push specific image to registry server."
echo "Usage: #./pushimage [imgae tag] [registry username] [registry password] [registry server]"
#
# 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"
set -e
# ----- 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
# ---------------------------
# Login to the registry
h2 "Login to the Docker registry"
DOCKER_LOGIN="docker login --username $USERNAME --password $PASSWORD $4"
info "docker login --username $USERNAME --password *******"
DOCKER_LOGIN_OUTPUT=$($DOCKER_LOGIN)
if [ $? -ne 0 ]; then
warn "$DOCKER_LOGIN_OUTPUT"
error "Login to Docker registry $REGISTRY failed"
exit 1
else
success "Login to Docker registry $REGISTRY succeeded";
fi
# 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
# Logout from the registry
h2 "Logout from the docker registry"
DOCKER_LOGOUT="docker logout $REGISTRY"
DOCKER_LOGOUT_OUTPUT=$($DOCKER_LOGOUT)
if [ $? -ne 0 ]; then
warn "$DOCKER_LOGOUT_OUTPUT"
error "Logout from Docker registry $REGISTRY failed"
exit 1
else
success "Logout from Docker registry $REGISTRY succeeded"
fi