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 echo "---> docker-login.sh"
12 # Log into a custom hosted docker registry and / or docker.io
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
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
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
33 # $SETTINGS_FILE : Job level variable with maven settings file location
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.
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
47 # Ensure we fail the job if any steps fail
50 #Check if current version less than desired version
51 version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
53 # Execute the credential lookup and set
55 set +x # Ensure that no other scripts add `set -x` and print passwords
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 \
62 USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
63 PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
67 echo "ERROR: No user provided"
73 echo "ERROR: No password provided"
79 # Login to the registry
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'
86 docker login -u "$USER" -p "$PASS" -e "$2" "$1"
88 docker login -u "$USER" -p "$PASS" "$1"
95 # Loop through Registry and Ports to concatentate and login to nexus
96 if [ "${DOCKER_REGISTRY:-none}" != 'none' ]
98 for PORT in $REGISTRY_PORTS
100 REGISTRY="${DOCKER_REGISTRY}:${PORT}"
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
109 # Login to docker.io after determining if email is needed.
110 if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ]
112 set_creds "$DOCKERHUB_REGISTRY"
113 if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]
115 do_login "$DOCKERHUB_REGISTRY" "$DOCKERHUB_EMAIL"
117 do_login "$DOCKERHUB_REGISTRY" none