Overview
GitHub webhooks enable automatic triggering of the Jenkins CI/CD pipeline whenever code is pushed to the repository. This eliminates the need to manually start builds and ensures continuous integration.How Webhooks Work
When you push code to your GitHub repository:- GitHub sends an HTTP POST request to your Jenkins server
- Jenkins receives the webhook payload
- Jenkins automatically starts the pipeline defined in the Jenkinsfile
- The pipeline executes all stages (Clone, Build, Test, Deploy)
Prerequisites
Before configuring webhooks, ensure:- Jenkins is running on a publicly accessible server (e.g., AWS EC2)
- Jenkins has the GitHub plugin installed
- Your Jenkins server accepts incoming connections on the webhook port
GitHub Webhook Configuration
Step 1: Navigate to Repository Settings
- Go to your GitHub repository:
https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD - Click on Settings
- In the left sidebar, click Webhooks
- Click Add webhook
Step 2: Configure Webhook Settings
The Jenkins webhook endpoint URL.Format:
http://<your-jenkins-server-ip>:8080/github-webhook/Example: http://54.123.45.67:8080/github-webhook/Make sure to include the trailing slash (
/) at the end of the URL.The format of the webhook payload.Select:
application/jsonOptional: A secret token for validating webhook requests.Leave blank or configure a secret token for added security.
Select the events that should trigger the Jenkins pipeline.Recommended: Just the push eventOptions:
- Just the push event (recommended for CI/CD)
- Send me everything
- Let me select individual events
Enable the webhook immediately after creation.Keep this checked to activate the webhook.
Step 3: Save Webhook
Click Add webhook to save your configuration.Jenkins Configuration
Install Required Plugins
Ensure these plugins are installed in Jenkins:- GitHub Integration Plugin
- Pipeline Plugin
- Docker Plugin
- Go to Manage Jenkins > Manage Plugins
- Search for the required plugins
- Install and restart Jenkins if needed
Configure Jenkins Job
- Create a new Pipeline job in Jenkins
- Under Build Triggers, enable:
- GitHub hook trigger for GITScm polling
- In Pipeline section, configure:
- Definition: Pipeline script from SCM
- SCM: Git
- Repository URL:
https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git - Branch:
*/main - Script Path:
Jenkinsfile
Testing the Webhook
Test Push Event
- Make a change to your repository (e.g., update README.md)
- Commit and push to the
mainbranch:
- Check Jenkins dashboard - the pipeline should start automatically
- View the build logs to verify all stages execute successfully
Verify Webhook Delivery
- Go to GitHub repository Settings > Webhooks
- Click on your webhook
- Scroll to Recent Deliveries
- Check for successful delivery (green checkmark)
- Click on a delivery to view:
- Request headers and payload
- Response from Jenkins
Troubleshooting
Webhook Not Triggering
Jenkins not receiving webhook
Jenkins not receiving webhook
- Verify Jenkins is publicly accessible
- Check firewall rules on your server
- Ensure port 8080 is open for incoming connections
- Verify the payload URL is correct with trailing slash
GitHub shows failed delivery
GitHub shows failed delivery
- Check Recent Deliveries in GitHub webhook settings
- Review the response code and error message
- Verify Jenkins is running and accessible
- Check Jenkins logs:
/var/log/jenkins/jenkins.log
Pipeline doesn't start automatically
Pipeline doesn't start automatically
- Verify GitHub hook trigger for GITScm polling is enabled
- Check Jenkins job configuration
- Ensure the branch name matches (e.g.,
mainvsmaster) - Review Jenkins system logs for errors
Security Considerations
Pipeline Execution Flow
Once the webhook triggers the pipeline, the following stages execute automatically:Accessing the Deployed Application
After successful pipeline execution:- Access your application at:
http://<your-ec2-public-ip> - The app runs on port 80 (mapped from internal port 3000)
- Check Jenkins console output for deployment confirmation