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

问题描述:

我可以通过多种方式提出这个问题,比如如何使用 Github 个人访问令牌配置 Jenkins 凭据如何使用 Github Personal Access Token 在 Jenkins 中克隆 Github repo

I can ask this question in many ways, like How to configure Jenkins credentials with Github Personal Access Token How to clone Github repo in Jenkins using Github Personal Access Token

这就是问题

我知道的替代解决方案

  • SSH connection
  • username password configuration in Jenkins. However, use of a password with the GitHub API is now deprecated.

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

But My question is how to setup Github connection with Jenkins using Personal Access Token

[UPDATE]git提出的新方案是

[UPDATE] The new solution proposed by git is

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

说的是:

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

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 更改为

Basically, change the add URL as

https://@github.com//.git

类似的东西

https://@github.com/dupinder/NgnixDockerizedDevEnv.git

并将凭据设置为无.

感谢@Gil Stal

Thanks to @Gil Stal

[旧技术]

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

After many discussion on multiple threads from *

我发现了一个有用的话题.

I found one thread that is useful.

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

基本上个人访问令牌可以用作密码,至少对于Jenkins而言.我向凭证管理器添加了新凭证.

Basically Personal access token can be used as a password, as far as Jenkins is concerned at least. I added new credentials to the credential manager.

转到Jenkins

  • 转到凭据 >系统 >全局凭证 >添加凭据将打开一个页面.
  • 种类下拉菜单中选择用户名和密码.
  • 在 User 中输入一个不存在的用户名,例如 jenkins-useruser.
  • 在密码字段中添加Personal Access Token
  • Go to credentials > System > Global credentials > Add credentials a page will open.
  • In Kind drop-down select Username and password.
  • In User put a non-existing username like jenkins-user or user.
  • Add Personal Access Token in the password field

现在开始配置您的项目.

Now start configuring your project.

  • 源代码管理选项卡,从凭据附近的下拉菜单中选择新配置的凭据,在存储库URL
  • source code management tab, select new configured credentials from Drop-down near credential Under Repository URL

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

So this is how we can configure or setup Authentication between Jenkins and Github using Personal Access Token

参考文献:
Jenkins 中的 Git Clone 和个人访问令牌永远空闲
更改 jenkins 管道以使用 github 而不是 gitlab