首页 > 解决方案 > 从 Node.js 访问 Jenkins 凭证环境变量

问题描述

我已经使用 Jenkins 的凭据插件设置了一些凭据环境变量。我在我的 Jenkinsfile 中使用它们,如下所示:

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'
        }
     }
   }
 }
}

在我的 Node.js 代码中,我试图通过编写 process.env.DEV_GOOGLE_CLIENT_SECRET 来访问这些环境变量,但这不起作用我越来越不确定......谢谢

标签: node.jsjenkins

解决方案


您使用了哪种类型的凭据:secret textusername and password

使用username and password时,您可以像这样分别获取用户名和密码

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"
  }
}
}

我不知道如何在 node.js 中调用 $DEV_GOOGLE_CLIENT_USR 和 $DEV_GOOGLE_CLIENT_PSW,抱歉。


推荐阅读