Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

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

My jenkins pipeline stage works find when I just use aws credentials alone. However I am trying to add in a second withCredentials in the same pipeline stage to point to a secret file called kubeconfig (this holds my kubeconfig file and is stored in the jenkins credentials) But I cannot get this to work. Would anyone mind taking a second look to review my syntax here please and if I'm missing some configuration. Thanks Brian

           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{ 
question from:https://stackoverflow.com/questions/66062834/how-to-add-a-secret-file-credential-to-a-jenkins-pipeline-stage-using-withcreden

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your example, you have specified correctly for AmazonWebServicesCredentialsBinding and missed the secret file details. It will be like the below example:

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

If I correct your example it will be:

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

   }
}

you can find more example from jenkins documentation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...