2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
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
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
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
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
32 # $SETTINGS_FILE : Job level variable with maven settings file location
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.
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
45 echo "---> docker-login.sh"
46 # Ensure we fail the job if any steps fail
49 #Check if current version less than desired version
50 version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
52 # Execute the credential lookup and set
54 set +x # Ensure that no other scripts add `set -x` and print passwords
56 CREDENTIAL=$(xmlstarlet sel -N "x=http://maven.apache.org/SETTINGS/1.0.0" \
57 -t -m "/x:settings/x:servers/x:server[starts-with(x:id, '${1}')]" \
58 -v x:username -o ":" -v x:password \
61 USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
62 PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
66 echo "ERROR: No user provided"
72 echo "ERROR: No password provided"
78 # Login to the registry
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'
85 docker login -u "$USER" -p "$PASS" -e "$2" "$1"
87 docker login -u "$USER" -p "$PASS" "$1"
94 # Loop through Registry and Ports to concatentate and login to nexus
95 if [ "${DOCKER_REGISTRY:-none}" != 'none' ]
97 for PORT in $REGISTRY_PORTS
99 REGISTRY="${DOCKER_REGISTRY}:${PORT}"
101 # docker login requests an email address if nothing is passed to it
102 # Nexus, however, does not need this and ignores the value
103 set_creds "$REGISTRY"
104 do_login "$REGISTRY" none
108 # Login to docker.io after determining if email is needed.
109 if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ]
111 set_creds "$DOCKERHUB_REGISTRY"
112 if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]
114 do_login "$DOCKERHUB_REGISTRY" "$DOCKERHUB_EMAIL"
116 do_login "$DOCKERHUB_REGISTRY" none
120 echo "---> docker-login.sh ends"