From 1520fb6474f9019f95e43386d5bc442614533f44 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Wed, 7 Sep 2022 13:23:41 +1000 Subject: [PATCH] upload-npm : support authToken argument Support setting authToken to use a token for upload rather than username/password. This is "based" on Id0c47d088d6e8febbae3c96caecc81ebe247754e which does the same for pypi. There we run a test by uploading to the test pypi server, I'm not aware of a similar thing for npm. Change-Id: Ie8610bdfaa33b2120a61802c12e6ba534fe55464 --- roles/upload-npm/README.rst | 7 +++++++ roles/upload-npm/tasks/main.yaml | 7 +++++++ roles/upload-npm/templates/.npmrc.j2 | 5 ++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/roles/upload-npm/README.rst b/roles/upload-npm/README.rst index 4d5260e84..05cbf9ff3 100644 --- a/roles/upload-npm/README.rst +++ b/roles/upload-npm/README.rst @@ -17,6 +17,13 @@ Upload javascript packages to npm Password to use to log in to npm. + .. zuul:rolevar:: authToken + + An auth token (issued from the npm server) to authenticate with. + This is required if you are using two-factor authentication. If + specifying an authToken you should not specify ```username`` and + ``password``. + .. zuul:rolevar:: email Email associated with the npm account. diff --git a/roles/upload-npm/tasks/main.yaml b/roles/upload-npm/tasks/main.yaml index 1812f3fc5..2216bdfb9 100644 --- a/roles/upload-npm/tasks/main.yaml +++ b/roles/upload-npm/tasks/main.yaml @@ -1,3 +1,10 @@ +- name: Validate password/token combo + fail: + msg: 'Specify either username/password or authToken' + when: > + (npm_credentials.authToken is defined) and + ((npm_credentials.username is defined) or (npm_credentials.password is defined)) + # This is not optimized, but allows for getting a version of node in the # home dir. - name: Create .npmrc configuration file diff --git a/roles/upload-npm/templates/.npmrc.j2 b/roles/upload-npm/templates/.npmrc.j2 index 572273211..babaff6b0 100644 --- a/roles/upload-npm/templates/.npmrc.j2 +++ b/roles/upload-npm/templates/.npmrc.j2 @@ -4,7 +4,10 @@ init.author.url={{ npm_credentials.author_url }} tag-version-prefix= sign-git-tag=true +{% if npm_credentials.username is defined %} //registry.npmjs.org/:username={{ npm_credentials.username }} //registry.npmjs.org/:_password={{ npm_credentials.password }} - +{% else %} +//registry.npmjs.org/:_authToken={{ npm_credentials.authToken }} +{% endif %} //registry.npmjs.org/:email={{ npm_credentials.author_email }}