首页 > 解决方案 > 来自 Gitlab 的结帐在 Jenkins 管道中失败

问题描述

我已阅读大量文档,但无法解决此问题。

我使用下面的代码从 jenkinfile 中的 gitlab 结帐

checkout changelog: true, poll: true, scm: [
      $class: 'GitSCM',
      branches: [[name: "origin/${env.gitlabSourceBranch}"]],
      doGenerateSubmoduleConfigurations: false,
      extensions: [[
        $class: 'PreBuildMerge',
        options: [
          fastForwardMode: 'FF',
          mergeRemote: 'origin',
          mergeStrategy: 'default',
          mergeTarget: "${env.gitlabTargetBranch}"
        ]
      ]],
      submoduleCfg: [],
      userRemoteConfigs: [[
        credentialsId: 'gitlab-jenkins-user-credentials',  
        name: 'origin',
        url: "${env.gitlabSourceRepoHttpUrl}"
      ]]
    ]

我什至试过

checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-jenkins-user-credentials', url: 'http://jenkins-master/testproject/devops_artifacts/test_devops.git']]]

但我不断收到此错误:

    Warning: CredentialId "gitlab-jenkins-user-credentials" could not be found.
Cloning the remote Git repository
Cloning repository http://gitlab-master.com/testproject/devops_artifacts/test_devops.git
 > C:\Program Files\Git\cmd\git.exe init C:\jenkins-docker\workspace\wildfly_gitlab # timeout=10
Fetching upstream changes from http://gitlab-master.com/testproject/devops_artifacts/test_devops.git
 > C:\Program Files\Git\cmd\git.exe --version # timeout=10
 > C:\Program Files\Git\cmd\git.exe fetch --tags --force --progress http://gitlab-master.com/testproject/devops_artifacts/test_devops.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "C:\Program Files\Git\cmd\git.exe fetch --tags --force --progress http://gitlab-master.com/testproject/devops_artifacts/test_devops.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: Logon failed, use ctrl+c to cancel basic credential prompt.
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'http://gitlab-master.com/testproject/devops_artifacts/test_devops.git/'

我已经以这种形式设置了 webhook

http://用户名:c4cc893d00cfc8865fc3@10.50.9.XXX:8080/project/wildfly_gitlab

我测试了它正在工作的连接。我也在 Jenkins 中配置了 Gitlab

詹金斯配置

连接也有效。我不知道为什么我仍然收到错误。

标签: jenkinsgitlabjenkins-pipeline

解决方案


我假设您的“gitlab-jenkins-user-credentials”是用户凭据(凭据范围为特定用户)。

withCredentials您可以通过获取用户凭据然后在配置中使用带有凭据 url 的 https来规避userRemoteConfigs

    parameters {
    credentials(credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl', defaultValue: '', description: 'Jenkins wants to use the "gitlab-jenkins-user-credentials" credentials. Select your credential to allow this.', name: 'gitlab-jenkins-user-credentials', required: true)
}
...

    withCredentials([usernamePassword(credentialsId: "gitlab-jenkins-user-credentials", passwordVariable: 'GITLAB_PASSWORD', usernameVariable: 'GITLAB_USER')]) {
       checkout([$class: 'GitSCM', branches: [[name: 'refs/tags/mytag']], userRemoteConfigs: [[url: "https://${GITLAB_USER}:${GITLAB_PASSWORD}@gitlab.com/myproject.git"]]]) 
    }

gitlab-jenkins-user-credentials 是“用户名和密码”类型的用户凭证,创建的凭证是:

  • id: gitlab-jenkins-user-credentials
  • 用户名 = gitlab 令牌名称
  • 密码 = gitlab 令牌

运行作业时,将提示用户选择其用户凭据。

但是请注意,从安全的角度来看,对秘密进行常规插值并不是一个好的做法(请参阅https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation)。


推荐阅读