From 719fc279b6832662ff646ac24af5396662547146 Mon Sep 17 00:00:00 2001 From: Robert Stettner Date: Thu, 27 Jul 2017 10:25:23 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + .travis.yml | 14 ++++++++++++++ Dockerfile | 10 ++++++++++ LICENSE.txt | 21 ++++++++++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++++++ bin/expect-npm-auth | 23 ++++++++++++++++++++++ bin/npm-auth | 47 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 155 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Dockerfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100755 bin/expect-npm-auth create mode 100755 bin/npm-auth diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1664508 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +--- +sudo: required + +services: + - docker + +language: generic + +script: + - if [ "$TRAVIS_BRANCH" == "master" ]; then + docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"; + docker build . -t robertstettner/drone-npm-auth; + docker push robertstettner/drone-npm-auth; + fi \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1de42d1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:6.9.1-slim + +# Install packages +RUN apt-get update && apt-get install -yq \ + git \ + expect-dev + +ADD bin/ /bin + +CMD npm-auth \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f25054b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright 2017 (c) Robert Stettner (https://github.com/robertstettner) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f23fb52 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# drone-npm-auth +[![Build Status](https://travis-ci.org/robertstettner/drone-npm-auth.svg?branch=master)](https://travis-ci.org/robertstettner/drone-npm-auth) + +Drone plugin for authenticating into NPM to install private dependencies. + +## Configuration + +The following parameters are used to configure the plugin: + +- `username`: The NPM username. Required. +- `password`: The NPM password. Required. +- `email`: The NPM email. Required. +- `registry`: The NPM registry. Defaults to the default NPM registry. +- `scope`: Scope of the NPM authentication. Optional. +- `path`: Output path of the generated `.npmrc` file. Defaults to `./`. + +### Drone configuration example + +```yaml +pipeline: + npm_auth: + image: robertstettner/drone-npm-auth + username: joebloggs + password: mypass + email: jb@me.com + + build: + image: node:6 + commands: + - npm install + - npm test + when: + event: [push, pull_request] +``` + + +## License + +MIT \ No newline at end of file diff --git a/bin/expect-npm-auth b/bin/expect-npm-auth new file mode 100755 index 0000000..1368bc4 --- /dev/null +++ b/bin/expect-npm-auth @@ -0,0 +1,23 @@ +#!/usr/bin/expect + +set timeout 20 + +set cmd [lindex $argv 0] +set username [lindex $argv 1] +set password [lindex $argv 2] +set email [lindex $argv 3] + +eval spawn $cmd + +expect "Username: " +send "$username\r"; + +expect "Password: " +send "$password\r"; + +expect "Email: (this IS public) " +send "$email\r"; + +expect eof + +puts $expect_out(buffer) \ No newline at end of file diff --git a/bin/npm-auth b/bin/npm-auth new file mode 100755 index 0000000..1824d85 --- /dev/null +++ b/bin/npm-auth @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +starttime=$(date +%s.%N) +username="${NPM_USERNAME:-$PLUGIN_USERNAME}" +password="${NPM_PASSWORD:-$PLUGIN_PASSWORD}" +email="${NPM_EMAIL:-$PLUGIN_EMAIL}" +registry="${NPM_REGISTRY:-$PLUGIN_REGISTRY}" +scope="${NPM_SCOPE:-$PLUGIN_SCOPE}" +path="${PLUGIN_PATH:-./}" + +echo "-- Generating authentication from NPM..." + +set -e + +if [ -z "$username" ]; then + echo "-- Username is not set!" + exit 1 +fi +if [ -z "$password" ]; then + echo "-- Password is not set!" + exit 1 +fi +if [ -z "$email" ]; then + echo "-- Email is not set!" + exit 1 +fi +if [ -z "$registry" ]; then + registry="registry.npmjs.org" +fi +if [ -z "$scope" ]; then + scope_option="" +else + scope_option="--scope=$scope" +fi + +expect-npm-auth "npm login --registry $registry $scope_option" $username $password $email >/dev/null 2>&1 + +endtime=$(date +%s.%N) +echo "duration: $(echo "$endtime $starttime" | awk '{printf "%f", $1 - $2}')s" + +if [ ! -e ~/.npmrc ]; then + echo "-- Error logging into NPM" + exit 1 +else + echo $(cat ~/.npmrc) | sed 's/[[:blank:]]\+/\n/g' > $path.npmrc + echo "-- NPM authentication generation done!" +fi \ No newline at end of file