如何在具有节点,阶段的jenkins文件中使用凭证功能

时间:2018-12-26 10:03:06

标签: jenkins google-cloud-platform

使用以下代码时,我没有获得Google凭据。 def GOOGLE_CREDENTIALS =凭据(“ XYZ凭据”)

1 个答案:

答案 0 :(得分:2)

[已编辑]

请检查此官方guide来查看您是否正确定义了它。

还要检查您是否要传递凭据ID,而不是对credentials()方法的描述。

如果您使用的是Jenkins管道,也可以尝试Credentials Binding Plugin。 在插件wiki中,用户名密码类型凭证的典型示例如下:

withCredentials([usernamePassword(credentialsId: 'amazon', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  // available as an env variable, but will be masked if you try to print it out any which way
  // note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
  sh 'echo $PASSWORD'
  // also available as a Groovy variable
  echo USERNAME
  // or inside double quotes for string interpolation
  echo "username is $USERNAME"
}

对于脚本化管道,您也可以使用withCredentials()(请参见this):

node {
  withCredentials([usernameColonPassword(credentialsId: 'mylogin', variable: 'USERPASS')]) {
    sh '''
      set +x
      curl -u "$USERPASS" https://private.server/ > output
    '''
  }
}

或者您可以使用withEnv()部分:

node {
    withEnv(["CREDS=credentials('jenkins-creds')"]) {
        stage('Build') {
            sh 'printenv'
        }
    }
}