I have created a Multibranch pipeline on Jenkins 2.107.2. I want to perform Git commands like git commit
, git push
etc on the cloned repository.
To authenticate to the GitHub repository, I have configured my user credentials in Jenkins using (https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin). I have tried few approaches to use these credentials to authenticate but they result in different errors.
First Approach
stage('clone'){
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/develop']],
userRemoteConfigs: [[url: 'https://github.com:xyz/demo.git']]])
}
}
stage('prepare release'){
steps{
sh "sed -i 's/-SNAPSHOT//g' pom.xml"
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'jenkins-user-for-demo-github', usernameVariable: 'GITHUB_DEMO_CREDENTIALS_USR', passwordVariable: 'GITHUB_DEMO_CREDENTIALS_PSW']]) {
sh "git add pom.xml"
sh "git commit -m 'release version set"
}
}
}
Error
*** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name"
Second Approach
stage('Checkout') {
steps{
git branch: 'develop', credentialsId: 'jenkins-user-for-demo-github', url: '[email protected]:xyz/demo.git'
}
}
stage('prepare release'){
steps{
sh "sed -i 's/-SNAPSHOT//g' pom.xml"
sh "git add pom.xml"
sh "git commit -m 'release version set"
}
}
Error
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress [email protected]:AbhayChandel/New-project.git +refs/heads/:refs/remotes/origin/" returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Struggling with these errors I have following questions:
- Is it possible to pass Username & Password credentials for authentication to Github repository from Jenkinsfile?
- Should I use SSH key instead of Username & Password?
- Is there any other approach I should try(but not too hackish)?