将 Jenkins 配置为使用 Git Bitbucket 存储库时出现身份验证错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20623879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:28:26  来源:igfitidea点击:

Authentication error configuring Jenkins to work with Git Bitbucket repo

gitjenkinsbitbucket

提问by user1004462

I am trying to configure Jenkins in a Windows 7 environment to work with Git repository on Bitbucket, but when I try to do a build - I get the error below.

我正在尝试在 Windows 7 环境中配置 Jenkins 以使用 Bitbucket 上的 Git 存储库,但是当我尝试进行构建时 - 我收到以下错误。

This is the Repository URL I am using:

这是我正在使用的存储库 URL:

https://<MY_ID>:<MY_PASSWORD>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git

Not sure if I need to generate keys for Jenkins-Bitbucket, if yes, can anyone please provide detailed instructions on how to do that for Windows?

不确定我是否需要为 Jenkins-Bitbucket 生成密钥,如果是,任何人都可以提供有关如何为 Windows 执行此操作的详细说明吗?

Started by user anonymous
Building in workspace C:\Users\<MY_NAME>\.jenkins\workspace\<MY_PROJECT>
Fetching changes from the remote Git repository
Fetching upstream changes from https://<MY_ID>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git
FATAL: Failed to fetch from https://<MY_ID>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git
hudson.plugins.git.GitException: Failed to fetch from https://<MY_ID>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:612)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:836)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:861)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1414)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:652)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:561)
    at hudson.model.Run.execute(Run.java:1678)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:231)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress https://<MY_ID>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git +refs/heads/*:refs/remotes/<MY_ID>/*" returned status code 128:
stdout: 
stderr: fatal: Authentication failed for 'https://<MY_ID>@bitbucket.org/<MY_ID>/<MY_REPO_NAME>.git/'

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1098)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:984)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access0(CliGitAPIImpl.java:68)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.execute(CliGitAPIImpl.java:217)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.fetch(CliGitAPIImpl.java:223)
    at hudson.plugins.git.GitAPI.fetch(GitAPI.java:229)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:610)
    ... 10 more

回答by user1004462

OK, here is what worked for me, but first, my set-up was as follows:

好的,这对我有用,但首先,我的设置如下:

  1. Windows 7
  2. Jenkins (with Git plugin)
  3. Git
  4. Account on Bitbucket
  1. Windows 7的
  2. 詹金斯(带有 Git 插件)
  3. 吉特
  4. Bitbucket 上的帐户

I started by following this tutorial. It works on Windows, all you have to do is double-click "Git Bash" in "C:\Program Files (x86)\Git" and follow the instruction to generate ssh keys. I only had to do Step 2 and Step 3.

我从遵循本教程开始。它适用于 Windows,您只需双击“C:\Program Files (x86)\Git”中的“Git Bash”,然后按照说明生成 ssh 密钥。我只需要执行第 2 步和第 3 步。

NOTE: I generated keys without a passphrase! This was the only way I could get it to work...

注意:我生成的密钥没有密码!这是我让它工作的唯一方法......

Then, open the public key file "id_rsa.pub" that was generated in "C:\Users\YOUR_WINDOWS_USER_NAME\.ssh" with a text editor and copy the content. Once you have the public key, you need to register it with Bitbucket, go to "Manage Account" -> "SSH Keys" -> "Add Key". Paste your public key and save.

然后,用文本编辑器打开在“C:\Users\YOUR_WINDOWS_USER_NAME\.ssh”中生成的公钥文件“id_rsa.pub”,复制内容。获得公钥后,您需要将其注册到 Bitbucket,转到“管理帐户”->“SSH 密钥”->“添加密钥”。粘贴您的公钥并保存。

To test the keys, I ran this command:

为了测试密钥,我运行了这个命令:

$ ssh -T [email protected]
logged in as <USER_ID>.

You can use git or hg to connect to Bitbucket. Shell access is disabled.

Now you need to register your private key with Jenkins: go to "Credentials" -> "Global credentials" -> "Add Credentials", enter your User Name, Description (optional), select "From a file on Jenkins master" and enter C:\Users\YOUR_WINDOWS_USER_NAME\.ssh\id_rsa.

现在您需要向 Jenkins 注册您的私钥:转到“凭据”->“全局凭据”->“添加凭据”,输入您的用户名、描述(可选),选择“来自 Jenkins master 上的文件”并输入C:\Users\YOUR_WINDOWS_USER_NAME\.ssh\id_rsa。

Last step is to configure Jenkins job. Under "Source Code Management", select "Git", under "Repository URL", enter:

最后一步是配置 Jenkins 作业。在“Source Code Management”下,选择“Git”,在“Repository URL”下,输入:

[email protected]:<USER_ID>/<REPO_NAME>.git

Under "Credentials", select credential that you've just created.

在“凭据”下,选择您刚刚创建的凭据。

NOTE: make sure that when you go to "Manage Jenkins" -> "Configure System" -> "Git", "Path to Git executable" is set to something like "C:\Program Files (x86)\Git\cmd\git.exe".

注意:确保当您转到“管理 Jenkins”->“配置系统”->“Git”时,“Git 可执行文件的路径”设置为类似“C:\Program Files (x86)\Git\cmd\” git.exe”。

Following these steps I was able to pull down my code from the repository.

按照这些步骤,我能够从存储库中提取我的代码。

回答by VonC

Not sure if I need to generate keys for Jenkins-Bitbucket

不确定是否需要为 Jenkins-Bitbucket 生成密钥

Keys? As in "ssh keys"? Certainly not if you are using an https url (as in this question). Login and password should be enough.

钥匙?就像在“ssh 密钥”中一样?如果您使用的是 https 网址(如本问题中所示),则肯定不会。登录名和密码应该足够了。

Double check your login/password and url: it is case sensitive.

仔细检查您的登录名/密码和网址:区分大小写。

Make sure your password don't have any special character (that you would need to percent-encode, as in this case).

确保您的密码没有任何特殊字符(您需要对其进行百分比编码,如本例所示)。



For using an ssh url, you can follow "Use the SSH protocol with Bitbucket" (after setting up ssh).
Make sure to modify the url of your git repo in your Jenkins job config to:

要使用 ssh url,您可以遵循“使用 Bitbucket 使用 SSH 协议”(设置 ssh 后)。
确保将 Jenkins 作业配置中 git repo 的 url 修改为:

[email protected]:accountname/reponame.git
# or
ssh://[email protected]/accountname/reponame.git

But, make sure Jenkins runs as the user where the ssh keys are stored: see "Jenkins finds cannot find ssh key" and this BitBucket thread. (Check also your firewall)

但是,请确保 Jenkins 以存储 ssh 密钥的用户身份运行:请参阅“ Jenkins finds not find ssh key”和这个BitBucket 线程。(还要检查您的防火墙