如何在 Jenkins 中使用 Github 个人访问令牌

Dup*_*ngh 25 authentication github jenkins jenkins-github-plugin

我可以通过多种方式提出这个问题,例如如何使用 Github 个人访问令牌配置 Jenkins 凭据如何使用 Github 个人访问令牌在 Jenkins 中克隆 Github 存储库

所以这就是问题所在


我知道的替代解决方案

但我的问题是如何使用 Jenkins 设置 Github 连接 Personal Access Token

Dup*_*ngh 47

[更新] git 提出的新解决方案是

https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/

其中说:

从 2021 年 8 月 13 日开始,我们将在对 Git 操作进行身份验证时不再接受帐户密码,并将要求使用基于令牌的身份验证,例如个人访问令牌(针对开发人员)或 OAuth 或 GitHub 应用程序安装令牌(针对集成商) GitHub.com 上所有经过身份验证的 Git 操作。您也可以继续在您喜欢的地方使用 SSH 密钥。

你需要做什么:

https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/#what-you-need-to-do-today

基本上,将添加 URL 更改为

https://<access token>@github.com/<userName>/<repository>.git

像这样的东西

https://<access token>@github.com/dupinder/NgnixDockerizedDevEnv.git

并将凭据设置为无。

感谢@Gil Stal


[旧技术]

在对 Stackoverflow 中的多个线程进行多次讨论之后

我发现了一个有用的线程。

参考这个答案:https : //stackoverflow.com/a/61104603/5108695


基本上 Personal access token可以作为密码使用,Jenkins至少就目前而言。我向凭证管理器添加了新凭证。

Jenkins

  • 转到credentials> System> Global credentials>Add credentials将打开一个页面。
  • Kind下拉列表中选择Username 和 password
  • 在 User 中输入一个不存在的用户名,例如jenkins-useror user
  • Personal Access Token在密码字段中添加

现在开始配置您的项目。

  • 源代码管理选项卡,选择新从下拉配置近凭证的凭证仓库URL

这就是我们如何使用个人访问令牌在 Jenkins 和 Github 之间配置或设置身份验证

参考:
Jenkins 中的 Git Clone with Personal Access Tokens永远空闲
更改 jenkins 管道以使用 github 而不是 gitlab

  • 这种方法有两个问题:1) 现在,只要写入 URL,PAT 就会写入日志中,2) 必须将 PAT 复制到需要它的每个作业的 URL 中。这些不良的副作用使得这个答案不受欢迎。 (4认同)
  • 请注意不要复制个人访问令牌末尾的空格。 (3认同)
  • 在 GitHub 中,正确设置个人访问令牌的权限非常重要。例如,您可能需要在 GitHub 中为您的令牌打开“repo”权限 (3认同)
  • 我发现这是在 Jenkins CI/CD 作业中使用个人访问令牌的最简单方法。 (2认同)
  • 看起来这不再起作用了 (2认同)

Gil*_*tal 10

由于以下原因,接受的答案不再有效:https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations

您将需要:

  1. 将 repo 的 URL 更改为:(https://<access token>@github.com/<user-name>/<repo-name>.git将 every 替换<...>为真实参数)

  2. 将凭据设置为无。