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

标签 git jenkins

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

在我的命令行中,命令

git clone https://user:token@github.exampleco.com/org/repo.git

克隆存储库没问题,大约需要 5-10 秒。

在 Jenkins 中,控制台输出显示“cloning into directory-name”,然后有一个旋转的轮子不停地旋转,这永远不会解决。

我在执行 shell 脚本中运行它,因为 github 插件运行一些显然仍然想要进行密码身份验证的命令,即使我向它提供没有凭据的 url 的 PAT 版本,我也没有看到添加凭据模式中的 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

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

最佳答案

我找到了一个更简单的方法来做到这一点,

请引用这个答案 https://stackoverflow.com/a/61104238/5108695


经过一番激烈的谷歌搜索后,我找到了答案,事实证明这比我想的要容易得多:

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

这样我就可以像以前一样从下 pull 列表中选择凭据,并且项目被毫无问题地克隆


关于带有个人访问 token 的 Jenkins 中的 Git 克隆永远闲置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809684/

相关文章:

git - Git 如何查看一个分支上的所有文件变化

Git: "The branch you are about to push seems to be a new branch for the remote"警告

c# - 使用 CI 工具自动部署

jenkins - 自动设置 JENKINS 2.32.1 服务器(使用脚本)

jenkins - 列出脚本化的Jenkins Pipeline中正在使用的插件

ruby - 是否可以在不先获取远程 git repo 信息的情况下获取它?

git - 如何使用嵌入式 GIT 重置 GIT

jenkins - 我们可以在 Jenkinsfile 的 'when' 中应用 'stages' 条件吗?

python - Git 预提交 Hook ,自动授予所有正在提交的 .sh 文件的执行权限 (+x)

maven - 如何使用 Gulp、Maven 和 Jenkins 组织完整的构建管道,一直到集成测试?