Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Jenkins Credentials Environment Variables from Node.js

I have set up some credentials environment variables using the Credentials plugin for Jenkins. I am using them in my Jenkinsfile like this :

pipeline {
  agent any
  environment {
      
      DEV_GOOGLE_CLIENT_ID          = credentials('DEV_GOOGLE_CLIENT_ID')
      DEV_GOOGLE_CLIENT_SECRET      = credentials('DEV_GOOGLE_CLIENT_SECRET')
     
  }
stages {
stage('Install dependencies') {
  steps {
      dir("./codes") {
            sh 'npm install'
        }
  }
}
stage('Stop previous forever process') {
  steps {
    dir("./codes") {
       sh 'forever stop dev || ls'
    }
  }
}
stage('Clean forever logs') {
    steps {
        dir("./codes") {
            sh 'forever cleanlogs'
        }
    }
}
stage('Test ') {
    steps {
        dir("./codes") {
            sh 'npm run test'
        }
     }
   }
 }
}

In my Node.js code, I'm trying to get access to those env variables by writing process.env.DEV_GOOGLE_CLIENT_SECRET but that is not working I am getting undefined ... Thank you

like image 354
Mickael Zana Avatar asked Dec 05 '25 08:12

Mickael Zana


1 Answers

Which type of credentials did you use: secret text or username and password?

While using username and password you can get username and password separately each other like this

pipeline {
  agent any
  environment {
      DEV_GOOGLE_CLIENT = credentials('DEV_GOOGLE_CLIENT')
  }
stages {
stage('Get username and password') {
  steps {
    echo "username is $DEV_GOOGLE_CLIENT_USR"
    echo "password is $DEV_GOOGLE_CLIENT_PSW"
  }
}
}

I don't know how to call $DEV_GOOGLE_CLIENT_USR and $DEV_GOOGLE_CLIENT_PSW in node.js, sorry.

like image 66
Dmitriy Tarasevich Avatar answered Dec 07 '25 22:12

Dmitriy Tarasevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!