From 20f7528a09898722c3ac32695186256358242032 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Mon, 9 Oct 2017 15:41:20 -0500
Subject: [PATCH] Add upload-npm role

We need a role for uploading content to npm. The old script assumed a
global npm install - but as this runs on the executor we shouldn't
assume that. Use nvm to install node into the work_dir and use the npm
from that node installation.

Change-Id: I7f7c031ce754838776043325025fe5928cd36135
---
 roles/upload-npm/README.rst          | 39 ++++++++++++++++++++++++++++
 roles/upload-npm/defaults/main.yaml  |  1 +
 roles/upload-npm/tasks/main.yaml     | 21 +++++++++++++++
 roles/upload-npm/templates/.npmrc.j2 | 10 +++++++
 4 files changed, 71 insertions(+)
 create mode 100644 roles/upload-npm/README.rst
 create mode 100644 roles/upload-npm/defaults/main.yaml
 create mode 100644 roles/upload-npm/tasks/main.yaml
 create mode 100644 roles/upload-npm/templates/.npmrc.j2

diff --git a/roles/upload-npm/README.rst b/roles/upload-npm/README.rst
new file mode 100644
index 000000000..2b0f00f0d
--- /dev/null
+++ b/roles/upload-npm/README.rst
@@ -0,0 +1,39 @@
+Upload javascript packages to npm
+
+**Role Variables**
+
+.. zuul:rolevar:: npm_info
+
+   Complex argument which contains the information about the npm
+   server as well as the authentication information needed.
+   It is expected that this argument comes from a `Secret`.
+   This role expects to be run on the executor.
+
+  .. zuul:rolevar:: username
+
+     Username to use to log in to npm.
+
+  .. zuul:rolevar:: password
+
+     Password to use to log in to npm.
+
+  .. zuul:rolevar:: email
+
+     Email associated with the npm account.
+
+  .. zuul:rolevar:: author_name
+
+     npm author name.
+
+  .. zuul:rolevar:: author_url
+
+     npm author url.
+
+  .. zuul:rolevar:: author_email
+
+     npm author email.
+
+  .. zuul:rolevar:: registry_url
+     :default: //registry.npmjs.org
+
+     URL of npm registry server.
diff --git a/roles/upload-npm/defaults/main.yaml b/roles/upload-npm/defaults/main.yaml
new file mode 100644
index 000000000..49998a283
--- /dev/null
+++ b/roles/upload-npm/defaults/main.yaml
@@ -0,0 +1 @@
+npm_registry_url: "{{ npm_info.registry_url|default('//registry.npmjs.org') }}"
diff --git a/roles/upload-npm/tasks/main.yaml b/roles/upload-npm/tasks/main.yaml
new file mode 100644
index 000000000..8d8d26377
--- /dev/null
+++ b/roles/upload-npm/tasks/main.yaml
@@ -0,0 +1,21 @@
+# This is not optimized, but allows for getting a version of node in the
+# home dir.
+- name: Create .npmrc configuration file
+  template:
+    dest: "~/.npmrc"
+    mode: 0400
+    src: .npmrc.j2
+
+- name: Install NVM
+  git:
+    repo: 'https://github.com/creationix/nvm'
+    dest: '~/.nvm'
+    version: v0.33.5
+
+- name: Upload tarball to npm
+  shell:
+    executable: /bin/bash
+    cmd: |
+      nvm install node
+      nvm use node
+      npm publish --ignore-scripts {{ zuul.executor.work_dir }}/artifacts/{{ zuul.project.short_name }}-{{ project_ver }}.tar.gz
diff --git a/roles/upload-npm/templates/.npmrc.j2 b/roles/upload-npm/templates/.npmrc.j2
new file mode 100644
index 000000000..01ed2e2da
--- /dev/null
+++ b/roles/upload-npm/templates/.npmrc.j2
@@ -0,0 +1,10 @@
+init.author.name={{ npm_info.author_name }}
+init.author.email={{ npm_info.author_email }}
+init.author.url={{ npm_info.author_url }}
+tag-version-prefix=
+sign-git-tag=true
+
+//registry.npmjs.org/:username={{ npm_info.username }}
+//registry.npmjs.org/:_password={{ npm_info.password }}
+
+//registry.npmjs.org/:email={{ npm_info.author_email }}