如何使用 withCredentials 语法将秘密文件凭证添加到 jenkins 管道阶段

how to add a secret file credential to a jenkins pipeline stage using withCredentials syntax

当我仅使用 aws 凭据时,我的 jenkins 管道阶段可以正常工作。 但是我试图在同一管道阶段添加第二个 withCredentials 以指向一个名为 kubeconfig 的秘密文件(这包含我的 kubeconfig 文件并存储在 jenkins 凭据中) 但我无法让它发挥作用。如果我遗漏了一些配置,请有人介意再次查看我的语法吗? 谢谢 布莱恩

           steps {
               
        withCredentials([[
                      $class: 'AmazonWebServicesCredentialsBinding',
                      accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                      credentialsId: 'awstoEKS',  // ID of credentials in Jenkins
                      secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
                  ],
                  [
                      credentialsId: 'kubeconfig',
                      Variable: 'kubeconfig'
                      ])
                      
                   {

               script{ 

对于您的示例,您已正确指定 AmazonWebServicesCredentialsBinding 并且遗漏了机密文件详细信息。就像下面的例子:

pipeline {
    agent any;
    stages {
        stage('debug') {
            steps {
                withCredentials([
                    file(credentialsId: 'secret-file', variable: 'FILE'),
                    [
                        $class: 'AmazonWebServicesCredentialsBinding',
                        accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                        credentialsId: 'awstoEKS',
                        secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
                    ]
                    
                ]) {
                    
                  
                  sh """
                    cat $FILE
                    curl -u $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY https:/do.something.aws.com > output
                  """
                }
            }
        }
    }
}

如果我更正您的示例,它将是:

....
steps {
   withCredentials([
       file(credentialsId: 'kubeconfig', variable: 'kubeconfig'),
       [
           $class: 'AmazonWebServicesCredentialsBinding',
           accessKeyVariable: 'AWS_ACCESS_KEY_ID',
           credentialsId: 'awstoEKS',  // ID of credentials in Jenkins
           secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
       ]
   ]) {
      sh """
         echo 'do something with' $kubeconfig $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      """

   }
}

您可以从 jenkins documentation

中找到更多示例