Injecting environmental variables in build thats uses NodeJS Plugin

89 views
Skip to first unread message

Anthony Green

unread,
Nov 29, 2017, 3:31:46 AM11/29/17
to Jenkins Users
I have a job that publishes to a private NPM repo.

The job is uses the NodeJS Plugin to provide an instance of node

To publish to NPM you need an .npmrc file specifying your auth token

In Jenkins configuration I have a managed file containing

//registry.npmjs.org/:_authToken=$NPM_AUTH_TOKEN

with the intention that $NPM_AUTH_TOKEN is injected as an environment variable

So under Bindings

I add it as a Secret Text

just using the system node this works but using the NodeJS Plugin it doesn't

If under Build Environment I

Inject environment variables to the build process

with the following groovy script


import jenkins.*
import jenkins.model.*
import com.cloudbees.plugins.credentials.Credentials

def credentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(com.cloudbees.plugins.credentials.Credentials.class);

def npm_creds =[:]
for (c in credentials) {
  if (c.id == 'NPM_AUTH_TOKEN') {
    npm_creds = ['NPM_AUTH_TOKEN': c.secret]
  }
}

return npm_creds

then the NodeJS plug does pick up the 
NPM_AUTH_TOKEN

Can anyone suggest either:

A simpler groovy script that just uses getC
redentialById 

or a simpler way for NodeJS to pick up the environment variable ?

TIA

Anthony


Victor Martinez

unread,
Nov 29, 2017, 4:02:08 AM11/29/17
to Jenkins Users
Hi,

What about using https://wiki.jenkins.io/display/JENKINS/Mask+Passwords+Plugin? It might help you to create a global pass parameter to be used as an environment variable.

Cheers


Reply all
Reply to author
Forward
0 new messages