X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fdocker-login.sh;fp=shell%2Fdocker-login.sh;h=a2055a9b2a993a1c839188d3fa43033acb0ea90d;hb=5a87383ecf1903ef44cd9d3dbcb53eef3adf4427;hp=0000000000000000000000000000000000000000;hpb=14ff3601ee1c72496af243eca74ac269d26d6877;p=releng%2Fglobal-jjb.git diff --git a/shell/docker-login.sh b/shell/docker-login.sh new file mode 100644 index 00000000..a2055a9b --- /dev/null +++ b/shell/docker-login.sh @@ -0,0 +1,78 @@ +#!/bin/bash +# @License EPL-1.0 +############################################################################## +# Copyright (c) 2017 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## + +# Log into a custom hosted docker registry and / or docker.io + +# $DOCKER_REGISTRY : Optional +# Jenkins global variable should be defined +# If set, then this is the base IP or FQDN that will be used +# for logging into the custom docker registry +# ex: nexus3.example.com +# +# $REGISTRY_PORTS : Required if DOCKER_REGISTRY is set +# Jenkins global variable should be defined (space separated) +# Listing of all the registry ports to login to +# ex: 10001 10002 10003 10004 +# +# $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 + +# Ensure we fail the job if any steps fail +set -eu -o pipefail + +# Execute the credential lookup and login to the registry +do_login() { + 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}')]" \ + -v x:username -o ":" -v x:password \ + "$SETTINGS_FILE") + + USER=$(echo "$CREDENTIAL" | cut -f1 -d:) + PASS=$(echo "$CREDENTIAL" | cut -f2 -d:) + + if [ -z "$USER" ] + then + echo "ERROR: No user provided" + return 1 + fi + + if [ -z "$PASS" ] + then + echo "ERROR: No password provided" + return 1 + fi + + docker login -u "$USER" -p "$PASS" -e "$2" "$1" +} + +if [ "${REGISTRY:-none}" != 'none' ] +then + for PORT in $REGISTRY_PORTS + do + REGISTRY="${DOCKER_REGISTRY}:${PORT}" + + # docker login requests an email address if nothing is passed to it + # Nexus, however, does not need this and ignores the value + do_login "$REGISTRY" none + done +fi + +# Attempt to login to docker.io only if $DOCKERHUB_EMAIL is configured +if [ "${DOCKERHUB_EMAIL:-none}" != 'none' ] +then + do_login docker.io "$DOCKERHUB_EMAIL" +fi