Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / docker-login.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Log into a custom hosted docker registry and / or docker.io
12
13 # $DOCKER_REGISTRY : Optional
14 #                    Jenkins global variable should be defined
15 #                    If set, then this is the base IP or FQDN that will be used
16 #                    for logging into the custom docker registry
17 #                    ex: nexus3.example.com
18 #
19 # $REGISTRY_PORTS  : Required if DOCKER_REGISTRY is set
20 #                    Jenkins global variable should be defined (space separated)
21 #                    Listing of all the registry ports to login to
22 #                    ex: 10001 10002 10003 10004
23 #
24 # $DOCKERHUB_REGISTRY: Optional
25 #                      Set global Jenkins variable to `docker.io`
26 #                      Additionally you will need to add as an entry
27 #                      to your projects mvn-settings file with username
28 #                      and password creds.  If you are using docker version
29 #                      < 17.06.0 you will need to set DOCKERHUB_EMAIL
30 #                      to auth to docker.io
31 #
32 # $SETTINGS_FILE   : Job level variable with maven settings file location
33 #
34 # $DOCKERHUB_EMAIL : Optional
35 #                    Jenkins global variable that defines the email address that
36 #                    should be used for logging into DockerHub
37 #                    Note that this will not be used if you are using
38 #                    docker version >=17.06.0.
39
40 #So here are the cases in docker login
41 #1) User logging into nexus no email required regardless of docker version
42 #2) User logging into docker.io with docker version <17.06.0  email optional
43 #3) User logging into docker.io wiht docker version >= 17.06.0 cannot use email flag
44
45 echo "---> docker-login.sh"
46 # Ensure we fail the job if any steps fail
47 set -eu -o pipefail
48
49 # Check if current version less than desired version
50 version_lt() {
51     test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1";
52 }
53
54 # Execute the credential lookup and set
55 set_creds() {
56     set +x  # Ensure that no other scripts add `set -x` and print passwords
57     echo "$1"
58     CREDENTIAL=$(xmlstarlet sel -N "x=http://maven.apache.org/SETTINGS/1.0.0" \
59         -t -m "/x:settings/x:servers/x:server[starts-with(x:id, '${1}')]" \
60         -v x:username -o ":" -v x:password \
61         "$SETTINGS_FILE")
62
63     USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
64     PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
65
66     if [ -z "$USER" ]; then
67         echo "ERROR: No user provided"
68         return 1
69     fi
70
71     if [ -z "$PASS" ]; then
72         echo "ERROR: No password provided"
73         return 1
74     fi
75
76 }
77
78 # Login to the registry
79 do_login() {
80     docker_version=$( docker -v | awk '{print $3}')
81     if version_lt "$docker_version" "17.06.0" && \
82        [ "$DOCKERHUB_REGISTRY" == "docker.io" ] && \
83        "$DOCKERHUB_EMAIL:-none" != 'none'
84     then
85         docker login -u "$USER" -p "$PASS" -e "$2" "$1"
86     else
87         docker login -u "$USER" -p "$PASS" "$1"
88     fi
89 }
90
91
92 ### Main ###
93
94 # Loop through Registry and Ports to concatentate and login to nexus
95 if [ "${DOCKER_REGISTRY:-none}" != 'none' ]; then
96     for PORT in $REGISTRY_PORTS; do
97         REGISTRY="${DOCKER_REGISTRY}:${PORT}"
98
99         # docker login requests an email address if nothing is passed to it
100         # Nexus, however, does not need this and ignores the value
101         set_creds "$REGISTRY"
102         do_login "$REGISTRY" none
103     done
104 fi
105
106 # Login to docker.io after determining if email is needed.
107 if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ]; then
108     set_creds "$DOCKERHUB_REGISTRY"
109     if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]; then
110         do_login "$DOCKERHUB_REGISTRY" "$DOCKERHUB_EMAIL"
111     else
112         do_login "$DOCKERHUB_REGISTRY" none
113     fi
114 fi
115
116 echo "---> docker-login.sh ends"