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

How to use Github Personal Access Token in Jenkins

我可以通过多种方式提出这个问题,比如 如何使用 Github 个人访问令牌配置 Jenkins 凭据 如何使用 Github 个人访问令牌

在 Jenkins 中克隆 Github 回购协议

这就是问题所在


我知道的替代解决方案

但我的问题是如何使用 Personal Access Token

设置与 Jenkins 的 Github 连接

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

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

其中说:

Beginning August 13, 2021, we will no longer accept account passwords when authenticating Git operations and will require the use of token-based authentication, such as a personal access token (for developers) or an OAuth or GitHub App installation token (for integrators) for all authenticated Git operations on GitHub.com. You may also continue using SSH keys where you prefer.

您需要做的事情:

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

并将凭据设置为 none。

感谢@Gil Stal


[旧技术]

经过 Whosebug 对多线程的多次讨论

我找到了一个有用的线程。

参考这个回答:


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

转到Jenkins

  • 转到 credentials > System > Global credentials > Add credentials 将打开一个页面。
  • 种类下拉select用户名和密码.
  • 在用户中输入一个不存在的用户名,例如 jenkins-useruser
  • 在密码字段中添加Personal Access Token

现在开始配置您的项目。

  • 源代码管理 选项卡,select 新配置的凭据来自 credential 下的下拉菜单存储库 URL

这就是我们如何使用个人访问令牌

在 Jenkins 和 Github 之间配置或设置身份验证

参考文献:

至于 Jenkins Github Plugin 的凭据,请注意此插件现在只接受 Personal access tokens

要生成这样的令牌,请遵循 Github 文档(例如 here)。不要保存它,它可以在 Github 中重新生成并在 Jenkins 中更新,如果丢失或迁移到不同的服务器。

要添加 Jenkins 凭据存储的令牌,请转到 <JENKINS_URL:PORT>/credentials/store/system/domain/_/newCredentials 和 select 键入“机密文本”(不是默认的“用户名和密码”),然后将令牌粘贴为 Secret 和选择一些ID。

测试:凭据应出现在 <JENKINS_URL:PORT>/credentials/ 的凭据列表中,并且 select 可以从 <JENKINS_URL:PORT>/configure/ 的下拉列表中按“测试连接”按钮应显示“已为用户 验证凭据”。

更多信息:查看 Github 插件 docs

已接受的答案将不再有效,因为: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(将每个 <...> 替换为实际参数)

  2. 将凭据设置为 none。

截至 2021 年 8 月,Dupinder Singh 发布的答案是准确的。我唯一要补充的是,如果您是团队的一员,url 格式似乎有点不同。这对我有用:

https://<access token>@github.com/<team>/<repo>.git

例如

https://ghp_6dh3jdk394jsmbh299jjdg20fh87hd83ksk39@github.com/MyKuleTeam/KuleGuyCode.git

请注意,如果您使用个人访问令牌,则不需要在 jenkins 中存储任何 github 凭据。

从 2020/04 开始,有(还有另一种)方法可以做到这一点,它应该优于个人访问令牌。最好的部分是您可以继续使用 username/password-style 凭据,插件将在后台使用 GitHub 进行身份验证。

好处包括:

  • 更大的速率限制 - GitHub 应用程序的速率限制与您的组织规模成比例,而基于用户的令牌的限制为5000,不管你有多少仓库。

  • 独立于用户的身份验证 - 每个 GitHub 应用程序都有自己的独立于用户的身份验证。不再需要 'bot' 用户或弄清楚谁应该是 2FA 或 OAuth 令牌的所有者。

  • 改进的安全性和更严格的权限 - GitHub 与服务用户及其个人访问令牌相比,应用程序提供更细粒度的权限。这让 Jenkins GitHub 应用程序需要更小的权限集才能正确 运行。

  • 访问 GitHub 检查 API - GitHub 应用程序可以访问 GitHub 检查API 从 Jenkins 作业创建检查 运行 和检查套件,并提供有关提交和代码注释的详细反馈

链接: