Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / npm-config.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
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 "---> npm-config.sh"
12
13 # Configure a custom hosted npm registry and / or https://registry.npmjs.org/
14
15 # $NPM_REGISTRY : Required
16 #                 Jenkins global variable should be defined
17 #                 If set, then this is the base IP or FQDN that will be used
18 #                 for logging into the custom npm registry
19 #                 ex: $NPM_URL/repository/npm.snapshot/
20 #                 wereh NPM_URL is defined as a global env var in Jenkins.
21 # $NPM_SERVER_ID: Required
22 #                 The id of the server as specified in the global-settings file
23 #                 Typically this is settings.xml
24 # $SETTINGS_FILE: Required
25 #                 Job level variable with maven settings file location
26 #
27
28 # Ensure we fail the job if any steps fail
29 set -eu -o pipefail
30
31 # Execute the credential lookup and login to the registry
32 do_config() {
33     echo "$1"
34     CREDENTIAL=$(xmlstarlet sel -N "x=http://maven.apache.org/SETTINGS/1.0.0" \
35         -t -m "/x:settings/x:servers/x:server[starts-with(x:id, '${NPM_SERVER_ID}')]" \
36         -v x:username -o ":" -v x:password \
37         "$SETTINGS_FILE")
38
39     USER=$(echo "$CREDENTIAL" | cut -f1 -d:)
40     PASS=$(echo "$CREDENTIAL" | cut -f2 -d:)
41
42     if [ -z "$USER" ]; then
43         echo "ERROR: No user provided"
44         return 1
45     fi
46
47     if [ -z "$PASS" ]; then
48         echo "ERROR: No password provided"
49         return 1
50     fi
51
52     # Compute auth token
53     auth_token=$(echo -n "$USER":"$PASS" | openssl base64)
54
55     # Write .npmrc
56     echo "//$NPM_REGISTRY:_auth$auth_token" >> "$HOME/.npmrc"
57 }