We have an Organization in GitHub, and a private Jenkins instance that checks out code from various repositories belonging to that Organization. Up to this point, I've used a Personal Access Token (PAT) created under my GitHub account as the credentials that Jenkins uses to clone the repos for builds. Since the PAT is associated to my account, there's a (obvious, to me) weak point there - all our Jenkins builds are coupled to my GitHub account.
I want to eliminate the dependency on my GitHub account (or any one developer's GitHub account), and instead use some kind of credentials that are associated to the Organization instead. But I can't find a way to create an access token for an Organization.
How can Jenkins be set up to be able to clone repos from an Organization?
You can accomplish this by using a GitHub App and its "client secret." To do this, you need to have admin rights in the GitHub Organization.
Go to the GitHub Organization settings - URL is like https://github.com/organizations/MyOrganization/settings/apps
Click the button to create a New GitHub App.
Fill out the required data for the app Name and URL (I just used the Organization page URL).
Un-check the Webhook > Active option.
In the Repository Permissions section, I selected the following:
Actions: Read & Write
Commit Status: Read & Write
Contents: Read-only
Packages: Read & Write (some of our jobs publish artifacts to GitHub Packages, yours may not need this).
Pull Requests: Read-only
Click the button to Create GitHub App.
Back on the App page, click the button to Generate a new client secret.
Also take note of the App ID (as of now, a 7-digit number).
Click the Install App link (left-side navigation), then install the app into your Organization account.
Now that you have the App ID and Client Secret, go to your Jenkins server and navigate to the Credentials page (Manage Jenkins > Credentials).
Click the Scope and Domain (System by default) and Domain ("global" by default) for the new credential, then Add Credentials.
On the New Credentials form, select GitHub App as the Kind.
For ID, leave it blank or make one up. This is NOT the GitHub App ID, this is the Jenkins credential ID and it will be generated if you don't enter one.
For App ID, enter the App ID you noted in step 8 above.
For Key, you'll use the client secret you copied in step 7.1 above.
Click the Test Connection button to make sure you copy/pasted everything correctly.
Click the Create button to add this credential.
This credential can now be selected for use in the Source Code Management of your Jenkins builds.