/
04_push.sh
executable file
·50 lines (40 loc) · 2.17 KB
/
04_push.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
#!/bin/bash
set -e
STARTMSG="[push]"
[ -z "$1" ] && echo "$STARTMSG No parameter with the Docker registry URL. Exit now." && exit 1
[ "$1" == "NOT2PUSH" ] && echo "$STARTMSG The NOT2PUSH slug is only for local build and retag not for pushin to docker registries. Exit now." && exit 1
[ -z "$2" ] && echo "$STARTMSG No parameter with the Docker registry username. Exit now." && exit 1
[ -z "$3" ] && echo "$STARTMSG No parameter with the Docker registry password. Exit now." && exit 1
REGISTRY_URL="$1"
REGISTRY_USER="$2"
REGISTRY_PW="$3"
##################################
# Find the right Docker Repo name e.g. dcso/misp-dockerized-proxy
[ -z "$(git remote get-url origin|grep git@)" ] || GIT_REPO="$(git remote get-url origin|sed 's,.*:,,'|sed 's,....$,,')"
[ -z "$(git remote get-url origin|grep http)" ] || GIT_REPO="$(git remote get-url origin|sed 's,.*github.com/,,'|sed 's,....$,,')"
if [ ! -z "$GITLAB_HOST" ]; then
[ -z "$(echo "$GIT_REPO" | grep "$GITLAB_HOST")" ] || GIT_REPO="$(git remote get-url origin|sed 's,.*'${GITLAB_HOST}'/'${GITLAB_GROUP}'/,,'|sed 's,....$,,')"
fi
# Set Container Name in lower case
CONTAINER_NAME="$(echo "$GIT_REPO"|cut -d / -f 2|tr '[:upper:]' '[:lower:]')"
# Set the right Docker Repository with the Docker registry URL
DOCKER_REPO="$REGISTRY_URL/$CONTAINER_NAME"
# Find all builded versions of the current Docker image
ALL_BUILD_DOCKER_VERSIONS=$(docker images --format '{{.Repository}}={{.Tag}}'|grep "$DOCKER_REPO"|cut -d = -f 2)
# Login to Docker registry
[ "$REGISTRY_URL" != "dcso" ] && DOCKER_LOGIN_OUTPUT="$(echo "$REGISTRY_PW" | docker login -u "$REGISTRY_USER" "$REGISTRY_URL" --password-stdin)"
[ "$REGISTRY_URL" = "dcso" ] && DOCKER_LOGIN_OUTPUT="$(echo "$REGISTRY_PW" | docker login -u "$REGISTRY_USER" --password-stdin)"
echo "$DOCKER_LOGIN_OUTPUT"
DOCKER_LOGIN_STATE="$(echo "$DOCKER_LOGIN_OUTPUT" | grep 'Login Succeeded')"
if [ ! -z "$DOCKER_LOGIN_STATE" ]; then
# Push all Docker images
for i in $ALL_BUILD_DOCKER_VERSIONS
do
echo "$STARTMSG docker push $DOCKER_REPO:$i" && docker push "$DOCKER_REPO:$i"
[ "$?" -ne 0 ] && exit 1
done
else
echo "$DOCKER_LOGIN_OUTPUT"
exit
fi
echo "$STARTMSG $0 is finished."