【问题标题】:Access Jenkins Credentials Environment Variables from Node.js从 Node.js 访问 Jenkins 凭证环境变量
【发布时间】:2020-12-21 18:17:27
【问题描述】:

我已经使用 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 来访问这些环境变量,但这不起作用我得到了 undefined ... 谢谢

【问题讨论】:

    标签: node.js jenkins


    【解决方案1】:

    您使用了哪种类型的凭据: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,抱歉。

    【讨论】:

    • 对,在 nodejs 中可以做 process.env。
    • 我正在使用机密文本的凭据,@PankajSaini 不明白你在说什么,你能解释一下吗?
    【解决方案2】:

    您按以下顺序传递您的凭据:Jenkins credentials > pipeline environment variable > node.js command line parameter

    确保首先创建一个 Jenkins 凭据作为秘密文本。

    pipeline {
      agent any
      environment {
          JENKINS_SECRET_TEXT=credentials('JENKINS_SECRET_TEXT')
      }
      stages {
        stage('Pass secret as command line parameter') {
          steps {
            sh 'SECRET_ENV_VAR="$JENKINS_SECRET_TEXT" node app.js'
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多