Jenkins中带有个人访问令牌的Git克隆永远闲置

Jenkins中带有个人访问令牌的Git克隆永远闲置,git,jenkins,Git,Jenkins,我们的企业github运行在公司网络之外的远程github服务器上,需要使用https代理进行克隆。我们不允许使用密码身份验证,所以可以使用ssh(由于代理问题而无法使用)或PAT 在我的命令行中 git clone https://user:token@github.exampleco.com/org/repo.git 克隆存储库没有问题,大约需要5-10秒 在Jenkins中,控制台输出读取“克隆到目录名”,然后有一个旋转的轮子无休止地旋转,这永远不会解决 我在一个execute shel

我们的企业github运行在公司网络之外的远程github服务器上,需要使用https代理进行克隆。我们不允许使用密码身份验证,所以可以使用ssh(由于代理问题而无法使用)或PAT

在我的命令行中

git clone https://user:token@github.exampleco.com/org/repo.git
克隆存储库没有问题,大约需要5-10秒

在Jenkins中,控制台输出读取“克隆到目录名”,然后有一个旋转的轮子无休止地旋转,这永远不会解决

我在一个execute shell脚本中运行这个脚本,因为github插件运行一些显然仍然希望进行密码验证的命令,即使我向它提供没有凭据的PAT版本的url,并且在add credentials模式中没有看到PAT授权选项

要澄清提供给Jenkins插件的url是:

https://user:token@github.exampleco.com/org/repo.git
我得到这样的输出:

No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://user:token@github.exampleco.com/org/repo.git # timeout=10
Fetching upstream changes from https://user@github.exampleco.com/org/repo.git
 > git --version # timeout=10
Setting http proxy: corporateproxy.com:8080
 > git fetch --tags --progress https://user@github.exampleco.com/org/repo.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://user@github.exampleco.com/org/repo.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:894)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1161)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1192)
    at hudson.scm.SCM.checkout(SCM.java:504)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1208)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:574)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:499)
    at hudson.model.Run.execute(Run.java:1818)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress https://user@github.exampleco.com/org/repo.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: Password authentication is not available for Git operations.
remote: You must use a personal access token or SSH key.
remote: See https://github.exampleco.com/settings/tokens or https://github.exampleco.com/settings/ssh
fatal: unable to access 'https://user@github.exampleco.com/org/repo.git': The requested URL returned error: 403

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2042)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1761)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$400(CliGitAPIImpl.java:72)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:442)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:892)
    ... 11 more
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE
我注意到,在配置远程源之后,user:token@被删除,取而代之的是user@。我尝试在shell中手动运行所有这些命令,当我使用第二个命令时,即:

git fetch --tags --progress https://user:token@github.exampleco.com/org/repo.git

它也像克隆命令一样永远闲置。

问题是我需要以自己的身份运行,而不是以詹金斯的身份运行。以用户jenkins的身份运行不允许我们克隆

我们可以通过将命令
whoami
放入executeshell构建步骤来检查我们正在作为谁运行。如果您的
whoami
与您的用户名不匹配(很可能
whoami
将返回jenkins作为用户名),我们需要进行一些配置

转到jenkins主页,选择管理jenkins,然后选择管理节点。创建一个新节点

为此新节点指定一个执行器,在用法中选择“仅使用与此节点匹配的标签表达式生成作业”,并为项目指定唯一的标签。在启动方法中选择“通过SSH启动代理”。使您的主机
localhost
,并添加您的凭据(用户名和密码)以登录您正在使用的计算机。选择不验证主机密钥,并在可用性中选择“尽可能保持此代理联机”,然后保存。你需要启动代理

现在去你的工作吧。在“常规”选项卡中的“配置”下,选中“限制此作业可以运行的位置”,然后输入该唯一标签。拯救您的作业现在应该以与命令行git完全相同的方式进行克隆


这在Mac电脑上可以使用,但我不确定windows是否可以,但我认为基本上是一样的。

我找到了一个更简单的方法来实现这一点

请参考这个答案


经过一番激烈的谷歌搜索,我找到了答案,结果证明比我想象的简单得多:

显然,至少就詹金斯而言,个人访问令牌可以用作密码。我向凭证管理器添加了新凭证,选择输入“用户名和密码”,输入一个不存在的用户名(“用户”),并将个人访问令牌放入密码字段

这样,我可以像以前一样从下拉列表中选择凭据,并且项目克隆时没有出现问题



你能用user:password代替user:token吗?那不行。不允许密码身份验证。我已经找到了解决方案,正在这里添加答案。我可以得到参考资料吗?因为你贴的答案有点不清楚