Integrate GitHub with Jenkins
Integrating GitHub with Jenkins automates your development process. Hereâs how to set it up:
Prerequisites
- Jenkins: Install and configure Jenkins.
- GitHub: Create a GitHub account and repository.
- Jenkins GitHub Plugin: Connect GitHub to Jenkins.
Steps to Integrate GitHub with Jenkins
1. Install GitHub Plugin in Jenkins
- Go to Jenkins Dashboard.
- Click on "Manage Jenkins".
- Click on "Manage Plugins".
-
Search for the following plugins in the Available tab:
- GitHub Plugin
- Git client
- GitHub API
- GitHub Authentication
- GitHub Branch Source
- GitHub Integration
- GitHub Issues
- Pipeline: GitHub
- Pipeline: GitHub Groovy Libraries
- Pipeline: Groovy
- Pipeline: Groovy Libraries
- Pipeline: Input Step
- Pipeline: Job
- Credentials Plugin
- OAuth Credentials
- Install the plugins and restart Jenkins if needed.
2. Configure GitHub Plugin in Jenkins
- Go to Jenkins Dashboard.
- Click on "Manage Jenkins".
- Click on "Configure System".
- Scroll to the "GitHub" section.
- Add your GitHub account with an OAuth token.
3. Create a Jenkins Job
- Go to Jenkins Dashboard.
- Click on "New Item".
- Enter the job name and select "Freestyle project".
- Click "OK".
4. Configure the Job to Use GitHub
- In the job configuration, go to the "Source Code Management" section.
- Select "Git".
- Enter your GitHub repository URL.
- Add credentials if needed.
5. Set Up Build Triggers
- Scroll to the "Build Triggers" section.
- Check "GitHub hook trigger for GITScm polling".
6. Configure GitHub Webhook
- Go to your GitHub repository.
- Click on "Settings".
- Click on "Webhooks".
- Click "Add webhook".
- Enter the Payload URL (your Jenkins URL + /github-webhook/).
- Select "application/json" as the content type.
- Choose events to trigger the webhook (e.g., "push").
- Save the webhook.
7. Test the Integration
- Make a change in your GitHub repository and push it.
- Check if the Jenkins job is triggered automatically.
- Verify the build status in Jenkins.
Example Pipeline
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git 'https://github.com/your-username/your-repository.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Run Tests') {
steps {
sh 'npm test'
}
}
stage('Build') {
steps {
sh 'npm build'
}
}
stage('Deploy') {
steps {
sh 'npm deploy'
}
}
}
}
Steps to Configure GitHub with Jenkins
Registering a GitHub App
To improve security and control, convert your GitHub repository into a GitHub App. See herefor more details.
- Navigate to your GitHub profile settings.
- Under "Developer settings", click GitHub Apps.
- Click the "New GitHub App" button and follow the prompts to create the app.
- Generate a private key and store it securely.
Set GitHub Permissions for Pipeline
Ensure your GitHub App has the necessary permissions to interact with Jenkins:
Repository Permissions:
- Contents: Set to Read & Write. This allows the pipeline to read and write files in the repository, essential for Jenkins to check out the code, make changes, and push updates if needed.
- Metadata: Set to Read-only. Necessary to access repository metadata, such as information about the repository.
- Issues: Set to Read & Write. If your pipeline automates issue management, such as creating or closing issues, this permission is required.
- Pull Requests: Set to Read & Write. This is necessary if the pipeline interacts with pull requests, such as triggering builds or adding comments.
- Commit Statuses: Set to Read & Write. This allows Jenkins to update the status of commits, indicating whether a build has passed or failed.
- Deployments: Set to Read & Write. If your Jenkins pipeline handles deployments, this permission allows the pipeline to create, modify, or view deployment statuses.
Organization Permissions:
- Members: Set to Read-only. If your Jenkins setup interacts with organization members, this permission might be needed.
Webhook Permissions:
- Events: Enable Push, Pull Request, and Issue Comment events. Ensure Jenkins is notified of relevant events like code pushes, pull requests, and issue comments, allowing it to trigger builds automatically.
Administration Permissions: (Use with caution)
- Repository Administration: Read & Write (Optional). Allows Jenkins to perform administrative tasks on the repository, such as renaming the repository or modifying settings.
Additional Considerations:
- Install the App: Ensure the GitHub App is installed on the correct repositories or the entire organization where your pipeline will be operating.
- Webhook Secret: If using webhooks, ensure that the webhook secret configured in GitHub matches the one configured in Jenkins.
Conclusion
Integrating GitHub with Jenkins automates your CI/CD process. Follow these steps to set up the integration and streamline your development workflow.