Add npm config script 67/7367/5
authorJeremy Phelps <jphelps@linuxfoundation.org>
Mon, 6 Nov 2017 20:20:55 +0000 (14:20 -0600)
committerJeremy Phelps <jphelps@linuxfoundation.org>
Fri, 10 Nov 2017 16:28:24 +0000 (10:28 -0600)
Change-Id: I12a9af7f6926660143e40b2382b44e7af606ff00
Signed-off-by: Jeremy Phelps <jphelps@linuxfoundation.org>
shell/npm-config.sh [new file with mode: 0644]

diff --git a/shell/npm-config.sh b/shell/npm-config.sh
new file mode 100644 (file)
index 0000000..749e7e5
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+# SPDX-License-Identifier: 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
+##############################################################################
+echo "---> npm-config.sh"
+
+# Configure a custom hosted npm registry and / or https://registry.npmjs.org/
+
+# $NPM_REGISTRY : Required
+#                 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 npm registry
+#                 ex: $NPM_URL/repository/npm.snapshot/
+#                 wereh NPM_URL is defined as a global env var in Jenkins.
+# $NPM_SERVER_ID: Required
+#                 The id of the server as specified in the global-settings file
+#                 Typically this is settings.xml
+# $SETTINGS_FILE: Required
+#                 Job level variable with maven settings file location
+#
+
+# Ensure we fail the job if any steps fail
+set -eu -o pipefail
+
+# Execute the credential lookup and login to the registry
+do_config() {
+    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, '${NPM_SERVER_ID}')]" \
+        -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
+
+    # Compute auth token
+    auth_token=$(echo -n "$USER":"$PASS" | openssl base64)
+
+    # Write .npmrc
+    echo '//'$NPM_REGISTRY':_auth'$auth_token >> $HOME/.npmrc
+}