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