简体   繁体   中英

Jenkins Declarative Pipeline throws NullPointerException when I use withCredentials

I have a small pipeline that performs some GIT actions. I'm trying to use ssh as the protocol to communicate, so I defined the private key in the JenkinsServer and I have everything working fine.

    stage('Some Stage'){
      steps{
        withCredentials([[$class: 'SSHUserPrivateKeyBinding', credentialsId: "${params.credentialsId}", usernameVariable: 'GIT_USERNAME']]){
          script{
           // Do some git operations...
          }
       }
  }

If I remove the withCredentials from here the code works as fine, but I have to set the username hard coded in the git push commands. I want to fetch the username from the credentials instead. but my jenkins always throws a NPE without any other message.

I'm totally lost in this

Try the below and see if it helps. You can then refer username from credentials using GIT_CREDENTIALS_USR and password using GIT_CREDENTIALS_PSW

stage('Some Stage'){

      environment {
                GIT_CREDENTIALS = credentials('<credentialsID>')
      }
      steps{
         script{
           // Do some git operations...
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM