How to use Github Personal Access Token in Jenkins How to use Github Personal Access Token in Jenkins jenkins jenkins

How to use Github Personal Access Token in Jenkins


[UPDATE]The new solution proposed by git is

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

Which says:

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

What you need to do:

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

Basically, change the add URL as

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

Something like this

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

and set the credentials to none.

Thanks to @Gil Stal


[OLD Technique]

After many discussion on multiple threads from Stackoverflow

I found one thread that is useful.

Refer to this answer:https://stackoverflow.com/a/61104603/5108695


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

Go to Jenkins

  • 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.

  • source code management tab, select new configured credentials from Drop-down near credential Under Repository URL

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

References:
Git Clone in Jenkins with Personal Access Token idles forever
Change jenkins pipeline to use github instead of gitlab


The accepted answer wont work anymore because of this: https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations.

You will need to:

  1. Change the URL of the repo to: https://<access token>@github.com/<user-name>/<repo-name>.git (Replace every <...> with the real parameters)

  2. Set the credentials to none.


As for credentials for Jenkins Github Plugin, please be aware only Personal access tokens are now accepted by this plugin.

To generate such a token, follow the Github docs (e.g. here). Don't save it, it can be regenerated in Github and updated in Jenkins if lost or when migrating to a different server.

To add the token do Jenkins credentials store, go to <JENKINS_URL:PORT>/credentials/store/system/domain/_/newCredentials and select Kind "Secret text" (not the default "Username and password"), then paste the token as Secret and choose some ID.

Testing: the credential should appear on the list of Credentials at <JENKINS_URL:PORT>/credentials/ and be selectable from the drop-down list at <JENKINS_URL:PORT>/configure/, where pressing the "Test connection" button should display "Credentials verified for user <GITHUB_USER>".

More info: see the Github plugin docs.