X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fdocker-login.sh;h=08ec941ae805c35dfcf9eecfcba325bb2f0ba866;hb=2a10c83b2d506008cc6c01c134515fb6328e1f3a;hp=a2055a9b2a993a1c839188d3fa43033acb0ea90d;hpb=af3db4b5361d4ef71a37aed82fb75055201ffe31;p=releng%2Fglobal-jjb.git diff --git a/shell/docker-login.sh b/shell/docker-login.sh index a2055a9b..08ec941a 100644 --- a/shell/docker-login.sh +++ b/shell/docker-login.sh @@ -1,5 +1,5 @@ #!/bin/bash -# @License EPL-1.0 +# SPDX-License-Identifier: EPL-1.0 ############################################################################## # Copyright (c) 2017 The Linux Foundation and others. # @@ -8,7 +8,7 @@ # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v10.html ############################################################################## - +echo "---> docker-login.sh" # Log into a custom hosted docker registry and / or docker.io # $DOCKER_REGISTRY : Optional @@ -22,19 +22,37 @@ # Listing of all the registry ports to login to # ex: 10001 10002 10003 10004 # +# $DOCKERHUB_REGISTRY: Optional +# Set global Jenkins variable to `docker.io` +# Additionally you will need to add as an entry +# to your projects mvn-settings file with username +# and password creds. If you are using docker version +# < 17.06.0 you will need to set DOCKERHUB_EMAIL +# to auth to docker.io +# # $SETTINGS_FILE : Job level variable with maven settings file location # # $DOCKERHUB_EMAIL : Optional # Jenkins global variable that defines the email address that # should be used for logging into DockerHub -# If defined than an attempt to login to docker hub will -# happen +# Note that this will not be used if you are using +# docker version >=17.06.0. + +#So here are the cases in docker login +#1) User logging into nexus no email required regardless of docker version +#2) User logging into docker.io with docker version <17.06.0 email optional +#3) User logging into docker.io wiht docker version >= 17.06.0 cannot use email flag + # Ensure we fail the job if any steps fail set -eu -o pipefail -# Execute the credential lookup and login to the registry -do_login() { +#Check if current version less than desired version +version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; } + +# Execute the credential lookup and set +set_creds() { + set +x # Ensure that no other scripts add `set -x` and print passwords echo "$1" CREDENTIAL=$(xmlstarlet sel -N "x=http://maven.apache.org/SETTINGS/1.0.0" \ -t -m "/x:settings/x:servers/x:server[starts-with(x:id, '${1}')]" \ @@ -56,10 +74,26 @@ do_login() { return 1 fi - docker login -u "$USER" -p "$PASS" -e "$2" "$1" } -if [ "${REGISTRY:-none}" != 'none' ] +# Login to the registry +do_login() { + docker_version=$( docker -v | awk '{print $3}') + if version_lt $docker_version "17.06.0" && \ + "$DOCKERHUB_REGISTRY" == "docker.io" && \ + "$DOCKERHUB_EMAIL:-none" != 'none' + then + docker login -u "$USER" -p "$PASS" -e "$2" "$1" + else + docker login -u "$USER" -p "$PASS" "$1" + fi +} + + +### Main ### + +# Loop through Registry and Ports to concatentate and login to nexus +if [ "${DOCKER_REGISTRY:-none}" != 'none' ] then for PORT in $REGISTRY_PORTS do @@ -67,12 +101,19 @@ then # docker login requests an email address if nothing is passed to it # Nexus, however, does not need this and ignores the value + set_creds $REGISTRY do_login "$REGISTRY" none done fi -# Attempt to login to docker.io only if $DOCKERHUB_EMAIL is configured -if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ] +# Login to docker.io after determining if email is needed. +if [ "${DOCKERHUB_REGISTRY:-none}" != 'none' ] then - do_login docker.io "$DOCKERHUB_EMAIL" + set_creds $DOCKERHUB_REGISTRY + if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ] + then + do_login "$DOCKERHUB_REGISTRY" "$DOCKERHUB_EMAIL" + else + do_login "$DOCKERHUB_REGISTRY" none + fi fi