【问题标题】:Authenticating to Github reppository with Username and Password credentials in Jenkins pipeline在 Jenkins 管道中使用用户名和密码凭据对 Github 存储库进行身份验证
【发布时间】:2019-03-11 18:29:10
【问题描述】:

我在 Jenkins 2.107.2 上创建了一个多分支管道。我想在克隆的存储库上执行git commitgit push 等 Git 命令。

为了对 GitHub 存储库进行身份验证,我使用 (https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin) 在 Jenkins 中配置了我的用户凭据。我尝试了几种方法来使用这些凭据进行身份验证,但它们会导致不同的错误。

第一种方法

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

错误

*** 请告诉我你是谁。 跑步 git config --global user.email "you@example.com" git config --global user.name "你的名字"

第二种方法

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

错误

引起:hudson.plugins.git.GitException:命令“git fetch --tags --progress git@github.com:AbhayChandel/New-project.git +refs/heads/:refs/remotes/ origin/”返回状态码 128: 标准输出: 标准错误:主机密钥验证失败。 致命:无法从远程存储库中读取。 请确保您拥有正确的访问权限 并且存储库存在。

与这些错误作斗争我有以下问题:

  1. 是否可以将用户名和密码凭据从 Jenkinsfile 传递到 Github 存储库进行身份验证?
  2. 我应该使用 SSH 密钥而不是用户名和密码吗?
  3. 我还应该尝试其他方法吗(但不要太老套)?

【问题讨论】:

    标签: git authentication github jenkins-pipeline


    【解决方案1】:

    “凭据”仅用于与 Git 服务器通信(例如git clonegit fetchgit push),但不需要用于操作本地存储库。因此,在您的第一个示例中,git not 抱怨 credentials 而是抱怨在执行 git commit 时缺少 configuration。您可以像这样在这里设置任意内容:

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

    之后git commit 应该可以工作了。除非您想推送提交,否则您可以删除 withCredentials 包装器。

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2021-12-14
      • 2016-11-09
      相关资源
      最近更新 更多