2 # @License EPL-1.0 <http://spdx.org/licenses/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 ##############################################################################
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 # $SETTINGS_FILE : Job level variable with maven settings file location
27 # $DOCKERHUB_EMAIL : Optional
28 # Jenkins global variable that defines the email address that
29 # should be used for logging into DockerHub
30 # If defined than an attempt to login to docker hub will
33 # Ensure we fail the job if any steps fail
36 # Execute the credential lookup and login to the registry
39 CREDENTIAL=$(xmlstarlet sel -N "x=http://maven.apache.org/SETTINGS/1.0.0" \
40 -t -m "/x:settings/x:servers/x:server[starts-with(x:id, '${1}')]" \
41 -v x:username -o ":" -v x:password \
44 USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
45 PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
49 echo "ERROR: No user provided"
55 echo "ERROR: No password provided"
59 docker login -u "$USER" -p "$PASS" -e "$2" "$1"
62 if [ "${REGISTRY:-none}" != 'none' ]
64 for PORT in $REGISTRY_PORTS
66 REGISTRY="${DOCKER_REGISTRY}:${PORT}"
68 # docker login requests an email address if nothing is passed to it
69 # Nexus, however, does not need this and ignores the value
70 do_login "$REGISTRY" none
74 # Attempt to login to docker.io only if $DOCKERHUB_EMAIL is configured
75 if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ]
77 do_login docker.io "$DOCKERHUB_EMAIL"