简体   繁体   English

设置 Jenkins 时权限被拒绝(公钥)

[英]Permission denied (publickey) when setting up Jenkins

I am setting up Jenkins on a Win 2008 server machine and am having some trouble configuring Jenkins to connect to GitHub.我在 Win 2008 服务器机器上设置 Jenkins 并且在配置 Jenkins 以连接到 GitHub 时遇到了一些问题。 I get the following error:我收到以下错误:

Command "git.exe fetch -t git@github.com:USER/REPO.git +refs/heads/*:refs/remotes/origin/*" returned status code 128: Permission denied (publickey).
fatal: The remote end hung up unexpectedly

ERROR: Could not fetch from any repository
FATAL: Could not fetch from any repository
hudson.plugins.git.GitException: Could not fetch from any repository
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:950)
    at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:908)
    at hudson.FilePath.act(FilePath.java:758)
    at hudson.FilePath.act(FilePath.java:740)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:908)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1184)
    at hudson.model.AbstractBuild$AbstractRunner.checkout(AbstractBuild.java:537)
    at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:425)
    at hudson.model.Run.run(Run.java:1376)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:175)

Things I Have Checked我检查过的东西

  • The Jenkins service is running under my username. Jenkins 服务正在我的用户名下运行。
  • I changed my SSH key so it no longer has a password.我更改了我的 SSH 密钥,因此它不再有密码。
  • I have verified my SSH key is valid by executing the same command as Jenkins using msysgit.我已经通过使用 msysgit 执行与 Jenkins 相同的命令来验证我的 SSH 密钥是否有效。
  • Verified all my paths are correct.验证我所有的路径都是正确的。

Jenkins Configuration Settings Jenkins 配置设置

  • Jenkins 1.418 Jenkins 1.418
  • GitHub plugin 0.5 GitHub 插件 0.5
  • Git plugin 1.1.9 Git 插件 1.1.9

msysgit Works but CMD Prompt Does Not When I navigate to a valid repo and execute the command from the DOS prompt, it also fails. msysgit 工作,但 CMD 提示不起作用当我导航到有效的存储库并从 DOS 提示符执行命令时,它也失败了。

Do you have any idea what I may be missing?你知道我可能会错过什么吗?

Thanks for your help.谢谢你的帮助。

As I commented, specifying the HOME environment variable is key, when using ssh protocol.正如我所评论的,在使用 ssh 协议时,指定HOME环境变量是关键。
Since Windows doesn't have a HOME , you need to define it explicitly, to whatever directory you want.由于 Windows 没有HOME ,因此您需要将其明确定义到您想要的任何目录。

However, Vestnik comments:然而, Vestnik评论说:

I've specified to override HOME on the windows slave node to point it to C:\jenkins .我已指定覆盖 windows 从节点上的HOME以将其指向C:\jenkins
I've put correct id_rsa under the C:\jenkins\.ssh but still have this issue.我已将正确的id_rsa放在C:\jenkins\.ssh下,但仍然存在此问题。
My slave agent running as service under SYSTEM account.我的从代理在 SYSTEM 帐户下作为服务运行。

Two advices:两个建议:

  • you need to make your slave display ' set ' in order to check if HOME is defined when used with the SYSTEM account.你需要让你的slave显示' set ',以检查在使用SYSTEM帐户时是否定义了HOME。
    If it is not, that may mean you need to add that variable to the "system environment variables", not the "user environment variables".如果不是,那可能意味着您需要将该变量添加到“系统环境变量”,而不是“用户环境变量”。

显示作业定义中的环境

  • Don't forget to have both id_rsa and id_rsa.pub in the %HOME%\.ssh directory: you need both public and private ssh keys.不要忘记在%HOME%\.ssh目录中同时拥有id_rsaid_rsa.pub :您需要公共和私有 ssh 密钥。 (as mentioned in " git clone with ssh issue ") (如“ git clone with ssh issue ”中所述)

If you have a parametrized build, you also can define HOME that way and check if your slave picks up the right value for HOME :如果你有一个参数化的构建,你也可以这样定义HOME并检查你的从属是否为HOME选择了正确的值:

定义自定义变量

There are two plugins in jenkins related to ssh that can be used: Publish Over SSH and Jenkins SSH plugin . There are two plugins in jenkins related to ssh that can be used: Publish Over SSH and Jenkins SSH plugin .

The first plugin gives the availability to set a global ssh key and the second plugin gives the availability to set various ssh keys.第一个插件可以设置全局 ssh 密钥,第二个插件可以设置各种 ssh 密钥。

Next you need to install two more plugins that will be used in the project configuration GitHub Plugin and Jenkins Git Plugin .接下来,您需要安装另外两个插件,它们将在项目配置GitHub PluginJenkins Git Plugin中使用。

The GitHub plugin will be used to set the GitHub project . GitHub 插件将用于设置GitHub 项目 The Jenkins Git plugin will be used to set the "URL of repository" and other things like a branch etc. Jenkins Git 插件将用于设置“存储库的 URL”以及分支等其他内容。

All the plugins are available from the tab available in the Jenkins Plugin Manager.所有插件都可以从 Jenkins 插件管理器中的选项卡中获得。

Copied from my blog post, on the topic :我的博客文章中复制,关于主题

Jenkins configuration: Jenkins 配置:
The Jenkins SSH plugin give the availability to set private key per host, the second plugin do the job for a global host. Jenkins SSH 插件提供了为每个主机设置私钥的可用性,第二个插件为全局主机完成这项工作。
If Jenkins SSH plugin is used, then in SSH remote hosts write the host, user, passphrase and the path to the private key.如果使用 Jenkins SSH 插件,则在 SSH 远程主机中写入主机、用户、密码和私钥的路径。
If Publish Over SSH is used, then in SSH setting write the passphrase and paste the private key or write the path to it.如果使用 Publish Over SSH,则在 SSH 设置中写入密码并粘贴私钥或写入路径。
The project configuration is:项目配置为:
GitHub Project https://github.com/GitUser/iOS-project/ GitHub 项目https://github.com/GitUser/iOS-project/
*Source Code Management *源代码管理
-> Git -> Git
-> -> Repositories -> -> 存储库
-> -> -> URL of repository: git@github.com:GitUser/iOS-project.git -> -> -> 存储库的 URL:git@github.com:GitUser/iOS-project.git
-- It is up to you configure a branch or take the default one -- -- 由您配置一个分支或采用默认分支 --
*Build Triggers *构建触发器
-> Poll SCM -- Seted -- -> 轮询 SCM -- 已设置 --
-> Schedule: * * * * * -> 时间表:* * * * *
*Build *建造
-> Executed Shell -> 执行 Shell

-> -> Command:  xcodebuild -target iOS-project -configuration AdHoc -sdk iphoneos5.0 clean
-> -> Command:  agvtool new-version -all $BUILD_NUMBER 
-> -> Command:  xcodebuild -target iOS-project -configuration AdHoc -sdk iphoneos5.0
-> -> Command:  xcrun -sdk iphoneos5.0 PackageApplication -v $WORKSPACE/build/AdHoc-iphoneos/iOS-project.app -o  $WORKSPACE/build/AdHoc-iphoneos/iOS-project-$BUILD_NUMBER.ipa PROVISIONING_PROFILE="<provisioning profile>" 
-> -> Command:  curl http://testflightapp.com/api/builds.json -F file=@$WORKSPACE/build/AdHoc-iphoneos/iCushion-1.0-$BUILD_NUMBER.ipa -F api_token=<api_token> -F team_token=<team_token> -F notes="This is an autodeploy build from Jenkins!" -F notify=True -F distribution_lists="<distributedlist 1>, <distributedlist 2>"

It appears the git plugin now provides a way to point Jenkins to the home directory.看来 git 插件现在提供了一种将 Jenkins 指向主目录的方法。

  1. Click the credential add button.单击凭据添加按钮。

在此处输入图像描述

  1. Point Jenkins to your private key.将 Jenkins 指向您的私钥。

在此处输入图像描述

  1. Select the newly-added credentials from the list. Select 列表中新添加的凭据。

在此处输入图像描述

In my environment (Jenkins 64-bit with Java 32-bit, running in a Windows Server 2016 64-bit), the solution was put the key in the C:\Windows\SysWOW64\config\systemprofile\.ssh folder. In my environment (Jenkins 64-bit with Java 32-bit, running in a Windows Server 2016 64-bit), the solution was put the key in the C:\Windows\SysWOW64\config\systemprofile\.ssh folder. According to the Git Plugin documentation :根据Git 插件文档

By default, the Jenkins Windows installer sets up Jenkins to run as a service on Windows, which runs as the “Local System account”, NOT your user account.默认情况下,Jenkins Windows 安装程序将 Jenkins 设置为在 ZAEA23489CE3AA9B6406EBB28E0CDA4 上作为服务运行,而不是作为“本地系统帐户”运行的“本地系统帐户”。 Since the “Local System account” does not have SSH keys or known_hosts set up, “git clone” will hang during the build.由于“本地系统帐户”没有 SSH 密钥或 known_hosts 设置,“git clone”将在构建期间挂起。 It's possible to keep Jenkins running as the “Local System account” and clone repositories via SSH by making sure that the “Local System account” is set up with a properly configured.ssh directory (ie id_rsa, id_rsa.pub, AND known_hosts) It's possible to keep Jenkins running as the “Local System account” and clone repositories via SSH by making sure that the “Local System account” is set up with a properly configured.ssh directory (ie id_rsa, id_rsa.pub, AND known_hosts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM