Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / docker-login.sh
index cac5186..73d0603 100644 (file)
@@ -8,7 +8,6 @@
 # which accompanies this distribution, and is available at
 # http://www.eclipse.org/legal/epl-v10.html
 ##############################################################################
-
 # Log into a custom hosted docker registry and / or docker.io
 
 # $DOCKER_REGISTRY : Optional
 #2) User logging into docker.io with docker version <17.06.0  email optional
 #3) User logging into docker.io wiht docker version >= 17.06.0 cannot use email flag
 
-
+echo "---> docker-login.sh"
 # Ensure we fail the job if any steps fail
 set -eu -o pipefail
 
-#Check if current version less than desired version
-version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
+# Check if current version less than desired version
+version_lt() {
+    test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1";
+}
 
 # Execute the credential lookup and set
 set_creds() {
@@ -62,14 +63,12 @@ set_creds() {
     USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
     PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
 
-    if [ -z "$USER" ]
-    then
+    if [ -z "$USER" ]; then
         echo "ERROR: No user provided"
         return 1
     fi
 
-    if [ -z "$PASS" ]
-    then
+    if [ -z "$PASS" ]; then
         echo "ERROR: No password provided"
         return 1
     fi
@@ -79,8 +78,8 @@ set_creds() {
 # Login to the registry
 do_login() {
     docker_version=$( docker -v | awk '{print $3}')
-    if version_lt $docker_version "17.06.0" && \
-       "$DOCKERHUB_REGISTRY" == "docker.io" && \
+    if version_lt "$docker_version" "17.06.0" && \
+       [ "$DOCKERHUB_REGISTRY" == "docker.io" ] && \
        "$DOCKERHUB_EMAIL:-none" != 'none'
     then
         docker login -u "$USER" -p "$PASS" -e "$2" "$1"
@@ -93,27 +92,25 @@ do_login() {
 ### Main ###
 
 # Loop through Registry and Ports to concatentate and login to nexus
-if [ "${DOCKER_REGISTRY:-none}" != 'none' ]
-then
-    for PORT in $REGISTRY_PORTS
-    do
+if [ "${DOCKER_REGISTRY:-none}" != 'none' ]; then
+    for PORT in $REGISTRY_PORTS; do
         REGISTRY="${DOCKER_REGISTRY}:${PORT}"
 
         # docker login requests an email address if nothing is passed to it
         # Nexus, however, does not need this and ignores the value
-        set_creds $REGISTRY
+        set_creds "$REGISTRY"
         do_login "$REGISTRY" none
     done
 fi
 
 # Login to docker.io after determining if email is needed.
-if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ]
-then
-    set_creds $DOCKERHUB_REGISTRY
-    if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]
-    then
+if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ]; then
+    set_creds "$DOCKERHUB_REGISTRY"
+    if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]; then
         do_login "$DOCKERHUB_REGISTRY" "$DOCKERHUB_EMAIL"
     else
         do_login "$DOCKERHUB_REGISTRY" none
     fi
 fi
+
+echo "---> docker-login.sh ends"