Authenticating to Github reppository with Username and Password credentials in Jenkins pipeline

自作多情 提交于 2021-02-11 15:37:34

问题


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 "you@example.com" git config --global user.name "Your Name"

Second Approach

stage('Checkout') {
        steps{
            git branch: 'develop', credentialsId: 'jenkins-user-for-demo-github', url: 'git@github.com: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 git@github.com: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:

  1. Is it possible to pass Username & Password credentials for authentication to Github repository from Jenkinsfile?
  2. Should I use SSH key instead of Username & Password?
  3. Is there any other approach I should try(but not too hackish)?

回答1:


"Credentials" are only required for talking to the Git server (e.g. git clone, git fetch, git push) but NOT for manipulating the local repository. So in your first example git does not complain about credentials but about missing configuration when doing git commit. You can set arbitrary stuff here like this:

stage('clone'){
    steps{
      checkout([$class: 'GitSCM', branches: [[name: '*/develop']], userRemoteConfigs: [[url: 'https://github.com:xyz/demo.git']]])
      sh "git config user.name 'John Doe'"
      sh "git config user.email 'foo@bar.bla'"
    }
}

After that the git commit should work. And unless you want to push the commit you can remove the withCredentials wrapper.



来源:https://stackoverflow.com/questions/55108199/authenticating-to-github-reppository-with-username-and-password-credentials-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!