How to use Jenkins CI with private GitHub repositories

Updated: February 4, 2024 By: Guest Contributor Post a comment

Introduction

Continuous Integration (CI) has revolutionized the way we develop software, allowing teams to integrate their work frequently, detect errors quickly, and improve quality. Jenkins, an open-source automation server, plays a pivotal role in implementing CI, providing hundreds of plugins to support building, deploying, and automating projects. In this tutorial, we delve into how you can leverage Jenkins CI with private GitHub repositories to secure your source code while enjoying the benefits of CI.

Prerequisites

  • A private GitHub repository.
  • A Jenkins server instance.
  • Knowledge of basic Jenkins and GitHub operations.

Step-by-Step Instructions

Step 1: Generate a Personal Access Token in GitHub

To connect Jenkins to your private GitHub repository, the first step is to generate a Personal Access Token (PAT) in GitHub with the appropriate scopes. This token will substitute for your password, reducing the security risks. To generate one:

  1. Navigate to your GitHub settings.
  2. Go to Developer settings > Personal access tokens > Generate new token.
  3. Select the scopes or permissions you’d like the token to have. For Jenkins integration, selecting ‘repo’ is usually sufficient.
  4. Click ‘Generate token’ at the bottom of the page.

Ensure you copy your new personal access token now, as GitHub won’t show it again.

Step 2: Configure Jenkins to Use the Personal Access Token

Now that you have your personal access token, the next step is to configure Jenkins to use this token for accessing your private GitHub repository:

  1. In Jenkins, navigate to Manage Jenkins > Manage Credentials.
  2. Choose the domain (e.g., global) where you want to add the credential.
  3. Click on ‘Add Credentials’.
  4. Select the credential type ‘Username with password’.
  5. For the username, enter your GitHub username, and for the password, enter the personal access token you created.
  6. Save your new credential.

Step 3: Create a New Jenkins Job

With your GitHub integration ready, it’s time to create a new Jenkins job:

  1. From the Jenkins dashboard, select ‘New Item.’
  2. Enter a name for your job, select ‘Freestyle project,’ and click ‘OK’.
  3. In the ‘Source Code Management’ section, select ‘Git.’
  4. Enter the repository URL of your private GitHub repository.
  5. In the Credentials dropdown, select the credential you added previously.
  6. Configure other settings as per your project’s requirements and click ‘Save’.

Step 4: Triggering Builds Automatically

To fully harness the power of CI, you’ll want to automate triggering builds upon new commits. Here’s how you can set up GitHub Webhooks:

  1. In your GitHub repository settings, navigate to Webhooks > Add webhook.
  2. Enter the Payload URL as http://your_jenkins_server/github-webhook/. Adjust this URL according to your Jenkins server’s setup.
  3. Select ‘Just the push event.’
  4. Ensure the Content type is set to ‘application/json.’
  5. Click ‘Add webhook.’

This configuration means that every push to your GitHub repository will now automatically trigger a Jenkins build, ensuring immediate feedback on integrations.

Step 5: Securing Your Jenkins and Repository Connection

Security is paramount when linking your Jenkins server to a private GitHub repository. Ensure to:

  • Always use HTTPS or a VPN to access your Jenkins server.
  • Regularly update your personal access token.
  • Restrict Jenkins’ global access and establish proper user permissions within Jenkins.

Conclusion

Integrating Jenkins CI with private GitHub repositories enhances your software development cycle by automating builds and tests. This setup, when done correctly, not only secures your code but also streamlines development processes, enabling faster release cycles. By following the steps outlined in this guide, you’ll be well-equipped to leverage the power of Jenkins CI with your private GitHub repositories. Embrace automation, secure your workflow, and achieve continuous integration success.