The secret text being printed as *** is a feature, so credentials aren't 
leaked in console logs. You cannot use Jenkins credentials during 
application runtime. (ie: If you deploy a backend Node app to an app 
server) You can however use Jenkins credentials to run npm scripts, such as 
npm test, (which require the Node runtime) within a Jenkins build.

The credentials function needs an input of the credential ID. When a 
credential is created, you can set the ID value to a custom one, such as a 
human-friendly name. Otherwise, you will end up with a unique UUID.

Everyone has their own personal taste, but I would refactor the Jenkinsfile 
above to the following:

stage ("E2E tests") {  
  when {
    branch 'e2e-dev'
  }
  environment {
    MY_SECRET = credentials('jenkins-secret-id')
  }   
  steps {
    sh 'rm -f config/e2e-config.json'
    sh 'mv config/e2e-dev-config.json config/e2e-config.json'
    sh 'npm install'
    sh 'npm test'
  }
}

With the above Jenkinsfile, you should be able to reference your secret 
with ${MY_SECRET} anywhere in the code or shell steps, but only when 
running npm scripts. This will not work for running applications on a 
server. It should be noted that if you need to specify the secret within a 
shell step line, that line must use double quotes to resolve the variable 
properly.

Another way of doing this would be:

withCredentials([string(credentialsId: 'credential-id-here', variable: 
'CUSTOM_VARIABLE_NAME_HERE')]) {
  sh "MY_SECRET=CUSTOM_VARIABLE_NAME_HERE npm test"
}

I hope that helps.


On Thursday, August 3, 2017 at 6:26:28 AM UTC-4, Idan Adar wrote:
>
> I have done the following:
>
> 1. Create a "secret text" type credential
> 2. Put in the credential a password
> 3. Create a Global Variable, mySecret, with its value being the credential 
> ID
> 4. In the declarative pipeline: 
>
> stage ("E2E tests") {  
>          environment {
>             mySecret = credentials('${mySecret}')
>          }
>              
>          steps {
>             script {
>                STAGE_NAME = "E2E tests"
>                echo "++++++++++++ $mySecret"
>
>
>                if (JOB_NAME == "e2e-dev") {
>                   // Setup packages and run tests
>                   sh '''
>                      rm -f config/e2e-config.json
>                      mv config/e2e-dev-config.json config/e2e-config.json
>                      npm install
>                      npm test
>                   '''
>                }
>            }
>         }
> }
>
> In Jenkins, the secret text is printed: "*****"
> In the NodeJS app, I'm getting an error...
>
> Global Variables in Jenkins can also be used as environment variables and 
> I know I can use those as clear text in the NodeJS app.
> My question is how to use Jenkins credentials from Jenkins in the NodeJS 
> app...
>
> Any thoughts?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/e30f0a45-fcf2-4237-8ec7-3df2689df9c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to