来自Gitlab的结帐在Jenkins管道中失败了

时间:2019-05-17 10:10:16

标签: jenkins gitlab jenkins-pipeline

我已经阅读了许多文档,但无法解决此问题。

我使用下面的代码从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://username:c4cc893d00cfc8865fc3@10.50.9.XXX:8080/project/wildfly_gitlab

我测试了连接是否正常。同样,我也在Jenkins中配置Gitlab

Jenkins configuration

连接也正常工作。我不知道为什么我仍然会收到错误消息。

1 个答案:

答案 0 :(得分:0)

好像您在为GitLab而不是git使用credental

credentialsId: 'gitlab-jenkins-user-credentials',  

在GitLab上配置访问令牌,并将其添加为Jenkins上的凭据。 此新凭据应用于通过git拉代码。

参考this ,了解如何使用您要用于构建MR的GitLabConnection凭据(与git凭据不同)。

通过链接:

// Reference the GitLab connection name from your Jenkins Global configuration (http://JENKINS_URL/configure, GitLab section)
properties([gitLabConnection('your-gitlab-connection-name')])

node {
  checkout scm // Jenkins will clone the appropriate git branch, no env vars needed

  // Further build steps happen here
}