我们有一个企业github运行在公司网络之外的远程github服务器上,需要使用https代理来克隆。我们不允许使用密码身份验证,因此要么使用 ssh(由于代理问题而无法使用)或 PAT。
在我的命令行上,命令
git clone https://user:token@github.exampleco.com/org/repo.git
Run Code Online (Sandbox Code Playgroud)
克隆存储库没有问题,大约需要 5-10 秒。
在 Jenkins 中,控制台输出显示“克隆到目录名称”,然后有一个旋转轮无休止地旋转,并且永远无法解决。
我在执行 shell 脚本中运行它,因为 github 插件运行一些命令,显然仍然想要进行密码身份验证,即使我向它提供没有凭据的 PAT 版本的 url,并且我没有看到 PAT 授权选项在添加凭据模式中。
为了澄清给 Jenkins 插件的 url 是:
https://user:token@github.exampleco.com/org/repo.git
Run Code Online (Sandbox Code Playgroud)
我得到这样的输出:
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
Run Code Online (Sandbox Code Playgroud)
我确实注意到,在配置远程源之后, user:token@ 被删除,取而代之的是 user@ 。我尝试在 shell 中手动运行所有这些命令,当我到达第二个命令时,即:
git fetch --tags --progress https://user:token@github.exampleco.com/org/repo.git
Run Code Online (Sandbox Code Playgroud)
它也像克隆命令一样永远闲置。
我找到了一个更简单的选择来做到这一点,
请参考这个答案 /sf/answers/4277296691/
经过一番激烈的谷歌搜索后,我找到了答案,事实证明这比我想象的要容易得多:
显然,至少就 Jenkins 而言,个人访问令牌可以用作密码。我向凭证管理器添加了新凭证,选择类型“用户名和密码”,输入不存在的用户名(“用户”),并将个人访问令牌放入密码字段。
这样我就可以像以前一样从下拉列表中选择凭据,并且项目可以毫无问题地克隆